Search code examples
pdfpdftk

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


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!


Solution

  • 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!