How To Modify PDF Files

On to extract/edit/merge pages from one or several PDF files

🕒 1 min read

Category: Linux

Tags: pdf, linux

So my advice is to go with gs.

Other commands exist such as pdfseparate and pdfunite. They are very good but the output files are quite heavy, compared to those obtained using gs.

Add a password to a PDF file

pdftk input.pdf output output.pdf userpw <password here>

Extracting pages

# Extracts all pages from 1 to 5
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage=1 -dLastPage=5 -sOutputFile=output.pdf input.pdf

# Extracts all pages from 1 to 2 and 4 to the end
pdftk input.pdf cat 1-2 4-end output output.pdf

# Extract only pages 1, 2, 4 and 5
pdftk input.pdf cat 1 2 4 5 output output.pdf

# Split each page into a file
pdftk input.pdf burst
# OR
pages=$(pdfinfo "$file" | grep "Pages" | awk '{print $2}') 
echo "Detect $pages in $file";
for i in $(seq -w 1 "$pages"); do
    pdftk "$file" cat "$i" output "$filename-$i.pdf";

Editing one page

Use Gimp (import with 300-dpi setting).

Merging several PDF files

gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sOutputFile=output.pdf input1.pdf input2.pdf input3.pdf
pdftk input1.pdf input2.pdf input3.pdf cat output output.pdf
pdfjam input1.pdf input2.pdf input3.pdf -o output.pdf