PDFlib Cookbook

cookbook

pdf_import/import_repeated_contents

In order to keep the output file size small, handles to imported PDF pages or images should be reused for repeated contents.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/**
 *
 * 
 * Repeated contents: In order to keep the output file size as small as possible
 * handles to imported PDF pages or images can and should be reused for repeated
 * contents.
 * 
 * Required software: PDFlib+PDI/PPS 9
 * Required data: PDF document, image file
 */
/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3) . "/input";
$title = "Repeated Contents";

$pagecount = 3;

$p = null;
$imagefile1 = "cambodia_preah_khan1.jpg";
$imagefile2 = "cambodia_preah_khan2.jpg";
$imagefile3 = "cambodia_preah_khan3.jpg";
$background = "card.pdf";

try {
    $p = new pdflib();
    
    
    $p->set_option("searchpath={" . $searchpath . "}");
    
    /* This means we must check return values of load_font() etc. */
    $p->set_option("errorpolicy=return");
    $p->set_option("stringformat=utf8");
    
    if ($p->begin_document("", "") == - 1)
        throw new Exception("Error: " . $p->get_errmsg());
    
    $p->set_info("Creator", "PDFlib Cookbook");
    $p->set_info("Title", $title);
    
    /* Load the font */
    $font = $p->load_font("Helvetica", "unicode", "");
    if ($font == - 1)
        throw new Exception("Error: " . $p->get_errmsg());
        
        /* Load the three images */
    $image1 = $p->load_image("auto", $imagefile1, "");
    if ($image1 == - 1)
        throw new Exception("Error loading image " . $imagefile1 . ": " .
                                         $p->get_errmsg());
    
    $image2 = $p->load_image("auto", $imagefile2, "");
    if ($image2 == - 1)
        throw new Exception("Error loading image " . imagefile2 . ": " .
                                         $p->get_errmsg());
    
    $image3 = $p->load_image("auto", $imagefile3, "");
    if ($image3 == - 1)
        throw new Exception("Error loading image " . $imagefile3 . ": " .
                                         $p->get_errmsg());
        
        /* Open the card file */
    $card = $p->open_pdi_document($background, "");
    if ($card == - 1)
        throw new Exception("Error: " . $p->get_errmsg());
        
        /* Load the first page of the card file */
    $cardpage = $p->open_pdi_page($card, 1, "");
    if ($cardpage == - 1)
        throw new Exception("Error: " . $p->get_errmsg());
        
        /* Create textflow for explanation */
    $explanation = "Reuse PDI page, image and textflow handles across " .
                     "multiple pages";
    $optlist = "alignment=left font=" . $font . " fontsize=20";
    $tf = $p->create_textflow($explanation, $optlist);
    if ($tf == 0) {
        throw new Exception("Error: " . $p->get_errmsg());
    }
    
    /* Query the width and height of the first page of the card */
    $cardwidth = $p->pcos_get_number($card, "pages[0]/width");
    $cardheight = $p->pcos_get_number($card, "pages[0]/height");
    
    /* Offset factor from page border for content */
    $offset = 0.15;
    
    /*
     * Now reuse the PDI page handle, the image handles and the textflow handle
     * on all the pages.
     */
    for($page = 1; $page <= $pagecount; $page += 1) {
        $p->begin_page_ext($cardwidth, $cardheight, "");
        
        /* Place the card page as background */
        $p->fit_pdi_page($cardpage, 0, 0, "");
        
        /* Place images */
        $p->fit_image($image1, $cardwidth * $offset, $cardheight * $offset, "");
        $p->fit_image($image2, $cardwidth * (1 - $offset), 
                        $cardheight * $offset, "position={right bottom}");
        $p->fit_image($image3, $cardwidth * (1 - $offset), 
                        $cardheight * (1 - $offset), "position={right top}");
        
        /* Place textflow, must be rewound each time */
        $p->fit_textflow($tf, $cardwidth * $offset, $cardheight * 0.5, 
                        $cardwidth * 0.5, $cardheight * (1 - $offset), 
                        "rewind=1");
        
        /* Create a page number at the bottom center of the page */
        $p->fit_textline("- " . $page . " -", $cardwidth * 0.5, 
                        $cardheight * $offset / 2, 
                        "font=" . $font . " fontsize=12 position=center");
        
        $p->end_page_ext("");
    }
    
    /* Clean up all handles */
    $p->close_image($image1);
    $p->close_image($image2);
    $p->close_image($image3);
    $p->close_pdi_page($cardpage);
    $p->close_pdi_document($card);
    $p->delete_textflow($tf);
    
    $p->end_document("");
    
    $buf = $p->get_buffer();
    $len = strlen($buf);
    
    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=import_repeated_contents.pdf");
    print $buf;
}
catch (PDFlibException $e) {
    echo("PDFlib exception occurred:\n" . "[" .
         $e->get_errnum() . "] " . $e->get_apiname() .
         ": " . $e->get_errmsg() . "\n");
    exit(1);
}
catch (Throwable $e) {
    echo($e);
    exit(1);
}

$p = 0;
?>