Creating multi-page PDF files with GIMP and `convert`

Occasionally I have to sign some document (old style, with a pen) and send it electronically. Sometimes those are multi-page documents. Since it is uncommon to send it back as multiple image files after scanning, and multi-page image formats are uncommon as well, I’d like to send them as PDF file. Before I discovered this method, I used to insert the scanned images into OpenOffice Writer, and then create the PDF with it. This works, but it is a bit cumbersome to tell OpenOffice Writer to maximise the images (eliminating page borders, etc.), especially when there are a lot of pages. It just doesn’t feel like a real solution.

So, here we go:

Prerequisites:

  • GIMP (I’m currently at version 2.6.8, but this will probably work with older versions as well)
  • GraphicsMagick (tested with 1.3.8) or ImageMagick (tested with 6.5.8.8)

Procedure:

  1. Get the scanned pages opened as layers of one image in GIMP. If they are available as files already, you can use File / Open as Layers….
  2. Make sure that the layers are ordered in the following way: Page 1 must be the bottom layer, the last page must be the top layer. You can reorder them via the “Layers” dialogue (activate it via the Windows / Dockable Dialogues menu if you don’t see it)
  3. Save As… and choose “MNG animation” or just add “.mng” to the filename. (In case you are wondering, MNG is the animated counterpart to PNG).
    A dialogue window saying “MNG plug-in can only handle layers as animation frames” will come up – choose “Save as Animation” here and press the Export button. In the next dialogue you don’t need to make any changes to the defaults, just press the Save button.
  4. Now, open a console window and simply enter
    convert document.mng document.pdf

That’s it – you now have your PDF file ready for sending!

Update (2010-02-08):
As chithanh pointed out in comment 1, there is another convenient way to accomplish the same. It does not involve GIMP, but instead requires pdftk to concatenate PDF files. Please see comment 2 for details.

Update (2010-03-01):
And yet another way (definitely the most straight-forward one, if you have the pages as single image files already) was pointed out by goffrie in comment 5.

