How do I insert a blank page into a PDF with ghostscript or pdftk?

22,645

Solution 1

From http://blog.chewearn.com/2008/12/18/rearrange-pdf-pages-with-pdftk/

pdftk A=src.pdf B=blank.pdf cat A1 B1 A2-end output res.pdf

Hope you like this script, just save it as pdfInsertBlankPageAt.sh, add execute permissions, and run.

./pdfInsertBlankPageAt 5 src.pdf res.pdf

#!/bin/bash
if [ $# -ne 3 ]
then
  echo "Usage example: ./pdfInsertBlankPageAt 5 src.pdf res.pdf"
  exit $E_BADARGS
else
  pdftk A=$2 B=blank.pdf cat A1-$(($1-1)) B1 A$1-end output $3
fi 

cat A1 B1 A2-end means that the output file will contain the first page of document A (src.pdf) followed by the first page of document B (blank.pdf) followed by the rest (pages 2 to end) of document B. This operation is called concatenation, Linux cat is very often used to display text, but it is interesting when used with more than one argument.

To create blank.pdf, see How do I create a blank PDF from the command line?

Solution 2

For anyone just looking to add a single blank page to the end of a PDF, I used the already linked question How do I create a blank PDF from the command line? to create a blank.pdf file and merge it with my existing pdf using pdfunite:

pdfunite input.pdf blank.pdf output.pdf
Share:
22,645

Related videos on Youtube

Matthew
Author by

Matthew

Updated on September 18, 2022

Comments

  • Matthew
    Matthew over 1 year

    I have a PDF file that needs a blank page inserted into it every so often. The pattern is unpredictable, so I need a command that will allow me to fit one in wherever necessary.

    How can i do this?

  • maxschlepzig
    maxschlepzig almost 13 years
    I suggest adding a small explanation what the command does, what effect it has (e.g. where the blank page is inserted). Also you could add a convenient way how to create a pdf file with only a blank page in it.
  • remjg
    remjg almost 10 years
    I found today the following command to create a blank page using the command line: echo "" | ps2pdf -sPAPERSIZE=a4 - pageblanche.pdf
  • bwright
    bwright over 5 years
    When trying this to insert a blank page a the beginning of the document (Page 1) i get the following error: Error: Unexpected range end; expected a page number or legal keyword, here: A1 Exiting. Errors encountered. No output created. Done. Input errors, so no output created.