Tuesday, December 08, 2009

png2pdf with pdflatex

Recently I needed to create a PDF from two scanned pages and found out that there was no png to pdf converter on openSUSE. Or at least I could not find it. The "png2pdf" project that I found on sourceforge when searching the net was already abandoned by its author and, as its successor, "bmeps", needed the very obscure and hard to package "dklibs" package.

Then I remembered that I had used some TeX template to create my slides for FOSDEM etc over the last years, and that they included an openSUSE logo in PNG format.

5 minutes later I came up with:

Which did its job reasonably well. Don't ask me why I needed that weird \geometry{} setting - I know next to nothing about TeX. But it produced a nice PDF with both scanned pages, so it was good enough for me ;)


  1. I am successfully using 'convert file*.any file.pdf'.

  2. Why not use ghostscript, imagemagick, or the CUPS PDF printer?

  3. The convert tool from ImageMagick is able to produce pdfs from pngs.
    Then there are also tools to convert png to pnm (pngtopnm) and from pnm to ps (pnmtops) and finally from ps to pdf (ps2pdf).
    Don't know which method provides the best output though.

  4. Michal, that works pretty well. I actually did not expect it to be so easy ;) I assumed that it surely would not create a multiple-page PDF, but it does, and very nicely so.

    anon, I personally find ghostscript always a pain to use, even more than TeX ;), and the CUPS PDF printer - well, my wife would probably just have drag-and-dropped the PNGs into an openoffice writer document and then just "printed" that to PDF, but hey, there really is no fun in doing that, is there? ;)

    Thanks for the suggestions, I'll write down the "convert" line for future reference.

  5. Gulli, yes, I just did not think of ImageMagick. The output is pretty good, I cannot spot any differences to the PDF I got from pdflatex, and the size is about the same (a few kB bigger with convert, but that's for a 1.2MB PDF).

    I knew that there was the possibility of going through at least three different formats and then to pdf, but I wanted to avoid that.

    And I had used TeX template the day before to write a letter, so the logical choice for the question "how to create a PDF?" was "use pdflatex".

    And in the end, my 9 lines of TeX are not more awkwared than going through pnm and ps to get pdf. I still would need to arrange the two pages then ;)

  6. xsane can scan directly to PDF. Regarding multipage: The tool "pdftk" can easily concat more pages to one PDF file and do much more.

  7. Bernhard, xsane would be an option - if I did not want to touch the scans before converting (for example setting the background color of the scanned document to all plain white instead of the thousands of different greys it had before surely helped get the size down from 10MB per page to 3MB per page, the rest was saved by going from 600dpi to 300dpi, now the whole PDF is ~1.2MB)

    But keep the suggestions coming, I'll probably collect them all and put them into a new post, so that others benefit from them ;)

  8. - On kde, open the image with gwenview -> Ctrl+P -> print to pdf. There must be something similar under gnome...
    - Create a new OOo Draw file -> insert the png -> File -> Export as pdf
    Life is short... ;)

  9. Ah, and I think the negative left margin is because the first line of a paragraph gets indented in LaTeX (instead of separating paragraphs with space, LaTeX indents the first line by default). Das kann man mit




    abschalten. Dann sollte ein linker Rand von 0cm und eine Blattbreite von 210 mm (A4) funktionieren.

  10. It may also be useful to compress the images in the PDF:
    "convert -compress jpeg scan*.jpg scan.pdf". ImageMagick rulez. Thanks to Prague colleagues for the tips. http://twitter.com/mvidner/status/1354243963

    (LaTeX! Switches your brain to German!)

  11. I think that the PNGs are actually pretty well comressed already (I converted them to 256 color palette PNGs with GIMP before etc), so there is probably not much to gain. But I will also write this down for future reference.

  12. [...] direkt in den Quellen liegt und höchstwahrscheinlich schon installiert ist:convert *.png file.pdf(via) Geschrieben von onli in Linux um 15:09 | Kommentare (0) | Trackbacks [...]