28

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?

Anko
  • 4,526
ixtmixilix
  • 13,230

4 Answers4

29

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?

  • 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. – maxschlepzig Jul 03 '11 at 15:22
  • 9
    I found today the following command to create a blank page using the command line: echo "" | ps2pdf -sPAPERSIZE=a4 - pageblanche.pdf – remjg May 31 '14 at 17:42
  • 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. – bwright Nov 13 '18 at 16:01
5

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
0

If using pdfjam is an option:

pdfjam --outfile output.pdf input.pdf "-20,{},21-"

In pdfjam, {} inside range specifications is interpreted as a blank page, so this command inserts a blank page between pages 20 and 21.

quazgar
  • 881
0

Here is the steps I used to have blank page after every page of my Pdf file with Pdftk in Ubuntu

1-Install pdftk if you haven't already

sudo apt-get install pdftk

2-Create a blank PDF file named blank.pdf. You can use the convert command from the ImageMagick package to create a blank image and then convert it to PDF. Run the following commands:

sudo apt install imagemagick
convert -size 595x842 xc:white blank.jpg
convert blank.jpg blank.pdf

if you got security policy error, open the policy.xml file using a text editor with root privileges. For example, you can use the following command to open it with the nano editor:

sudo nano /etc/ImageMagick-6/policy.xml

Change the value of the rights attribute from "none" to "read|write" for PDF. The modified line should look like this:

<policy domain="coder" rights="read|write" pattern="PDF" />

3- Now navigate to the directory where your PDF file is located then use the following commands

pdftk input.pdf burst output burst_output%d.pdf
pdftk $(for f in burst_output*.pdf; do echo -n "$f blank.pdf "; done) cat output final.pdf

put the name of your input file instead of "input".

Now your final.pdf is ready! You can delete the splitted pages after this.

HohO
  • 1
  • 2