Skip to content

Instantly share code, notes, and snippets.

@emleddin
Created December 3, 2022 03:32
Show Gist options
  • Save emleddin/ba63fdf839b9cf0da13e14efd4e9b0ec to your computer and use it in GitHub Desktop.
Save emleddin/ba63fdf839b9cf0da13e14efd4e9b0ec to your computer and use it in GitHub Desktop.
Tips for modifying PDFs and saving images.

Modifying PDFs

PDFtk has a command-line tool for working with PDFs. It's fantastic for saving out single pages or combining files.

Ghostscript also has good features for PDFs, including being able to reduce the file size while preserving links and bookmarks.

Combining PDFs

# Combine several PDFs into 1
pdftk first.pdf second.pdf third.pdf fourth.pdf cat output combined.pdf

Saving All Pages Individually

The burst option will save pages individually. Use %d to include the page number.

# Save as burst
pdftk full.pdf burst output about-%d.pdf

Saving a Specific Page

# Save a single page
pdftk infile.pdf cat 7 output page7.pdf

Saving a Page as an Image

The following script will save 1 page and convert it to a PNG. It requires the convert command from ImageMagick.

# Set variables
in=infile.pdf
out=outfile
page=123

# Select out PDF page
pdftk ${in} cat ${page} output ${out}.pdf

# Create a high-res PNG with correct coloration on white background
# Remove '-flatten' for transparent image
convert -density 600 -colorspace sRGB -flatten \
${out}.pdf ${out}.png

Reduce File Size

This single ghostscript command brought my dissertation from 339.3 MB to 76.2 MB without sacrificing quality.

# Reduce PDF size
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH \
-sOutputFile=outfile.pdf infile.pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment