Search code examples
linuxbashpdftkfilelist

Output list of pdf files as one pdf using pdftk bash script


I have a bash script containing a list of PDF files. The printer output is tailored by commenting/ uncommenting file requirement (see example script below). I wish to add to this so I can choose to print all selected files to a single pdf using pdftk (or the paper printer). I am familiar with pdftk though not bash.

Can anyone indicate the bash code to output as one PDF ?

Thank you

!/bin/bash

# Print document pack

# Instructions
#   ls > print.txt                  Generate the file list into a file 
#   -o sides=two-sided-long-edge -#1        Prints two sided long edge, one copy
#   -o sides=two-sided-long-edge -#2        Prints two sided long edge, two copies
#   -o page-ranges=1 -#1                Print first page only, one copy
#   -o page-ranges=1-2,5-7 -#1          Print pages 1-2 & 5-7 only, one copy

#   -lt x                       sets number of packs to be printed
#   while [  $COUNTER -lt  ***** 1 ****** ]; do     NUMBER INSIDE STARS INDICATES NUMBER PACKS TO PRINT

COUNTER=0

while [  $COUNTER -lt 1 ]; do

    lpr "doc01.pdf"

    lpr "doc02.pdf"

    lpr -o sides=two-sided-long-edge -#1 "doc03.pdf"

#   lpr "doc04.pdf"

#   lpr "doc05.pdf"

    lpr -o sides=one-sided-long-edge -o page-ranges=1 -#1 "doc06.pdf"

#   lpr "doc07.pdf"

    lpr -o sides=two-sided-long-edge -#2 "doc08.pdf"

    lpr "doc09.pdf"

    lpr "doc10.pdf"

let COUNTER=COUNTER+1 
done

Solution

  • Something like this:

    #!/bin/bash
    
    files=()
    
    add() {
      files+=("'""$1""'")
    }
    
    add "file1.pdf"
    
    #add "file2.pdf"
    
    add "file3.pdf"
    
    add "file with     spaces.pdf"
    
    echo "${files[*]}"
    

    Naturally, substitute the proper pdftk command for echo.


    Edit 2

    This new "version" will work better with filenames containing spaces.


    Edit 3

    To hand the files over to the command, it seems something like the following will do the trick:

    bash -c "stat $(echo "${files[*]}")"