PDFlib Cookbook

cookbook

color/reverse_printing

'Reverse printing' is the process of printing text or graphical elements by applying ink to the surrounding area and keeping the text unprinted.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Reverse printing
 *
 * "Reverse printing" is the process of printing text or graphical elements by 
 * applying ink to the surrounding area and keeping the text unprinted. When 
 * printing on paper the unmarked area remains in paper color, i.e. usually 
 * white. In flexographic printing (used for package printing on substrates 
 * other than paper) it may be required to apply an additional die color, e.g. 
 * when printing on translucent substrate.
 * The code in this topic draws a solid rectangle with 100% spot color and then 
 * places text with 0% spot color.
 * 
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: none
 *
 */

/* This is where the data files are. Adjust as necessary. */
$searchpath = "../input";
$outfile = "";
$title = "Reverse printing";

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");

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

    $p->set_info("Creator", "PDFlib Cookbook");
    $p->set_info("Title", $title);
    
    /* Start the page */
    $p->begin_page_ext(0, 0, "width=a4.height height=a4.width");

    /* Draw a background rectangle and fill it with 100% "PANTONE 463 C". */
    $p->set_graphics_option("fillcolor={spotname {PANTONE 463 C} 1}");

    $p->rect(0, 0, 842, 595);
    $p->fill();

    /* Draw text with the same spot color, but a tint value of 0. Nothing 
     * will be painted because no ink is applied. Due to the colorized 
     * background area the text will be visible nevertheless.
     */
    
    $p->fit_textline("Reverse Printing", 50, 50, 
        "fontname=NotoSerif-Bold fontsize=40 boxsize={742 495} fitmethod=meet " .
        "position=center fillcolor={spotname {PANTONE 463 C} 0}");
    
    $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=reverse_printing.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;
?>