Batch convert xcf to pdf

I try to convert an xcf file with four layers to a four page pdf.

On the command line. From a Makefile. Without firing up the gui.
Is that possible? I spend some time know googling and gemining around to no avail.

An ideal Makefile would look like this:

bla.pdf: bla.xcf
    gimp --batch $< $@

This will work:

gimp -nids ~/Documents/Untitled.xcf --batch-interpreter=python-fu-eval -b "Gimp.file_save(Gimp.RunMode.NONINTERACTIVE, Gimp.get_images()[0], Gio.File.new_for_path('/my/path/file.pdf'), None)" --quit

Note that I’d suggest to use gimp-console instead of gimp. Both would work the same, but gimp-console is made for CLI use cases and doesn’t link GTK or any GUI code to begin with.

2 Likes

P.S.: in GIMP build itself, we do a bunch of this, of course at the end of the build once GIMP has been successfully compiled. Then we use the just-compiled GIMP to generate a bunch of image (splash images, but also multiple images in various size for some packages, etc.).

Dog fooding and all that! :grin: (some say the expression is wrong because it implies bad quality: but if we had any dog, we’d feed it good and healthy food, just like GIMP itself is very good food! :face_with_tongue:)

2 Likes

Ok, thanks. It works. Nearly. :roll_eyes: I have four layers in my xcf and want to have four pages in the pdf. In the generated image there is only one page. Can Gimp do this, too?

I’m all for quality dog food! :joy:

Uh, I had not received any notification about your answer! I discover it by chance.

Sure. You can do nearly everything you can do in the interface through scripts.

For this, we need to use the specific PDB procedure generated by the PDF Export plug-in, because it will require using options specific to this plug-in (the gimp_file_save() function also uses specific PDB procedures under the hood, but just uses all arguments with defaults).

  1. First of all, you want to check the options in the PDB procedure of interest. Go to menu Help > Procedure Browser
  2. In the search field, start typing “pdf”
  3. The “file-pdf-export” procedure is what you want (there is a “file-pdf-export-multi” in the list, but it’s actually for using a mix from several source documents and I’m not sure it’s well tested at all; we should probably get rid of it; this can be done through scripting anyway!). Click on it.
  4. On the right, the PDB procedure API docs appears:
  5. In your case, what you need is setting the “layers-as-pages” argument to TRUE. So make a script file, which we’ll call gimp-xcf2pdf.py:
p = Gimp.get_pdb().lookup_procedure('file-pdf-export')
c = p.create_config()
c.set_property('image', Gimp.get_images()[0])
c.set_property('file', Gio.File.new_for_path('/path/to/output.pdf'))
c.set_property('layers-as-pages', True)
p.run(c)
  1. Finally call GIMP like this:
cat gimp-xcf2pdf.py | gimp -nids /path/to/bla.pdf --batch-interpreter=python-fu-eval -b - --quit

Note: you could put it all into a one-liner with newlines replaced by ;, but I usually find it more readable, maintainable (and reusable!) to put the code into a file and just pipe it to stdin (and when you start using loops and conditional code, etc. it’s even more necessary!).

Note 2: as you can see, there are other args. You could also set “reverse-order” if the page order is not the right. Or “root-layers-only” if you were using group layers, etc. Just look at the docs! :grin:

1 Like

Neat! I just replaced /path/to/bla.pdf to /path/to/input.xcf as I figured this would be the input xcf file.

Thanks very much!! :grinning_face:

It is. Sorry, I forgot the input was XCF, but that works anyway! :slight_smile: