1

img2pdf works very quickly with hundreds of images and creates a pdf out of them with a command like

img2pdf *.tif -o out.pdf

but the page order is wrong in my case. I ran the command in konsole under Kubuntu 20.04.

The image files are named (renamed in Dolphin file manager) in the form

Vol_1.tif
Vol_2.tif
Vol_3.tif
...
Vol_430.tif

The resulting pdf starts with the file/page called Vol_100.tif which makes some sense (100 is seen before 1 or 11). Then, the one called Vol_119.tif is followed by Vol_11.tif, Vol_129.tif is followed by Vol_12.tif ... ...Vol_189.tif is followed by Vol_18.tif.

How to proceed?

0

2 Answers 2

5

Your best option is to rename the files so that they all have the same number of zero-padded digits, using the perl rename utility (this has different names on different distros, including perl-rename, prename, file-rename). For example:

rename -n 's/^(Vol_)(\d+)/sprintf "%s%03i", $1, $2/e' Vol_*.tif

Change %03i to %04i or %05i if three digit zero-padding is not enough.

This uses the -n option, so will only show what would be renamed. If/when you're certain it does what you want, either remove the -n to silently rename the files (no output except on errors), or replace it with -v for verbose operation.

With -v, you'll see output like this:

$ rename -v 's/^(Vol_)(\d+)/sprintf "%s%03i", $1, $2/e' Vol_*.tif
Vol_1.tif renamed as Vol_001.tif
Vol_2.tif renamed as Vol_002.tif
Vol_50.tif renamed as Vol_050.tif
Vol_60.tif renamed as Vol_060.tif

Another option is to perform a natural sort on the filenames, so you'll need to use GNU find, sort, and xargs (or other versions with support for NUL separators):

e.g.

find . -name 'Vol_*.tif' -print0 | sort -z -V | xargs -0r img2pdf -o out.pdf

GNU sort's -V option is a "version" sort, which is their name for a natural sort.

1
  • Thunar file manager has a rename utility that can do that too.
    – cipricus
    Commented Aug 24, 2021 at 11:56
4

You can use the shell to expand the filenames in the correct order.

In bash, you can use brace expansion if you know the number range:

img2pdf Vol_{1..430}.tif -o out.pdf

Or using regular wildcards, just say you want single-digit numbers first:

img2pdf Vol_?.tif Vol_??.tif Vol_???.tif -o out.pdf

You must log in to answer this question.

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