82

There are a lot of questions out there about how to convert a PDF file to a PNG image, but I'm looking to take a nice sharp PNG file and just basically wrap it or embed it in a PDF file without having it look blurry or fuzzy.

I realize with imagemagic installed I can do a simple conversion like:

convert sample.png sample.pdf

I've also tried a lot of the switches to set the depth, and also the quality setting:

convert -quality 100 sample.png sample.pdf

However the PDF still comes out looking blurry / fuzzy.

Here's a sample image:

http://img406.imageshack.us/img406/6461/picture3mu.png

As a png it's crisp and clean. When I convert it to a PDF, even at the same size it looks blurry:

Picture 4.png http://img803.imageshack.us/img803/9969/picture4at.png

How can I convert PNG to a PDF in high quality?

4
  • I hope you find a good answer to your question, but I think it is just a given property of PDF to store images in JPG format. PNG, like the one you show us, has much better quality than JPG.
    – jippie
    Jul 12, 2012 at 6:32
  • 1
    @cwd: Did you try to put it in a .tex file, and then generate the PDF? With \usepackage[pdftex, final]{graphicx} and \includegraphics[width=516px]{calendar.png}, for example. Jul 13, 2012 at 8:17
  • 4
    @jippie: No, PDF can store bitmaps losslessly. The link gives a list of compression algorithms rather than formats, because the bitmap data inside a PDF can't be extracted and viewed directly as a JPEG or TIFF, but you wouldn't go far wrong saying that PDF images are either JPEG (lossy), JPEG 2000 (also lossy) or any of several TIFF variants (lossless). What is true, however, is that a given PDF distiller may default to translating bitmaps into DCT (a.k.a. JPEG) form, and have to be told to use a lossless form instead. Jul 13, 2012 at 9:38
  • @cwd Have you thought about accepting some answer? I think user32208's answer is rather good unix.stackexchange.com/a/64495/16920 Jun 25, 2016 at 7:35

7 Answers 7

45

Try using the -density option. The default resolution is 72 dots per inch. So try something like -density 300.

For reference see -density in the ImageMagick command-line options documentation.

3
  • Density definitely seems to be the key. Scale and density seem to be inversely related, so I have been playing with both settings to get an optimum result in terms of both appearance and file size... If there is a set formula, I wish I knew it.
    – Brian Z
    May 2, 2015 at 4:18
  • How to find the best density option? How much is data lost with density 300 with any example picture? I think the result depends on the input. A new thread about it here unix.stackexchange.com/q/292025/16920 Jun 25, 2016 at 7:34
  • 4
    this doesnt change the quality at all for me. Aug 15, 2019 at 19:23
39

It can be very complicated to get good pdf output from convert. Try img2pdf instead. From the readme:

Lossless conversion of images to PDF without unnecessarily re-encoding JPEG and JPEG2000 files. Thus, no loss of quality and no unnecessary large output file.

To clarify: PDF can embed lossless JPEG 2000 images (and most readers appear to support them). So this conversion is completely lossless:

convert sample.png -quality 0 sample.jp2
img2pdf -o sample.pdf sample.jp2

(Assuming the JP2 delegate is available of course: check identify -list format | grep JP2.)

7
  • 6
    This is IMHO the best answer here, but you should explain better your point, i.e. that PDF can embed lossless JPEG 2000 images. So the full command for the OP would be something like: convert sample.png -quality 0 sample.jp2; img2pdf -o sample.pdf sample.jp2. (Assuming the JP2 delegate is available of course: check identify -list format | grep JP2.)
    – Nemo
    Oct 8, 2015 at 20:15
  • 1
    If you want to stick to standard repositories on Ubuntu 14, convert to tiff and then tiff2pdf. Nov 4, 2015 at 17:24
  • This is a great answer, thank you very and very much. Jan 26, 2017 at 0:39
  • @brian-z In your answer, can u please explain the argument -quality 0 ?
    – SebMa
    Mar 13, 2019 at 14:17
  • 1
    @SebMa -quality 0 means don't compress, it's the compression level from the interval [0, 100]
    – hroptatyr
    Jul 3, 2019 at 12:05
17

I am almost certain that what you perceive as a loss of quality in the PDF, is just an effect of your PDF viewer's anti-aliasing feature.

If you use evince to view the PDF, you can see the anti-aliasing feature automatically switched off at a certain zoom (300% in my quick test). You can see that vividly when you keep zooming in - you will notice that at some point, pixels become suddenly clearly visible. That is the point when anti-aliasing must have been switched off to allow precise image inspection.

2
  • Hmm - that makes sense but I guess I was hoping to be able to somehow set the image and the "initial view" to 100% so that it looks crisper.
    – cwd
    Jul 12, 2012 at 20:10
  • 1
    @cwd Don't mistake the zoom (a way of examining the file) with the actual cause of image smoothing: anti-aliasing. The image is stored properly. It is the PDF viewer that fools you. But it could be the other way around even - if you'd take some other viewing applications or change their settings. You could then see a sharp image in a PDF file and a smoothed PNG file in an image viewer. Jul 13, 2012 at 6:37
8

Brian Z above provided the below, which is the correct, fully reversible, and lossless (assuming the convert step is in fact lossless, which I think it is or at least ought to be) way to put png's into a pdf. You are required to convert from png to lossless jp2 in order to be compliant with PDF structure / readers (I think).

$ convert sample.png -quality 0 sample.jp2
$ img2pdf -o sample.pdf sample.jp2

However it is worth noting that you can supply the .png files themselves directly to img2pdf like :

$ img2pdf -o sample.pdf sample-page1.png <sample-page2.png ...>

This will produce the smallest pdf file, and will insert the png's raw hex into objects within the pdf losslessly*.

*The drawback, is this process is not reversible unlike using jp2. The header/footer and and chunk header/footer data has been stripped (which actually makes the files even smaller!) from the png that is inserted leaving only the raw picture data (the metadata that was deleted is integrated into the pdf structure), presumably to "hack" the pdf to display raw png which is technically non-compliant with the container. It displays fine in firefox, and may display fine in all modern readers, but if png is non-compliant with the container then strict readers may not render the raw png data (as they should not expect it / process it correctly).

Here you can find an extremely raw bash script that worked for me to extract and reconstruct the png files with matching hashes to the ones used for input into the pdf. https://github.com/jack4455667788/RebuildRawPNGExtractedFromPDF

0
3

PDF is a vector format (i.e., the file contains a description of lines to draw), while other formats (JPG, PNG) are raster formats (the file describes what color to paint each pixel). If you blow a PDF up, it is still just sharp lines; JPG and PNG show the pixelation.

(OK, OK, I lied. A PDF can also be a raster).

2

I think that for png-to-pdf -density parameter should small rather than large. You could try something like convert -quality 100 -density 50

2
  • 5
    Nope higher density is definitely better, just did a test, 50 results in lots of very visible pixels, 300 is nice and crisp looking. Sep 28, 2014 at 23:13
  • This has been confusing me, but I think a higher density results in output with lower resolution. That means that if the output is fuzzy (over-aliased, like the example in the original question) then lower density is what you want. But if a PDF is pixelated then indeed you need to convert with a higher density.
    – Brian Z
    Apr 22, 2015 at 7:32
1

I tried using famous imagemagick's convert but got errors.

The following worked for me with no errors:

(1) Install and use GraphicsMagick:

sudo apt-get install graphicsmagick

(2a) To convert a image, run:

gm convert file.jpg file.pdf

(2b) Convert multiple images:

gm convert file1.png file2.png file3.jpg file.pdf

For more, just run:

man gm

You must log in to answer this question.

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