DEV Community

NullPointerZen
NullPointerZen

Posted on

Three ways to get a picture out of a PDF, and the page that returned 0 results


I dropped a one-page PDF with a flowchart and a bar chart into an image extractor, pressed Start, and got this: "Complete", "0 results · 0 KB", and a pale, disabled button that still said "Save this image". No hint about what went wrong. Nothing had gone wrong, as it turns out. The page just didn't contain any images, and it took me a minute to see why that's the right answer. It also made me sort out the three ways I actually know to get a picture out of a PDF, because they're good at completely different things.

Extract PDF images on a vector-only test page I drew:

The test page is one I drew myself with reportlab: four boxes with arrows between them and six coloured bars. Every shape on it is a line or a filled rectangle described in the PDF's own drawing instructions. No photo, no bitmap anywhere. So a tool whose whole job is pulling out embedded images has nothing to pull, and it doesn't fake it with a screenshot of the page. I like that it doesn't fake it. I don't like that it leaves you on a "0" with no next step, because the next step exists and sits one click away in the same workspace.

Way 1: pull out the original embedded image

This is the one to use for photos, product shots, anything that was a picture file before it went into the PDF. ImgIng's Extract PDF images runs in a browser tab and doesn't ask you to sign up. On my own 5-page test file, where a photo is reused on three pages and a logo sits on three pages too, it returned 3 files, each card listing the pages it appears on. The JPEGs it gave back were byte-for-byte the same as the JPEGs I had put in, SHA-256 and all, which is about as "original" as a file can be.

If you're comfortable in a terminal, poppler's pdfimages does the same family of job. Going by its man page, -list prints every image with its page, size, colour space and encoding, and -all writes JPEG, JPEG2000, JBIG2 and CCITT images in their native format, CMYK as TIFF and everything else as PNG. I haven't run it for this post, it isn't installed on this laptop, so treat that as the documentation talking rather than me.

Way 2: render the page and crop

This is what the 0-result page actually needs. Same workspace, left column, switch to PDF to images, set the format and resolution, pick the page. At 216 DPI my A4 test page came out as a 1786×2526 PNG of 142 KB, flowchart and bars nice and sharp. At 300 DPI an A4 page is 2480×3508. Since the chart is drawn from shapes, you can render it bigger and it just gets crisper; there is no "original size" to lose.

PDF to images on the same vector page: PNG at 216 DPI, 1786 × 2526

Two catches. It's the whole page, and the workspace has no crop tool, so cutting the chart out is a job for whatever image editor you already have. And for photos, rendering is the worse deal, not an equivalent one. In another of my test files a 1600×1067 photo was placed smaller on the page, and a 216 DPI render gave me that photo at 1361×908 after cropping. It looked fine, but it was a smaller, re-encoded copy of something the PDF had stored at full size the whole time. A chart figure I stored at 1200×800 and placed 8 cm wide came out at 680×454 from a 216 DPI render and 945×630 at 300 DPI, still short of the 1200 it really was.

So: bitmaps, Way 1. Anything drawn, Way 2.

Way 3: ask for the source file

Slightly boring, often the best. A render of a vector chart is still pixels at the end; if the person who made the PDF can send the SVG, the spreadsheet behind the chart or the design file, you get something you can resize and edit forever. I went looking for an SVG export in the workspace because the footer literally reads "PDF · SVG · OCR", but none of the three modes I used, PDF to images, Extract PDF images and Extract PDF content, offered SVG as an output. Maybe it lives in another tool on the site; I didn't go digging.

My rule of thumb now, for what it's worth: if Extract PDF images shows "0 results" on a page that clearly has a graphic, that graphic is drawn, not embedded, so I switch to PDF to images at 300 DPI and crop. If it shows results, I take those and don't render anything. Both modes live in the same place, https://imging.ai/ , under the PDF tools.

Top comments (0)