Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

pdf

pdftk

I've got a PDF and I want a fast way to insert a blank page every second page (except at the end). E.g. my PDF has the pages

1: A
2: B
3: C
4: D

it should look like:

1: A
2: empty
3: B
4: empty
5: C
6: empty
7: D

Is there any easy scripting way to do so? I thought of using pdftk but I don't know exactly if it's the easiest way to do... I'm running Windows 7.

Thanks so far!

like image 425
tim Avatar asked Oct 17 '12 20:10

tim


4 Answers

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.

like image 81
user1828796 Avatar answered Oct 23 '22 17:10

user1828796


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
like image 44
ucyo Avatar answered Oct 23 '22 16:10

ucyo


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!

like image 1
tim Avatar answered Oct 23 '22 16:10

tim


i'm just using pdftk, but i guess u can use the shuffle option. if you have notblank.pdf with n pages (n is a little big), create a file blank.pdf with 1 blank page (size could be controlled with PhotoShop or PowerPoint), then the batch file (say n=10)

@echo off
setlocal enabledelayedexpansion
set "str="
for /l %%n in (1,1,10) do (set "str=!str! A" )
pdftk A=blank.pdf cat %str% output blank10.pdf
pdftk A=notblank.pdf B=blank10.pdf shuffle A B output blanknot.pdf

basically does the job. it first use the 1-page blank.pdf to create a 10-page blank10.pdf, then shuffle with the original notblank.pdf

p.s. i found that using the multistamp command gives rise to a simpler solution. say we now have the original n-page notblank.pdf and a 1-page blank.pdf (make sure that the background is indeed WHITE instead of transparent), then the following commands will suffice

pdftk notblank.pdf multistamp blank.pdf output stamped.pdf
pdftk A=notblank.pdf B=stamped.pdf shuffle A B output zebra.pdf

there's also a blank page at the end of the output file zebra.pdf, which is easy to get rid of by

pdftk A=zebra.pdf cat A1-r2 output zebra1.pdf

then the last blank page is removed. the output file is roughly twice larger in size, though.

i'm new to pdftk and this is my first post. pls correct me if i'm doing anything stupid.

like image 1
davyjones Avatar answered Oct 23 '22 17:10

davyjones