Menu

1 / 4

Converting PDF pages to JPG entirely in the browser: PDF.js, DPI math and the iOS canvas limit

DEV Community: frontend·shaojie gong·20 days ago
#nJW1rQc3
#devtofrontend#devto#dev#canvas#scale
Reading 0:00
15s threshold

I'm the developer of MakeQuickWork, a small collection of free browser tools. This post is about how one of them, PDF to JPG, works under the hood.

Most "PDF to JPG" sites follow the same pattern: you upload the file, a server renders it, and you download a ZIP a few seconds later. That works, but it means a contract, a payslip or a medical letter sits on someone else's server for a while. I wanted a version where the PDF never leaves the browser tab. It turned out to be a nice little exercise in canvas, DPI math and browser quirks.

The tool is live at https://makequickwork.com/pdf/pdf-to-jpg/ if you want to follow along with DevTools open.

The pipeline

The whole thing is four steps, all client side:

Read the File the user picked into an ArrayBuffer .

Open it with PDF.js (Mozilla's PDF renderer).

For each page, render to a <canvas> at the scale that matches the chosen DPI.

Encode the canvas with canvas.toBlob() as JPEG or PNG, and if there are several pages, zip them with fflate.

One detail that…

Continue reading — create a free account

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