61 thoughts on “Creating multi-page PDF files with GIMP and `convert`”

    1. Good idea, thanks! πŸ™‚

      After having recompiled GCC to support GCJ, and then having built pdftk, I gave it a try:

      convert page1.png page1.pdf
      convert page2.png page2.pdf
      pdftk page1.pdf page2.pdf cat output document.pdf

      which could be generalised to

      for page in page*.pdf; do convert ${page} ${page%???}png; done && pdftk page*.pdf cat output document.pdf

      for an arbitrary number of page*.png files.

      BTW, I don’t agree, that my proposed solution is necessarily more complicated than your proposal – it’s probably a matter of taste which tools to use, and what exactly you are doing. For example I usually have the scanned images open in GIMP anyway, because I use it to optimise the colour curves. Then again, this could also be done with `convert`.

    2. No way, his original idea was way easy. Took like 30 seconds. Really nothing to it. Took more time switching the order of the pages than anything else.

      BTW, Thanks a million Patrick! This really saved my butt! πŸ˜€ πŸ˜€ πŸ˜€

  1. Windows 7, GIMP
    GraphicsMagick 1.3.11 Q16

    Installed GhostScript first from link inside GraphicsMagick setup.

    However, GIMP returned a warning on export saying that the mng save executable had crashed.

    “Plug-in crashed: “file-mng.exe”
    (C:\Program Files\GIMP-2.0\lib\gimp\2.0\plug-ins\file-mng.exe)
    The dying plug-in may have messed up GIMP’s internal state. You may want to save your images and restart GIMP to be on the safe side.”
    and
    “Saving ‘C:\Users\Oriana\Documents\ChemExam2.mng’ failed:
    Procedure ‘file-mng-save’ returned no return values”

    A file was generated, so I used GraphicsMagick on that, but it returned “Invalid Parameter – document.pdf”. Replacing document with the name of my document, of course. I was expecting that since the .mng was 0 bytes. >.< why does windows hang on to 0 byte files??

    Anyway, if you have any advice I'd welcome it, but I thought I'd post just fyi and for anybody who visits looking for windoze stuff.

      1. Wow, that’s so simple! And it comes out with a smaller file size than the MNG way too, but looks just as good.

      1. Wow, that’s so simple! And it comes out with a smaller file size than the MNG way too, but looks just as good.

        (I meant to put my other duplicate comment here… Don’t know how it ended there)

    1. I am confused, if you open it as multiple layers, this wouldn’t work, would it? You have to have it as multiple images/files?

      If you want to do it as multiple layers, am I correct that you have to do .mng?

      1. Two year old thread – but added for information:

        convert in.pdf docEdit.png

        # docEdit-1.png is created
        # docEdit-2.png is created etc… add signature image

        convert docEdit*.png out.pdf

  2. Another method is to use PDFCreator. This installs as a printer. You can print multiple documents from different programs and combine them into one .pdf.

    Steven

  3. “convert page*.png out.pdf” appears to work only in some later versions of convert. It doesn’t work on my linux 6.2.8 (which is the latest in the default repo for Centos) but does work with 6.7.3-6 on Windows.

  4. This technique is really useful when one has a PDF received for example as a scan which contains multiple pages.

    GIMP imports that fine but treats them as layers.

    When you apply your chop and the date to the last page and “merge them down”, this method works super-well – save the whole mess as a .mng and use convert.

  5. You don’t need Step #2 (to reorder the layers before saving as MNG). After saving the animation, this works:

    convert document.mng -reverse document.pdf

    You can also save as an animated GIF (NOT a normal GIF) if you prefer. Then the command is:

    convert document.gif -reverse document.pdf

  6. I tried to convert them in a console but i get this:

    neron@ubuntu:~$ convert Job Application Form 2010.pdf-pages1.mng page1.pdf
    convert: unable to open image `Job’: @ error/blob.c/OpenBlob/2489.
    convert: unable to open image `Application’: @ error/blob.c/OpenBlob/2489.
    convert: unable to open image `Form’: @ error/blob.c/OpenBlob/2489.
    convert: unable to open image `2010.pdf-pages1.mng’: οΏ½HE @ error/blob.c/OpenBlob/2489.
    convert: unable to open image `2010.pdf-pages1.mng’: @ error/blob.c/OpenBlob/2489.
    convert: missing an image filename `page1.pdf’ @ error/convert.c/ConvertImageCommand/2940.

    Any suggestions?

    1. Hi, you need to escape spaces (and other “special” characters) on the command line. There are two ways to do it:

      1) Write ‘convert Job’ and then press the TAB key, it will automatically complete the filename and do the escaping (i.e. putting a \ in front of every space) for you
      2) Put the filename in quotes

  7. Thanks for the info. Playing around with this I figured out how to do this solely in GIMP.

    1) Scan the first page. Don’t worry about cropping any additional space at the bottom of the page (if you are scanning a letter size doc on a legal size scanner).
    2) Go to Image/Canvas Size and uncheck the chain link. Then expand the height of the canvas to large enough to hold all of the pages you want to. If you don’t want to calculate this you can set it for a number big enough and crop it in the last step.
    3) Scan the next page. When the new image comes up selecte Edit/Copy.
    4) Go to the original page and select Edit/Paste as/New layer.
    5) Use the drag tool to drag the new layer to just below the first page. Leave it over any extra scanned area (legal vs letter) on the page prior so it covers it up.
    6) Repeat steps 3-5 for additional pages.
    7) Use the rectangle selection tool to select the area you want to save (leaving unused space at bottom of canvas out), and use Image/Crop to selection.
    8) Print the image and select the option to print to file (PDF). Alternately you could save the image as a PNG and then use the convert function referenced above to create a pdf.

    1. patricks way of doing is waaaay easier. I did exactly what you did, and it took like 10 minutes and didn’t come out right. the MNG method took literally 30 seconds, if that. It might seem a bit complicated, but it’s really a snap. In linux at least.

    2. Nice one mountain man.
      One thing for newbies you might need to flatten the image after increasing the canvas size. This allows you to start editing the newly created space.

  8. Thanks for posting this! The method can be applied to the problem of making a pdf out of pages selected from a longer pdf. You just open the original file as layers, but only select for import the pages you want, and take it from there.

  9. Can you please clarify the last step? “open a console window”… do you mean a command prompt window or the Graphics Magick program? Where do you find the console?

    1. Hi Joshua, yes, I mean a command prompt. Since GraphicsMagick and ImageMagick are both command line tools, a command prompt / terminal window / console is the only way you can interact with them.

  10. To get this to work in Windows, make sure you navigate to the directory your file is in (should be obvious, but you never know) and you’ll actually need to use the command: gm convert document.mng document.pdf

    Thanks for the tip! Worked like a charm on windows with GraphicsMagick once I tweaked it a bit.

    1. Thanks a lot for the command for windows, and thanks to Patrick for the original tutorial. Ultimately, very very easy even on Windows 7.

  11. Great tip!

    In Mac it’s not necessary install ImageMagick (however it’s possible by macports).

    GIMP 2.8 doesn’t support .mng files no more… use animated GIF instead.

    You can export gif to pdf in mac using the own OSX Previewer… Open your gif in previewer, then click File > Export and choose “PDF” in Format combobox. But you need to sort the pages/layers in reversed order in GIMP, because Previewer doesn’t have an option to reverse GIF images before export to PDF.

    ImageMagick is a better solution for batch conversion but if you hate the Terminal (that I love – shell script >>>>>>> Apple Scripts >>>>>>>>>>>>>>>>> windows .bat and .vbs – blergh!!), Previewer solution is good too.

    1. But btw, you can re-order the gif images in Previewer. Just go to the preview sidebar on the right and drag the images to the order you want!

  12. Hi there,

    I am wondering if it is at all possible to create a PDF file that uses the layers in Gimp to make a layered PDF?

    For example, layer 1 would be the base PDF file, layer 2 would be the first layer, layer 3 the second layer etc.

    I am not even sure if PDF supports “layers” that I want (togglable) but apparently Adobe’s software does do layered PDF from what I’ve read.

  13. OK. I give. Where does one find the program “convert”, along with any information on it.

    I’m on a Mac OSX 10.6.8, trying “convert” at the command line just gives “command not found”, so I don’t think it comes installed. Web searches on “convert” “file conversion”, etc, just turn up very specific stuff, and nothing named “convert”. I’m not a member of any type of secret file conversion, or image conversion society, so I guess I need a pointer from those who are.

    Thanks in advance.

  14. Thank you for saving my afternoon.

    Convert can be applied directly to xcf (gimp) images.
    But in my case, the resulting multi page PDF file
    would look odd: Only outlines, partly green
    (orginally: all black printed letters as bitmap, greyscale).

  15. Thanks all. I use GIMP 2.8 and Windows 8.1 pro. This post and comments helped. I saved did the first few steps and saved in GIMP as animated gif without looping. Then I opened Nitro pro9 (free edition) and converted the gif to pdf in one easy step that took 1 second.

  16. Creative ideas ! Apropos , if anyone are searching for a service to merge some PDF files , my boss saw a service here https://goo.gl/8vM3hc.

  17. The original solution by Patrick is efficient. Reverse the order of layers in a snap using “Layers > Stack > Invert layers” (in french : “Calque > Pile > Inverser l’ordre des calques”)

  18. Thanks a lot for this valuable info. just try online “unifypdf.com” It’s a free and beautiful tool to combine your PDF files exactly the way you want it, No file limit, no ad watermarks.

  19. If you have the pages as layers, you can just export as pdf from gimp and select “layers as pages”. I don’t know if this is a recent feature.

Leave a Reply to ed Cancel reply

Your email address will not be published. Required fields are marked *