Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I merge pdf files together and take only the first page from each file?

Tags:

qpdf

I am using qpdf to merge all pdf files in a directory and I would like to merge only the first page of multiple inputfiles. According to the qpdf documentation on page selection this should be possible. I have tried couple variants without luck:

qpdf --empty --pages *.pdf 1-1 -- "output.pdf"
qpdf --empty --pages *.pdf 1 -- "output.pdf"

What can I do?

like image 395
Human Avatar asked Sep 20 '25 09:09

Human


1 Answers

As explained in this qpdf issue, the shell expands *.pdf in the command qpdf --empty --pages *.pdf 1 -- "output.pdf", that means it replaces *.pdf with a list of pdf files in the current directory. Assuming you have the following pdf files in the current directory:

  • file1.pdf
  • file2.pdf
  • file3.pdf

the command becomes:

qpdf --empty --pages file1.pdf file2.pdf file3.pdf 1 -- "output.pdf"

so the page selector is only applied to the last pdf. On a Mac or Linux you can script the command to add a 1 after each pdf-filename, to take the first page of each pdf file and put it all together like so:

qpdf --empty --pages $(for i in *.pdf; do echo $i 1; done) -- output.pdf
like image 139
Human Avatar answered Sep 23 '25 20:09

Human



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!