Skip to content
Advertisement

Script to merge all image files from a folder inside a pdf

I frequently have to merge a lot of images inside a pdf. Naturally, I’d like to automate this a bit.

Here are the things I need:

  • lossless merge into a pdf
  • merge of most common image formats
  • natural sorting of numbers (1.jpg, 2.jpg, and 10.jpg would be merged in this order, not as 1.jpg 10.jpg 2.jpg)
  • can deal with white-spaces in file names

I’ve come across a solution that works exactly like I’d need it to, but which fails for files containing a white-space in their name.

JavaScript

Advertisement

Answer

Trying to parse ls is wrong, as you’ve found out. Assuming you’re using GNU versions of utilities as the linux tag usually implies, try something like:

JavaScript

which uses the common trick of separating filenames with nul characters in order to safely handle whitespace when populating a bash array with sorted filenames.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement