214

I have many jpeg files in a directory, and I want to convert them to pdf and concatenate them together to make a document. How can it be done? I'd prefer the command line, as it will be faster.

324

From the imagemagick package, use the convert command:

convert *.jpg pictures.pdf

You will get a single pdf containing all jpg in the current folder.

Install IM with:

sudo apt-get install imagemagick

source: stackoverflow

Edit: Note that images will be out of specific order if they are not numbered. if you have 10 or more you need to name them ending filename01.jpg...filename99.jpg etc. The leading zeros are required for proper ordering. If you have 100 or more 001...999.

  • 9
    Wow, this is soooo convenient! – king_julien Apr 19 '14 at 21:39
  • 4
    @lovespeed Why has this answer not been accepted? – MrHug Oct 7 '14 at 20:45
  • 1
    Using the joker didn't work for me, I had to use @Alex's solution. – Eusebius Sep 11 '15 at 9:31
  • 2
    @Eusebius if the wildcard doesn't work for you with convert, you could use a oneliner like for f in *.jpg; do convert "$f" "$f.pdf"; done; pdftk *.pdf cat output final.pdf – Elder Geek Dec 2 '15 at 0:00
  • 1
    GrphicMagick users should run gm convert *.jpg pictures.pdf – michaelbn Jan 16 '17 at 9:50
31
convert `ls -1v` file.pdf
  • This ls will list one file a time in a "natural order" (1,2,3...) and proceed with conversion.
  • 1
    Just what I was looking for to convert scanned images of an old book to a PDF version. – eshwar Apr 9 '15 at 5:08
  • 1
    I tried this and it didn't work for me. numerous errors indicate that it might have something to do with spaces in the filenames. – Elder Geek Dec 1 '15 at 23:31
  • 4
    To take care of the spaces issue, perform a pattern replacement first: for ThisFile in *.jpg; do mv "$ThisFile" "${ThisFile// /_}" >/dev/null 2>&1; d – Simon Mattes May 1 '18 at 13:06
19

Worked for me (BUT warning! turns off compression and resulting PDF will be big!):

convert page1.jpg page2.jpg +compress file.pdf

or even:

convert -rotate 90 page\*.jpg +compress file.pdf

From ubuntuforums.org, the +compress helps it to not hang. NOTE: the +compress turns off compression. The machine I was working on at the time seemed to hang ?forever?(I did not wait forever though to find out.) without the +compress option. Your Mileage May Vary quite a bit! RTFM on imagemagick.org option -compress, maybe experiment with -compress < type> if you have slow compression/hanging problems to find out what will work for you.

  • 11
    Do NOT use the +compress option with the convert command as suggested above! It actually disables all compression leaving you with a PDF 10 times bigger than the original JPEG. Just don't specify compression options, and convert will go with the input compression format (JPEG) which in this case is the best option file size-wise. Source: http://www.imagemagick.org/script/command-line-options.php#compress – user335432 Oct 7 '14 at 20:25
  • That is not intuitive! Thanks S Minddal. That did work for me. I presume the machine I was using had a problem doing the compression then. I will adjust answer. – gaoithe Oct 8 '14 at 14:31
  • 2
    so $ converet *.jpg file.pdf worked well with smaller file size than with +compress argument – doctorate Sep 1 '15 at 18:02
  • +1 for -rotate – raacer yesterday
10

Open jpg or png file with LibreOffice Writer and export as PDF.

I hope, this is simple way to export pdf.

  • Yes! LibreOffice is the best way to export image files to PDF. We have full control of the PDF output, including page layout, resizing of imported files, optional headers and footers e more... Many thanks to user359154 for the clever suggestion! – user371260 Jan 22 '15 at 16:59
8

The following solution also relies on ImageMagick's convert but is a bit more sophisticated because:

  • It allows all images to have different dimensions, while keeping the output PDF page size the same (A4 in the example below).
  • It centers the images on the PDF page.
  • It allows you to guarantee a minimum space between image borders and PDF page borders, to allow printing the PDF without problems.
  • It does not change the image data. (So image quality is unaffected, the PDF file has about the same file size as the image, and you can re-extract the original images later with pdfimages -j file.pdf img.) At the moment, this only works with PNG – see the comment by @dma_k below.

Instructions:

  1. Use my script from this answer to convert each image into its own one-page PDF file with A4 page size and 5% border all around.
  2. Concatenate all your one-page PDF files with PDFtk as follows:

    pdftk *.pdf cat output out.pdf
    
  • 2
    This command shows that (at least with the versions supplied on Ubuntu 16.04) image data is affected: convert some.jpg -format pdf -compress jpeg generated.pdf ; pdfimages -j generated.pdf generated.pdf ; diff -sq some.jpg generated.pdf-000.jpg – Stéphane Gourichon Jul 21 '16 at 0:14
  • 2
    I was playing with GraphicsMagick and I have noticed that it performs image recompression. The quality of the resulting JPEG image on PDF page is much worse. So at the moment the safest way is to add PNG images – they are inserted correctly. See this post as well. – dma_k Oct 1 '17 at 16:55
6

Unfortunately convert changes the image quality before "packing it" into the PDF, so to have minimal loss of quality, is better to put the original jpg into the PDF, you need to use img2pdf, I use this commands:

1) This to make a pdf file out of every jpg image without loss of either resolution or quality:

ls -1 ./*jpg | xargs -L1 -I {} img2pdf {} -o {}.pdf

2) This to concatenate the pdfpages into one:

pdftk *.pdf cat output combined.pdf

3) And last I add an OCRed text layer that doesn't change the quality of the scan in the pdfs so they can be searchable:

pypdfocr combined.pdf  

Alternatively to pypdfocr:

ocrmypdf combined.pdf combined_ocr.pdf  
  • 1
    Great method! The only disadvantage without compression would be that the resulting .pdf filesize will be very high (that is, slightly larger than all pics combined). But worth it if the document you're converting is really an important one! – Gokul NC Dec 23 '18 at 13:14
-2

I have used http://convert-my-image.com/ The positive thing that you can provide (the same site but different page) an archive of images and concatenate them to the common pdf

  • 1
    This suggestion has at least three problems: (1) it will be slow, due to uploading the images to the website and downloading the resulting PDF; (2) it exposes your data to whoever runs that website, and to anyone who may have breached that website, and to anyone on the wire; (3) it is not a command-line solution. – sampablokuper Oct 12 '18 at 16:59

protected by Community Jan 22 '15 at 17:08

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.