118

Openning a png file in ubuntu, I can see the menu item for 'print to file'. How can I do the same on shell? PS: I prefer installing no extra package, due to lack of root access.

EDIT: the operating system is ubuntu 11.10

ish
  • 139,926
Richard
  • 1,777

4 Answers4

176

convert xyz.png xyz.pdf should do the trick.

See man convert for more options.


As mentioned in a comment below, if you get a security error, you may need to edit the security policy file. For additional details see:

Alexis Wilke
  • 2,707
abhshkdz
  • 3,989
  • 20
    +1 - additionally you can use wildcards: convert *.png file.pdf – Geppettvs D'Constanzo Jun 30 '12 at 18:49
  • does this require extra packages? – Richard Jun 30 '12 at 19:27
  • 3
    Yes it requires the ImageMagick package, although it was there by default on my Ubuntu 12.04 – abhshkdz Jun 30 '12 at 19:29
  • Also there by default on lubuntu 12.10. – Rasmus Nov 23 '12 at 07:59
  • 5
    Be careful if you use wildcards, files might not appear in the desired order, especially if filenames contain numbers (for example, a file named 17.png will precede a file named 2.png). To preview the order in which they will be merged into the pdf file, you can use the command echo *.png. –  Apr 10 '15 at 20:24
  • 2
    If you're using more recent versions of Ubuntu, you probably need to make this security exception: https://askubuntu.com/questions/1081895/trouble-with-batch-conversion-of-png-to-pdf-using-convert PDF looks beautiful, original images are unharmed. – GlenPeterson Jun 23 '19 at 17:33
  • 5
    convert-im6.q16: not authorized `h1.pdf' @ error/constitute.c/WriteImage/1037. – momo2047 Mar 12 '20 at 22:13
  • 8
    attempt to perform an operation not allowed by the security policyPDF'` – Cerin Apr 06 '23 at 21:02
  • 1
    @Cerin same issue and this anwser fixed it https://stackoverflow.com/a/53180170/5472004 – Martin Tovmassian May 05 '23 at 14:20
33

If you want to convert multiple images, e.g. png files, into one single pdf use convert with the specified pdf filename at the end

convert *.png mydoc.pdf

It will merge all png files into a sinlge mydoc.pdf file in a descendant order.

EliuX
  • 439
7

In 18.04 LTS, open image using ImageViewer; then print to file as a PDF.

Andor Kiss
  • 779
  • 1
  • 6
  • 19
1

If converting each PNG file to separate PDF files is necessary:

for file in *.png; do convert ${file} ${file:0:-4}.pdf; done

This command takes all PNG files in a directory and produces PDF files with the same name.

  • Is that Imagemagick's convert command? Might it be the case that your answer is valid only up to Imagemagick V6? V7 would need magick convert? See: https://askubuntu.com/a/1315605/1157519 (Ah o.k. this thread is full with convert... Then again, those are dated answers.) – Levente Dec 19 '22 at 23:16
  • 1
    Don't use $(ls *.png) for looping over files. It's vulnerable for whitespace and unnecessarily verbose. Just use for file in *.png, which is neither nor. – user unknown Dec 19 '22 at 23:54