I have around 500+ files and I know how to merge them. But the problem is I would like to merge them in such a way that every file starts with the file neme in its first page. For example I have 3 files (fila1,file2,file3) and each file has 10,20 and 30 pages. I need the merged file in such a way first page would be FILENAME that is file1 then the complete first file , then name of second file in one page followed by the file and so on.... How could I do so?

  • It sounds like you need to find a way to take a string of text and turn it into a new, one-page pdf - then merge all of those with all the original files, in the correct order. What creates the original pdfs, and what command do you plan to use to merge them? – JigglyNaga Jun 23 '16 at 14:17

There are many ways to generate a banner page pdf. Here's one using groff:

declare -a files del
for file in file1.pdf file2.pdf file3.pdf
do  echo -e '.ft H\n.ps 30\n.vs 35\n.sp 10c\n.ce\n'"$file" |
    groff -Tps | 
    ps2pdf - - >/tmp/title$i.pdf
    files+=(/tmp/title$i.pdf "$file")
    let i=i+1
pdfconcat -o /tmp/out.pdf "${files[@]}"
rm ${del[@]}

It creates a tmp title pdf for each file and collects the filenames in bash array files and the names to delete in array del. The groff input is standard troff commands to set the font to Helvetica, the point size to 30 points, the line spacing slightly bigger, and to space 10 centimetres down the page for the text which is centered.

| improve this answer | |
  • I tried as you mentioned and receive the following error message – TCFP HCDG Jun 24 '16 at 9:48
  • declare -a files del; i= 1; for file in *.pdf; do echo -e '.ft H\n.ps 30\n.vs 35\n.sp 10c\n.ce\n'" $file" |groff -Tps |ps2pdf - - >/tmp/title$i.pdf ; files+=(/tmp/title$i.pdf "$file") ; del+=(/tmp/title$i.pdf) ; let i=i+1; done; pdfconcat -o /tmp/out.pdf "${files[@]}"; rm ${del[@]} 1: command not found pdfconcat: open /tmp/title.pdf: No such file or directory rm: cannot remove '/tmp/title.pdf': No such file or directory rm: cannot remove '/tmp/title1.pdf': No such file or directory rm: cannot remove '/tmp/title2.pdf': No such file or directory – TCFP HCDG Jun 24 '16 at 9:48
  • Could you please have a look what I did wrong? – TCFP HCDG Jun 24 '16 at 9:52
  • The reason for the error 1: command not found is because you left a space when setting i. It should be i=1. I dont know why you have the other errors. Make sure you are using bash. Add set -x to the start to see what each command is doing, and perhaps it will become clear what the problem is. Add any further debug output to your original question, where it will be easier to read, and just mention it in a comment to my answer. – meuh Jun 24 '16 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.