Ubuntu – How to merge a pdf file as a first page into many pdf files

pdfpdftk

I have a creative commons license in one .pdf file, say license.pdf. I want to merge this license.pdf as a first page in another .pdf file. I can do it manually by the following command:

pdftk license.pdf t2.pdf output t2merged.pdf

But, I am having more than 4000 .pdf files in a folder and want to add the license.pdf to all of the 4000 .pdfs as a first page. How can I automate this process?


I tried. the following outputs came in the terminal.

info-farmer-lm17mate@64bit-Lenovo-Flex-2-14 ~/Desktop/LICENSEs $ sudo chmod u+x add-license.sh
[sudo] password for info-farmer-lm17mate: 
info-farmer-lm17mate@64bit-Lenovo-Flex-2-14 ~/Desktop/LICENSEs $ ./add-license.sh
Error: Unable to find file.
Error: Failed to open PDF file: 
   edits/files/2mergedpdffile.pdf
Done.  Input errors, so no output created.
Error: Unable to find file.
Error: Failed to open PDF file: 
   edits/files/A_Vocabulary_of_English_and_Tamil_Words-google.pdf
Done.  Input errors, so no output created.

@0x450 still facing the same error as above

Best Answer

Let's assume that all of your "4000" .pdf files are located in a single directory (for example files ) and the license.pdf is directly outside this folder. Then you could easily iterate through each file and concatenate it with your license. This script should do the trick:

#!/usr/bin/env bash
for current in files/*
do
    pdftk license.pdf "$current" output "edits/$current"
done

At the end all modified files will be contained in the folder edits, which is at the same level as files.

Make the script executable with chmod u+x name-of-the-script and run it with ./name-of-the-script and don't forget to place it in the same directory where the license.pdf file is stored.

Related Question