PDFlib Cookbook

cookbook

color/overprint

Demonstrate overprint controls using CMYK and DeviceN colors or blend modes.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Overprint colors:
 * - demonstrate the difference between CMYK output with/without overprinting
 * - blendmode=darken as device-independent alternative to overprinting
 * - DeviceN is never affected by overprint settings
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: font file
 */

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

$x = 50;
$y = 700;
$width = 150;
$height = 75;
$fontsize = 9;

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("", "") == 0)
        throw new Exception("Error: " . $p->get_errmsg());
    
    $p->set_info("Creator", "PDFlib Cookbook");
    $p->set_info("Title", $title);
    
    /*
     * The transparency group colorspace is important for Overprint
     * Preview on the screen.
     */
    $p->begin_page_ext(0, 0, "width=a4.width height=a4.height transparencygroup={colorspace=devicecmyk}");
    
    $font = $p->load_font("NotoSerif-Regular", "unicode", "");
    
    if ($font == 0)
        throw new Exception("Error: " . $p->get_errmsg());
    
    $p->setfont($font, 11);
    
    /*
     * *******************************************************
     * Yellow and Cyan in CMYK color space without overprinting
     */
    $p->set_graphics_option("fillcolor={cmyk 1 0 0 0}");
    $p->rect($x, $y, $width, $height);
    $p->fill();
    
    $optlist = "overprintfill=false";
    $gstate = $p->create_gstate($optlist);
    $p->save();
    $p->set_gstate($gstate);
    $p->set_graphics_option("fillcolor={cmyk 0 0 1 0}");
    $p->rect($x + $width / 2, $y + $height / 2, $width, $height);
    $p->fill();
    $p->restore();
    $p->fit_textline("CMYK colors without overprinting: " . $optlist, $x, 
                    $y - 2 * $fontsize, "fillcolor=black");
    
    /*
     * *******************************************************
     * Overprint Yellow and Cyan in CMYK color space: this is
     * device-dependent; it requires "Overprint Preview" enabled in
     * Acrobat.
     */
    $y -= 2 * $height;
    $p->set_graphics_option("fillcolor={cmyk 1 0 0 0}");
    $p->rect($x, $y, $width, $height);
    $p->fill();
    
    $optlist = "overprintfill=true overprintmode=1";
    $gstate = $p->create_gstate($optlist);
    $p->save();
    $p->set_gstate($gstate);
    $p->set_graphics_option("fillcolor={cmyk 0 0 1 0}");
    $p->rect($x + $width / 2, $y + $height / 2, $width, $height);
    $p->fill();
    $p->restore();
    $p->fit_textline("Device-dependent overprinting: " . $optlist, $x, 
                    $y - 2 * $fontsize, "fillcolor=black");
    
    /*
     * *******************************************************
     * Achieve device-independent overprinting effect with Yellow and
     * Cyan in CMYK color space with blend mode "Darken".
     */
    $y -= 2 * $height;
    $p->set_graphics_option("fillcolor={cmyk 1 0 0 0}");
    $p->rect($x, $y, $width, $height);
    $p->fill();
    
    $optlist = "blendmode=darken";
    $gstate = $p->create_gstate($optlist);
    $p->save();
    $p->set_gstate($gstate);
    $p->set_graphics_option("fillcolor={cmyk 0 0 1 0}");
    $p->rect($x + $width / 2, $y + $height / 2, $width, $height);
    $p->fill();
    $p->restore();
    $p->fit_textline(
                    "Device-independent overprinting effect with blend mode: " .
                                     $optlist, $x, $y - 2 * $fontsize, 
                                    "fillcolor=black");
    
    /*
     * *******************************************************
     * Overprint Yellow and Cyan in DeviceN color space: overprint
     * settings don't have any effect.
     */
    $y -= 2 * $height;
    $devicen = $p->create_devicen(
            "names={Cyan Yellow} alternate=devicecmyk transform={{0 0 3 1 roll}} errorpolicy=exception");
    
    $p->set_graphics_option("fillcolor={devicen " . $devicen . " 1 0}");
    $p->rect($x, $y, $width, $height);
    $p->fill();
    
    $gstate = $p->create_gstate("overprintfill=true overprintmode=1");
    $p->save();
    $p->set_gstate($gstate);
    $p->set_graphics_option("fillcolor={devicen " . $devicen . " 0 1}");
    $p->rect($x + $width / 2, $y + $height / 2, $width, $height);
    $p->fill();
    $p->restore();
    $p->fit_textline(
                    "DeviceN colors (Cyan+Yellow): always unaffected by overprint settings", 
                    $x, $y - 2 * $fontsize, "fillcolor=black");
    
    $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=overprint.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;
?>