Search code examples
qpdf

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


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?


Solution

  • 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