PDFlib Cookbook

cookbook

pdfa/transparent_stamp_for_pdfa1

Apply a transparent stamp to an existing PDF/A-1 document while maintaining PDF/A-1 conformance.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 *
 * 
 * Transparent stamp for PDF/A-1:
 * Apply a transparent stamp to an existing PDF/A-1 document while maintaining
 * PDF/A-1 conformance.
 * 
 * Import all pages from an existing PDF/A-1 document and place a stamp on the
 * page. The stamp is filled with a pattern color, where the pattern consists of
 * a bitmap. The bitmap is used as a mask to create a certain percentage of
 * transparency. This is required since real transparency is not allowed in
 * PDF/A-1. Transparency by pattern color is PDF/A-1 compatible, so we use it to
 * apply a transparent stamp on a PDF/A-1 document while maintaining PDF/A-1
 * conformance.
 * 
 * Note that this trick is only necessary for PDF/A-1, as true PDF
 * transparency is allowed beginning with PDF/A-2.
 * 
 * If the transparent stamp is not visible in Acrobat (or is visible only at
 * certain zoom levels) you can set the following in Acrobat:
 * Edit, Preferences, Page Display, enable "Smooth images".
 * 
 * Required software: PDFlib+PDI/PPS 10
 * Required data: PDF document
*/

/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";

/* By default annotations are also imported. In some cases this
 * requires the Noto fonts for creating annotation appearance streams.
 * We therefore set the searchpath to also point to the font directory.
 */
$fontpath = dirname(__FILE__,3)."/resource/font";
$outfile = "";
$title = "Transparent Stamp for PDF/A-1";

$pdffile = "PLOP-datasheet-PDFA-1b.pdf";

/* Data set for our halftoning bitmap: the bits describe small
* circles which cover a certain percentage of the total pattern area.
*/
$data = array(array( 
    0x00, 0x00,  /* 30% */
    0x00, 0x00,
    0x00, 0x00,
    0x03, 0xC0,
    0x07, 0xE0,
    0x0F, 0xF0,
    0x1F, 0xF8,
    0x1F, 0xF8,

    0x1F, 0xF8,
    0x1F, 0xF8,
    0x0F, 0xF0,
    0x07, 0xE0,
    0x03, 0xC0,
    0x00, 0x00,
    0x00, 0x00,
    0x00, 0x00,
),array(
    0x00, 0x00, /* 20% */
    0x00, 0x00,
    0x00, 0x00,
    0x00, 0x00,
    0x03, 0xC0,
    0x07, 0xE0,
    0x0F, 0xF0,
    0x0F, 0xF0,

    0x0F, 0xF0,
    0x0F, 0xF0,
    0x07, 0xE0,
    0x03, 0xC0,
    0x00, 0x00,
    0x00, 0x00,
    0x00, 0x00,
    0x00, 0x00,
));

$ht = 1; /* index in halftone array */

define("SCALING_FACTOR", 16);

try {
    $p = new PDFlib();

    $p->set_option("searchpath={" . $searchpath . "}");

    $p->set_option("searchpath={" . $fontpath . "}");

    /* This means we must check return values of load_font() etc. */
    $p->set_option("errorpolicy=return");

    if ($p->begin_document($outfile, "pdfa=PDF/A-1b:2005") == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    $p->set_info("Creator", "PDFlib Cookbook");
    $p->set_info("Title", $title);

    /* -----------------------------------------------------------------
     * Open the input PDF.
     * This must be done before creating the pattern because the output
     * intent must be set before defining the pattern.
     * -----------------------------------------------------------------
     */
    $indoc = $p->open_pdi_document($pdffile, "");
    if ($indoc == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    $endpage = (int) $p->pcos_get_number($indoc, "length:pages");

    /* Since the input document contains its own output intent retrieve
     * the output intent from the input document and copy it to the output
     * document.
     */
    $res = $p->pcos_get_string($indoc, "type:/Root/OutputIntents");
    if ($res == "array") {
        $ret = $p->process_pdi($indoc, -1, "action=copyoutputintent");
        if ($ret == 0)
            throw new Exception("Error: " . $p->get_errmsg());
    }else {
        /* If the input document doesn't have any output intent we
        * explicitly set sRGB so that we can use grayscale bitmaps.
        */
        $p->load_iccprofile("sRGB", "usage=outputintent");
    }

    /* -------------------------------------------------------------------
     * Define a bitmap pattern based on an image mask.
     * We scale down the image to provide a smoother appearance on screen.
     * -------------------------------------------------------------------
     */
    $bitmap = "";

    for ($j=0; $j < count($data[$ht]); $j++) {
        $bitmap .= sprintf("%c",$data[$ht][$j]);
    }

    $p->create_pvf("/pvf/image/bitmap", $bitmap, "");

    $image = $p->load_image("raw", "/pvf/image/bitmap",
        "bpc=1 components=1 height=16 width=16 invert mask");

    if ($image == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    $w = 16 / SCALING_FACTOR;
    
    $pattern = $p->begin_pattern_ext($w, $w, "");

    $p->fit_image($image, 0, 0, "scale=" . (1 / SCALING_FACTOR));

    $p->end_pattern();

    $p->close_image($image);

    /* Loop over all pages of the input document */
    for ($pageno = 1; $pageno <= $endpage; $pageno++)
    {
        $page = $p->open_pdi_page($indoc, $pageno, "");

        if ($page == 0)
            throw new Exception("Error: " . $p->get_errmsg());

        /* Page size may be adjusted by fit_pdi_page() */
        $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");

        /* Place the imported page on the output page, and adjust the page
         * size.
         */
        $p->fit_pdi_page($page, 0, 0, "adjustpage");

        /*
         * Load the font for the stamp.
         */
        $font = $p->load_font("NotoSerif-Regular", "unicode", "");

        if ($font == 0)
            throw new Exception("Error: " . $p->get_errmsg());

        /* Place the stamp, filled with the pattern color */
        $p->setcolor("fill", "pattern", $pattern, 0, 0, 0);

        $p->fit_textline("PUBLISHED", 20, 20,
            "font=" . $font . " fontsize=1 boxsize={550 800} stamp=ll2ur");

        $p->close_pdi_page($page);

        $p->end_page_ext("");
    }

    $p->end_document("");
    $buf = $p->get_buffer();
    $len = strlen($buf);

    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=transparent_stamp_for_pdfa1.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("PHP exception occurred: " . $e->getMessage() . "\n");
    exit(1);
}

$p = 0;
?>