Question

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!

Was it helpful?

Solution 3

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!

OTHER TIPS

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

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.

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.

If you have latex on your system, then the following script could be useful:

#!/bin/sh

mkdir /tmp/$$
cp $1 /tmp/$$
d=`pwd`
cd /tmp/$$
pdftk $1 burst
echo "\\documentclass[12pt]{article}" > blank.tex
echo "\\\\begin{document}" >> blank.tex
echo "\\mbox{}" >> blank.tex
echo "\\end{document}" >> blank.tex
pdflatex blank
for i in pg*.pdf; do echo -n " blank.pdf $i " >> list; done
pdftk `cat list` cat output withblanks.$1
cd $d
cp /tmp/$$/withblanks.$1 .

Pdfjam and exiftool can be used in a way that is similar to pdftk. You need to create your own blank.pdf for the following to work:

NUM=$(exiftool -T -PageCount input.pdf)
PDFJAM_ARG=""
for i in $(seq 1 ${NUM}); do
  PDFJAM_ARG="${PDFJAM_ARG} input.pdf $i blank.pdf 1 ";
done
pdfjam -o output.pdf --fitpaper true ${PDFJAM_ARG}

in windows you can use a java executable like

Multivalent (latest free version with tools included from following links, current hosted on sourceforge has no tools in itself; they were removed)

java -cp \path...to\Multivalent.jar tool.pdf.Impose -verbose -dim 1x1 -layout "1,b" file.pdf

Multivalent adds a -up suffix to original filename

this will add a blank page after any page of pdf BUT... also after last page!!!

since this is what you don't want, you need to perform some other tasks after invoking Multivalent

this is the GENERAL PATTERN that you can use to automatize the whole process putting these further tasks in a batch file

1. revert pages of pdf (last page becomes the first)

pdftk *-up.pdf cat end-1 output reverted.pdf

2. cut the last blank page that you don't desire in ouput file (that now is the FIRST page of your REVERTED pdf)

pdftk reverted.pdf cat 2-end output reverted2.pdf

3. revert again the order of pages into pdf to get the original (1,2,3 and so on...) page order

pdftk reverted2.pdf  cat end-1 output originalfilename-up.pdf

Here a solution with a batch script to get the number of page of the pdf, create a blank.pdf via a command and ImageMagick and bind them all automatically.

For batch file under windows.

@echo off
SetLocal EnableDelayedExpansion

:: get the number of pages
    :: extract info with pdftk
    pdftk output.pdf dump_data | grep NumberOfPages > temp
    set /p pages=<temp
    :: get only the real number
    set pages2=!pages:NumberOfPages: =!
    del temp
    
:: Prepare the string sequence to merge 
FOR /L %%p IN (1, 1, !pages2!) DO (
    if %%p==1 (
        set list=A1 B1
    ) else (
        set list=!list! A%%p B1 
    )
)

:: create a blank pdf
magick convert xc:none -page A4 blank.pdf

pdftk A=output.pdf B=blank.pdf cat %list% output combined.pdf

del blank.pdf
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top