Convert multiple JPG or PNG to PDF in Linux

I think we all have been in the situation before, you have some scanned documents in JPG or PNG format that you need to convert into a PDF file.
There are many solutions for this on the internet, including free sites that just does it for you if you upload your images, but they almost always put some kind of watermark on the pages if you don’t pay them.
There are also a bunch of Windows applications that do this, but they are often complicated or full of ads.
This guide show you a free solution on Linux, that just works, is fast, and does not add any crap to the PDF.

Requirements

To do this, you don’t need much. only 2 programs that can be installed with your package manager easily!
I am using Arch Linux on my laptop, but it should be the same on any other Linux system except the installation command for the 2 programs you need might be a little different.

Installing software

First you have to install the software you need to make the magic happen. for this you need 2 applications called “ghostscript” and “imagemagick”.
Below are the commands needed to install it on Arch Linux and Debian/ubuntu Linux. If you know the installation command to other Linux distributions please leave a comment so i can add them.
NOTE: It is really important that ghostscript is installed first, else imagemagick will not know where it is located doing compilation.
Install it on Arch Linux with the following command:

pacman -S ghostscript imagemagick

Install it on Debian/ubuntu Linux with the following command:

apt-get install ghostscript imagemagick

Convert the images to PDF

When you have installed the 2 applications, then you are ready to convert your files.
You then have to make sure your images are in the right order. Page1 has to be called 1.png or 1.jpg depending on your filetype. Page2 has to be called 2.png or 2.jpg and so on….
If you don’t have the images in the right order with numbers, the pages will be mixed randomly when generating the PDF.
The command for this is the same in every distro.

convert *.png outputfile.pdf

Above you see the command to convert your images, in this example I am converting all .PNG files in the current folder to outputfile.pdf
If you need to do it for all .JPG files, the command would be:

convert *.jpg outputfile.pdf

You can change the name of the pdf to whatever you want just by changing the name in the command.
There you go, no ads, free, fast and simple!

9 thoughts on “Convert multiple JPG or PNG to PDF in Linux

  1. Piotr

    Hi
    I have put the pages in wright order, but they are in random order after convertion. I noticed, that when I do ls -A, the pages in *jpg format are going in the same order as in pdf. What can I do to get wright conversion?

    Reply
    1. Steffan Post author

      That might be because if you name them numbers 1,2,3….9,10.. 10 will come before 2.
      Try naming them letters instead when you have over 9 pages. like a.jpg, b.jpg, c.jpg and so on.. that should be the easiest way to fix your paging problem.

      Reply
      1. Piotr

        Hi Steffan, thanks a lot for Your quick response.
        I have 61 pages, so after 9th page I started to use letters and capitals. The order is that numbers go first, than small letter, capital letter and so on. For example 1,2,3…..9,a,A,b,B……z,Z. I handled this problem because for me it stopped as Z. Is there a solution for unlimited number of pages?

        Reply
        1. Steffan Post author

          They get sorted the same way as if you do a “ls -l”
          So you can use multiple letters, or numbers. however you want.. the limit is in the filenames and that limit is so high you will never get there.
          example:
          1.jpg
          10.jpg
          11.jpg
          2.jpg
          21.jpg
          2222222222222222.jpg
          9.jpg
          a.jpg
          aa.jpg
          aaa.jpg
          aab.jpg
          aac.jpg
          aad.jpg
          And so on.. you are not limited to 1 letter or number only. just check with “ls -l” before you convert them to PDF to see if they are in the right order to save youself some time.

          Reply
  2. Dr. Clock

    Here is what I have just in case anyone wants, needs it:
    It has up to page 150 and there were pages I had skipped so I tried to fill in what was missing.
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    10
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    11
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    12
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    13
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    14
    150
    15
    16
    17
    18
    19
    1
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    2
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    3
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    4
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    5
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    6
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    7
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    8
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    9
    Here is the command that worked for me:
    convert -compress JPEG *.jpg output.pdf
    I found several links that I used commands from, but that was the command that came out being the same size as the JPEGs took up.
    Here for instance:
    convert source.png -compress zip source.pdf
    the compress option is in the wrong place, or at least it was for me and I didn’t catch it. Clearly under the manpage it says:
    convert [input-options] input-file [output-options] output-file
    This link helped me:
    http://ubuntuforums.org/archive/index.php/t-1267482.html
    As an aside, I am not sure why I used zip compression in my earlier post, because JPEG compression typically yields smaller file sizes with little or no difference in quality. With newer versions of convert, I believe JPEG compression is enabled by default for JPG files.
    The funny thing is I have looked at so many links for finding the right program that I missed the command he posted which is the command I just last used. I don’t remember where I got that command from, lol.
    I also read some of this page while unsure what to do next:
    http://www.imagemagick.org/Usage/formats/#vector
    I mean when I did other commands it once went from taking up 1.2 GBs to 14 GBs.
    I am running Ubuntu 10.04, but as I said before the pdf file came out as 1.2 GBs from the JPEGs that were also taking up 1.2 GBs.
    I am sorry that the info is partly out of place
    and
    I hope this helps someone else out, because I was running in circles for a while, lol, take care.

    Reply
  3. Dr. Clock

    I forgot to write quote and put double quotes:
    This link helped me:
    http://ubuntuforums.org/archive/index.php/t-1267482.html
    Quote:
    “As an aside, I am not sure why I used zip compression in my earlier post, because JPEG compression typically yields smaller file sizes with little or no difference in quality. With newer versions of convert, I believe JPEG compression is enabled by default for JPG files.”
    Sorry Moderator. Thank you for this program and the online help. Have a good day.

    Reply
  4. Bruno Delsupexhe

    Here is a oneline command to rename files so numbers are preceeded by 0, so 1 becomes 001 and 10 becomes 010, then all files are correctly ordered prior to pdf merge. It works for values smaller than 1000, using 3 digits from 000 to 999
    mmv ‘PrefixFileName[0-9].*’ ‘PrefixFileName00#1.#2’ && mmv ‘PrefixFileName[0-9][0-9].*’ ‘PrefixFileName0#1#2.#3’

    Reply

Leave a Reply

Your email address will not be published.