Skip to content

Instantly share code, notes, and snippets.

@pradyumnac
Last active January 26, 2022 12:08
Show Gist options
  • Save pradyumnac/6d1a9c7d732ff366f2af48a2424ed47e to your computer and use it in GitHub Desktop.
Save pradyumnac/6d1a9c7d732ff366f2af48a2424ed47e to your computer and use it in GitHub Desktop.
Combines all pdf files on the current folder to a single pdf (ghostscript)

Combining pdfs usingghostscript

Combines all pdf extension files to single pdf. optimises pdf size.

uses ghostscript

#!/usr/bin/env bash
#@ Get pdf filelist
ls|grep \.pdf > filelist.pdf
## section to anipulate individual pdfs
## In this case last line is removed
filelist=""
mkdir -p temp
while IFS="" read -r p || [ -n "$p" ]
do
pdftk $p cat 1-r2 output temp/$p
filelist="$filelist temp/$pf"
done < filelist.txt
## Merge PDF
# cd temp ##Move to target folder where the manipulated pdfs are there
filelist=`ls |grep \.pdf`
mkdir -p output
gs -sDEVICE=pdfwrite \
-dNOPAUSE -dBATCH -dSAFER \
-sOutputFile=../output/full.pdf \
$filelist
# cd ..
#!/usr/bin/env bash
#@ Get pdf filelist
# ls|grep \.pdf > filelist.pdf
## section to anipulate individual pdfs
## In this case last line is removed
# filelist=""
# mkdir -p temp
# while IFS="" read -r p || [ -n "$p" ]
# do
# pdftk $p cat 1-r2 output temp/$p
# filelist="$filelist temp/$pf"
# done < filelist.txt
## Merge PDF
cd temp
filelist=`ls |grep \.pdf`
mkdir -p output
gs -sDEVICE=pdfwrite \
-dNOPAUSE -dBATCH -dSAFER \
-sOutputFile=../output/full.pdf \
$filelist
cd ..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment