Browser-local images to PDF with pdf-lib: orientation, page fit, and memory tradeoffs #1790
MarcoRysz
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I maintain Refentra Images to PDF, a browser-local tool using pdf-lib to combine 1–25 JPEG, PNG, or static WebP images into an ordered PDF. It offers A4, Letter, and image-fitted pages without an account or uploading the source images.
A few implementation choices may be useful to others building an image-to-PDF flow:
createImageBitmap(..., { imageOrientation: "from-image" }), check the decoded dimensions against our image inspection, and paint it onto a whiteOffscreenCanvas. We encode that canvas as JPEG, then callPDFDocument.embedJpg. This gives transparent PNG/WebP inputs an explicit white background. The tradeoff is lossy re-encoding: this is not a lossless archival workflow.Math.min(pageWidth / imageWidth, pageHeight / imageHeight). We center the scaled image using(pageWidth - width) / 2and(pageHeight - height) / 2. Auto orientation follows each image; mixed portrait/landscape batches can therefore have different page orientations.finally, and check batch bytes, decoded pixel counts, accumulated normalized JPEG bytes, and final PDF size. Sequential processing reduces simultaneous bitmap allocation, but pdf-lib still retains the document and its embedded images until saving; it is not streaming output.The result contains raster image pages, with no OCR or searchable text layer. Browser decoding and encoding support still matters.
Disclosure: I am the Refentra maintainer. The application is closed source and AI-assisted; this post was also prepared with OpenAI Codex assistance and checked against the implementation. Optional usage counts require opt-in. AdSense is planned behind approval and consent; ads are currently locked off. The browser-local processing claim concerns source files, not a claim that the entire website makes no network requests.
All reactions