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.
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.
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!
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?
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.
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?
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.
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.
OK thank You very much Steffan. Happy New Year 🙂
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.
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:
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:
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
I hope this helps someone else out, because I was running in circles for a while, lol, take care.
I swear I thought I posted this link showing the command out of sequence:
If it doesn’t get posted then here is a link that threw me off:
That one had +compress for instance instead of -compress and was in the wrong spot as I said before.
I forgot to write quote and put double quotes:
This link helped me:
“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.
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’