Menu

How I built a free Image to PDF converter that runs 100% in your browser?
📰

How I built a free Image to PDF converter that runs 100% in your browser?

DEV Community: javascript·AFMEI·13 days ago
#SLMp5cKy
#devtojavascript#devto#dev#const#jspdf
Reading 0:00
15s threshold

Most "free" image to PDF sites upload your files to a server, add a watermark, or ask you to sign up. I wanted one that does none of that, so I built it with plain JavaScript and one small library.

Try it: Image to pdf

How it works?

The user picks images (or drops them in).

Each image is read in the browser with FileReader and shown as a thumbnail.

When the user clicks convert, jsPDF adds one image per page and builds the PDF in memory.

The browser downloads the PDF. Nothing is sent to a server.

The core code

js const { jsPDF } = window.jspdf; const pdf = new jsPDF({ orientation: 'portrait', unit: 'mm', format: 'a4' });

images.forEach((img, i) => { if (i > 0) pdf.addPage(); const pageW = pdf.internal.pageSize.getWidth(); const pageH = pdf.internal.pageSize.getHeight(); const margin = 10; const ratio = Math.min((pageW - margin * 2) / img.width, (pageH - margin * 2) / img.height); const w = img.width * ratio, h = img.height * ratio; pdf.addImage(img.src, 'JPEG', (pageW - w) / 2, (pageH - h) / 2, w,…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.