Insert a blank page between each existing page in a PDF document

Had also this idea for reviewing paper. Here is the full script.

#!/bin/bash

if [ $# -ne 1 ]
then
  echo "Usage example: ./bashscript src.pdf"
  exit $E_BADARGS
else
  NUM=$(pdftk $1 dump_data | grep 'NumberOfPages' | awk '{split($0,a,": "); print a[2]}')
  COMMSTR=''

  for i in $(seq 1 $NUM);
  do
    COMMSTR="$COMMSTR A$i B1 " 
  done
  $(echo "" | ps2pdf -sPAPERSIZE=a4 - pageblanche.pdf)
  $(pdftk A=$1 B=pageblanche.pdf cat $COMMSTR output 'mod_'$1)
  (pdfnup 'mod_'$1 --nup 2x1 --landscape --outfile 'print_'$1)
  $(rm pageblanche.pdf && rm 'mod_'$1)

fi

#for f in *.pdf; do ./bashscript.sh $f; done 2> /dev/null

Okay I did it myself using PHP and FPDI/FPDF:

<?php
error_reporting(E_ALL);
require_once('fpdi/fpdf.php');
require_once('fpdi/fpdi.php');

// Format für die einzelnen Folien:
$format = 'L';  // Entweder '' (horizontal) oder 'L' (Landscape!)

// Verzeichnis lesen
foreach(glob('infiles/*.pdf') as $file)
{
    $filename = basename($file);
    $fileout = 'outfiles/' . $filename;

    // Ausgabe-PDF
    $out = new FPDI();

    // Vorhandenes PDF einlesen
    $pagecount = $out->setSourceFile($file);

    // Alle Seiten nacheinander importieren
    for($i = 1; $i <= $pagecount; $i++)
    {
        // Importiere Seite
        $tpl = $out->importPage($i); // , '/MediaBox'

        // Vorhandene Seite
        $out->addPage($format);
        $out->useTemplate($tpl);

        if($i < $pagecount)
        {
            // Leere Seite anfügen (nur nicht am Ende)
            $out->addPage($format);
        }
    }

    $out->Output($fileout);
}

all files in the subdirectory 'infiles' will get blank Pages inserted and saved to 'outfiles' with the same filename!


the only hard part about doing it with pdftk is typing in everything. For posterity (like if someone has a small number of pages and wants to do it this way) Here's how to do it with pdftk (using 3 pages, as an example).

  1. install pdftk http://www.pdflabs.com/docs/install-pdftk/

do this:

pdftk A=notblank.pdf B=blank.pdf cat A1-1 B1-1 A2-2 B1-1 A3-3 output combined.pdf

If you wanted to have a blank page at the end of every 3 pages it would be like this:

pdftk A=notblank.pdf B=blank.pdf cat A1-3 B1-1 A4-6 B1-1 A7-9 output combined.pdf

If you happened to want a blank page at the end just add another B1-1. Also, you need a blank PDF to work with, and of course this works with non blank pages, and you can mess around with the numbers and use more than 2 pdfs.

Tags:

Pdf

Pdftk