8

You know when you have a pdf, which is a scan of a document and it's a really huge file, because it just stores the picture of the scanned document?

And there are OCR tools which can help you to make a proper document which just stores the text?

Well, I need the reverse of that! Let's say I have a perfect pdf document generated with pdflatex and I need to turn it into such a "huge" pdf, which looks exactly the same when printed on paper (with a certain dpi value), but is just a picture of the original.

My initial idea is to turn the pdf into a series of JPGs and then back into a PDF, but perhaps there is some canonical way for that?


In case you wonder why I would want to do such a thing: I'm currently stuck with a network printer, which is not maintained by me, and which randomly drops characters in printed files! So until someone figures out what's wrong there, I want this as workaround.

4
  • Basically you want to convert Latex or PDF to JPG/PNG files. And print that. (Just a simplification of what you wrote.)
    – Apache
    Apr 26, 2015 at 14:14
  • But I want to still store a document with multiple pages in a single file, so that I can use the features of my pdf viewer, e.g. print 2 document pages on one paper page and so on. I imagine this to be cumbersome with loose PNG files. Apr 26, 2015 at 14:23
  • GhostScript can render PDFs into PNG.
    – Palec
    Apr 26, 2015 at 18:01
  • Same question on SuperUser: How to distill / rasterize a PDF in Linux - Super User
    – user202729
    Nov 13, 2023 at 1:51

4 Answers 4

4

You could test out if image based PDF's are polluted as well. First convert PDF to (multipage) TIFF, e.g. with ghostscript:

gs -sDEVICE=tiffg4 -o sample.tif sample.pdf

Then convert the TIFF to PDF, e.g.:

tiff2pdf -z -f -F -pA4 -o sample-img.pdf sample.tif

This result in a PDF file where the pages are images instead of text.

Alternatively, if your system supports printing of TIFF files try to print it directly.

There is also the option of pdf2ps for converting PDF to PS, which if works, would likely be preferable.

3
  • 1
    Works great! Also pdf2ps. May 1, 2015 at 19:34
  • @DimitriSchachmann: Good. Also note that one have pdf2svg if one want vectorized documents.
    – Runium
    May 3, 2015 at 23:17
  • 1
    Nice. After pdf2ps my.pdf my.ps and then ps2pdf my.ps mynew.pdf, mynew.pdf still contains the text in my.pdf, i.e. it is not rasterized. Am I missing something?
    – Tim
    Sep 23, 2017 at 1:20
2

I did it the way Dimitri described in the comments by using pdf2ps and ps2pdf.

First I converted my pdf to a .ps format by using the command

pdf2ps my_file.pdf my_file.ps

And then converted it back to pdf format by

ps2pdf my_file.ps my_file.pdf

This way I got a rasterized version of the original pdf where the content is actually an image. Hope this helps.

2
  • Great, just a reminder that pdf2ps and ps2pdf are from the ghostscript, so probably need to install ghostscript if your system doesn't come with it.
    – xjlin0
    Apr 6, 2021 at 14:51
  • This solution does not guarantee that the PDF will be rasterized! Although sometimes the PDF is indeed rasterized.
    – user202729
    Jan 28 at 22:22
2

Solution

The following code rasterizes a.pdfc.pdf at 1200 DPI, by initially rasterize at 2400 DPI then downscale by 2 before output. Documentation.

gs -dNOPAUSE -dBATCH -sDEVICE=pdfimage24 -r2400 -dDownScaleFactor=2 -o c.pdf a.pdf

If you only need to print black and white, you can replace pdfimage24 with pdfimage8 in order to speed up.

Benchmark

As can be seen, the solution above is the fastest, only lose to pdf2ps + ps2pdf (but pdf2ps + ps2pdf is not guaranteed to rasterize the file), and to pdf2ppm (but convert to .jpg enlarge the file)

Solution Time taken (s) Memory taken (KiB) Output file size (KiB) Printing time (s)
pdftoppm (1200) (.jpg) + img2pdf (†) 2.710 603092 10341.3
pdf2ps + ps2pdf with temporary file (source) (*) 4.110 37596 1706.4
pdfimage8 (1200) 4.180 35668 2348.6 9.5
pdfimage24 (1200/2) 5.020 36088 1971.9 9.7
pdfimage24 (1200) 6.520 36212 3316.1
pdf2ps + ps2pdf with pipe (*) 7.230 37668 1706.4
convert (600) 9.560 964532 5953.6
pdftoppm (1200) (.tiff) + img2pdf (†) 10.850 1539512 14483.3
convert (600) + gs to optimize (source) 12.010 964532 1989.4 9.9
pdfimage8 (2400/2) 20.350 43872 3481.9
pdfimage24 (2400/2) 23.510 46484 4833.2 15.8
pdftoppm (1200) (.png) + img2pdf (source) (†) 33.000 626896 14127.2

(*): Solution doesn't actually always rasterize PDF, but gs may decide to do that in some (it's unknown in which case it will, probably the cases where the PDF is too complicated) cases.

(†): the code as written will only work for 1-page PDF file, but it can be adapted.

Details of benchmarked solutions

pdftoppm (1200) (.jpg) + img2pdf (†)

pdftoppm -progress -r 1200 -jpeg a.pdf a
img2pdf a-1.jpg -o c.pdf

pdf2ps + ps2pdf with temporary file (source) (*)

gs -sDEVICE=ps2write -dNOCACHE -sOutputFile=c.ps -q -dBATCH -dNOPAUSE a.pdf
ps2pdf c.ps c.pdf

pdfimage8 (1200)

gs -dNOPAUSE -dBATCH -sDEVICE=pdfimage8 -r1200 -o c.pdf a.pdf

pdfimage24 (1200/2)

gs -dNOPAUSE -dBATCH -sDEVICE=pdfimage24 -r1200 -dDownScaleFactor=2 -o c.pdf a.pdf

pdfimage24 (1200)

gs -dNOPAUSE -dBATCH -sDEVICE=pdfimage24 -r1200 -o c.pdf a.pdf

pdf2ps + ps2pdf with pipe (*)

gs -sDEVICE=ps2write -dNOCACHE -sOutputFile=- -q -dBATCH -dNOPAUSE a.pdf -c quit | ps2pdf - c.pdf

convert (600)

convert -density 600 a.pdf c.pdf

pdftoppm (1200) (.tiff) + img2pdf (†)

pdftoppm -progress -r 1200 -tiff a.pdf a
img2pdf a-1.tif -o c.pdf

convert (600) + gs to optimize (source)

convert -density 600 a.pdf b.pdf
gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite  -sOutputFile=c.pdf b.pdf   -q

pdfimage8 (2400/2)

gs -dNOPAUSE -dBATCH -sDEVICE=pdfimage8 -r2400 -dDownScaleFactor=2 -o c.pdf a.pdf

pdfimage24 (2400/2)

gs -dNOPAUSE -dBATCH -sDEVICE=pdfimage24 -r2400 -dDownScaleFactor=2 -o c.pdf a.pdf

pdftoppm (1200) (.png) + img2pdf (source) (†)

pdftoppm -progress -r 1200 -png a.pdf a
img2pdf a-1.png -o c.pdf

Source code of the benchmark can be found here.

1
1

The accepted answer should cover most use cases. However, I found myself in the situation that I wanted to rasterize to a specific resolution. This answer to a similar question introduced me to the tool pdftoppm which yielded the best quality results.

A simple usage example would be

pdftoppm input.pdf output -tiff

which results in files named output-X.tif, where X corresponds to the page number of the PDF file.

1
  • Thanks for sharing! But how do you specify the resolution?
    – tomekwi
    May 7, 2021 at 11:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .