438

I have many JPEG files in a directory, and I want to convert them to PDF and concatenate them together to make a single document.

How can this be done?

I would prefer using the command line, as this process will be faster.

1
  • unfortunately this question not accepting answers any more, but give pdfcpu a try it's written in go so it's a single binary and have simple cli interface Commented Jun 26, 2023 at 10:25

10 Answers 10

524

From the imagemagick package, use the convert command:

convert *.jpg -auto-orient pictures.pdf

You will get a single pdf containing all jpg in the current folder. The option -auto-orient reads the image's EXIF data to rotate the image.

Install IM with:

sudo apt-get install imagemagick

sources: stackoverflow imagemagick options

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.

15
  • 2
    Using the joker didn't work for me, I had to use @Alex's solution.
    – Eusebius
    Commented Sep 11, 2015 at 9:31
  • 4
    @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
    Commented Dec 2, 2015 at 0:00
  • 3
    GrphicMagick users should run gm convert *.jpg pictures.pdf Commented Jan 16, 2017 at 9:50
  • 5
    Maybe my jpg files are too big and numerous, but doing that almost immediately consumed so much RAM that my 16GB system started swapping.
    – RonJohn
    Commented Apr 12, 2018 at 21:18
  • 42
    Note that this method may now result in a not authorized error; see this related question (and answers). Commented Apr 16, 2019 at 12:45
188
+50

Unfortunately, convert changes the image quality before "packing it" into the PDF. So, to have minimal loss of quality, it is better to put the original jpg (or .png) into the PDF. To do this, you need to use img2pdf as follows:

(Updated as suggested in the comments) A shorter one line solution using img2pdf:

  1. Make PDF

    img2pdf *.jp* --output combined.pdf
    
  2. (Optionally) OCR the output PDF

    ocrmypdf combined.pdf combined_ocr.pdf
    

Below is the original answer with more commands and more tools needed:

  1. This command is 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 command will concatenate the pdf pages into one document:

    pdftk *.pdf cat output combined.pdf
    
  3. And finally, I add an OCRed text layer that doesn't change the quality of the scan in the pdfs so that they can be searchable:

    pypdfocr combined.pdf
    

    Or, as an alternative to using pypdfocr:

    ocrmypdf combined.pdf combined_ocr.pdf
    
8
  • 5
    This worked fine after convert failed with "attempt to perform an operation not allowed by the security policy `PDF'". Commented May 6, 2019 at 15:33
  • 3
    img2pdf support inputing simultaneously many JPEGs. See examples in its manpage.
    – Yai0Phah
    Commented Jan 2, 2020 at 10:54
  • 3
    img2pdf *.jpeg --output jpegsas.pdf did the trick for me, no piping or multiple steps, ordering was correct. I don't care about ocr though, but that coud be added easily. Thanks! Commented Feb 11, 2020 at 8:52
  • 4
    @MatthiasBraun That is addressed here askubuntu.com/questions/1081695/…
    – yroc
    Commented Mar 17, 2020 at 20:50
  • 2
    This makes the trick! Especially combined with jpegoptim to first control the size of the input pictures, for example: jpegoptim --overwrite --size=1000k *.jp*
    – Antonio
    Commented May 20, 2021 at 13:10
42
convert `ls -1v` file.pdf
  • This ls will list one file a time in a "natural order" (1,2,3...) and proceed with conversion.
2
  • 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
    Commented Dec 1, 2015 at 23:31
  • 6
    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 Commented May 1, 2018 at 13:06
30

Worked for me (BUT warning! +compress options 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.

3
  • 16
    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
    Commented Oct 7, 2014 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
    Commented Oct 8, 2014 at 14:31
  • 3
    so $ converet *.jpg file.pdf worked well with smaller file size than with +compress argument
    – doctorate
    Commented Sep 1, 2015 at 18:02
23

I'm curious nobody pointed out pdfjam, which is a super efficient way to merge images/pdf into a pdf:

pdfjam --a4paper *.jpg

will create for you a pdf in A4 format for all .jpg files, usually named with a -pdfjam.pdf at the end. To force a specific output name, you have a --outfile <your output> option!

As far as I can see, there is no re-encoding of the file, making the command pretty fast compared to convert.

To install pdfjam, I'm not sure to know what's the most efficient way (it comes automatically with LaTeX), but you can try:

sudo apt install pdfjam

or maybe

sudo apt install texlive-extra-utils
6
  • This did the job for me!
    – kmario23
    Commented Mar 3, 2022 at 21:11
  • Liked the A4 format, but not worth the 400MB install of the texlive-extra-utils... Good answer, tho
    – CarlosRos
    Commented Jun 6, 2022 at 21:11
  • Did not work for me. pdfjam: FAILED. The call to /usr/bin/pdflatex resulted in an error
    – mishadr
    Commented Nov 3, 2022 at 15:47
  • @mishadr weird. Maybe create another question with more details, in particular the full log.
    – tobiasBora
    Commented Nov 5, 2022 at 3:51
  • Excellent little program. Exactly what I was looking for. Definitely worth the extra 400MB (see comment above)
    – DAB
    Commented Feb 5, 2023 at 21:18
15

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

I hope, this is simple way to export pdf.

1
7

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
  • 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 Commented Jul 21, 2016 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
    Commented Oct 1, 2017 at 16:55
1

Using img2pdf you can do that.
But sometimes you may need your images converted to document in an order by timestamp or by size or by name.To make that possible this script does that work.

ls -trQ | tr '\n' ' ' | sed 's/$/\ --output\ mydoc.pdf/' | xargs img2pdf

In place of mydoc.pdf, enter name of output file as your wish.
Option of ls command( instead of -tr use these as per your need)

  • -S , sort by file size, largest first
  • -t , sort by modification time, newest first
  • -X , sort alphabetically by entry extension
  • -r , reverse order while sorting
0

Although convert does the job, it tries to open all the source files together and if you have a lot of files and do not have a huge amount of RAM you can run out it.

So as an alternative you can run the following commands in a terminal while being in the folder where the jpg files are.

ls *.jpg | xargs -I% img2pdf -o %.pdf % 

This converts each image to a single page pdf, one by one, without overloading the system. Then:

pdfunite *.pdf output.pdf && rm *.jpg.pdf 

This merges the pdfs into a single pdf and deletes the single page ones.

0

It is humblesome - but file-size can explode - to avoid exploding file-size you can do these steps :

a) At first you need to export with "gimp" the *.jpeg-files to *.jpg-files. (jpeg is Apple format - jpeg and jpg are both NOT the same !). jpg-file would need a small white or black 'passepartout' (=frame).

b) With Android and app "photocompress" I compress the jpg files to size under 300 KBytes each.

c) then back to Desktop of Ubuntu you can edit these files with Libre-Office and create a pdf-map with them.

Surely somebody knows how this works from a) to c) simply in terminal ?

The side-effect of this is, that it can happen, because of correct byte-size the recipient with bad $mickrosaft has then posters, but it is not your fault.

1
  • It's much simpler than that, you can use jpegoptim to control the size of the pictures, for example: jpegoptim --overwrite --size=1000k *.jp*
    – Antonio
    Commented May 26, 2021 at 13:45

You must log in to answer this question.

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