Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge pdf files with numerical sort

I am trying to write a bash script to merge all pdf files of a directory into one single pdf file. The command pdfunite *.pdf output.pdf successfully achieves this but it merges the input documents in a regular order:

1.pdf
10.pdf
11.pdf
2.pdf
3.pdf
4.pdf
5.pdf
6.pdf
7.pdf
8.pdf
9.pdf

while I'd like the documents to be merged in a numerical order:

1.pdf
2.pdf
3.pdf
4.pdf
5.pdf
6.pdf
7.pdf
8.pdf
9.pdf
10.pdf
11.pdf

I guess a command mixing ls -v or sort -n and pdfunite would do the trick but I don't know how to combine them. Any idea on how I could merge pdf files with a numerical sort?

like image 765
max Avatar asked May 13 '14 23:05

max


People also ask

How do I combine PDF files in a specific order?

You can merge PDFs or a mix of PDF documents and other files. Arrange and delete content: Click, drag, and drop to reorder files or press "Delete" to remove any content you don't want. Combine files: When you're finished arranging, click "Combine Files". Save as a PDF file: Name your file and click the "Save" button.

Is there a free PDF combiner?

The Acrobat Merge PDFs tool lets you create a merged PDF of up to 1,500 pages. You can combine up to 100 files, with each individual file limited to 500 pages.

How do I combine PDF fillers?

First, upload or create multiple PDF documents that you intend to merge. Then, select files to be combined, click the More button in the top panel, and select Merge. Before you click the Merge button, set the document order before you merge PDF files. To do so, simply drag and drop documents in your preferred order.


1 Answers

you can embed the result of command using $(), so you can do following

$ pdfunite $(ls -v *.pdf) output.pdf

or

$ pdfunite $(ls *.pdf | sort -n) output.pdf

However, note that this does not work when filename contains special character such as whitespace.

In the case you can do the following:

ls -v *.txt | bash -c 'IFS=$'"'"'\n'"'"' read -d "" -ra x;pdfunite "${x[@]}" output.pdf'

Although it seems a little bit complicated, its just combination of

  • Bash: Read tab-separated file line into array
  • build argument lists containing whitespace
  • How to escape single-quotes within single-quoted strings?

Note that you cannot use xargs since pdfunite requires input pdf's as the middle of arguments. I avoided using readarray since it is not supported in older bash version, but you can use it instead of IFS=.. read -ra .. if you have newer bash.

like image 195
ymonad Avatar answered Oct 18 '22 10:10

ymonad