PDFlib Cookbook

cookbook

color/spot_color_shading

Define and use several spot colors as shading.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Shadings with N=2/3/4 Pantone spot colors as stop colors
 * PDFlib automatically creates a suitable DeviceN color space in which
 * a shading between multiple spot colors can be expressed.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9.1.1
 * 
 * Required data: none
 */
/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";
$outfile = "";
$title = "Spot color shading";

$y = 600; $x = 30; $width = 700; $height = 50; $fontsize = 14; $leading = 21;
$exitcode = 0;

try {
    $p = new pdflib();

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

    /* This means that errors in load_font() etc. throw an exception */
    $p->set_option("errorpolicy=exception");

    if ($p->begin_document($outfile, "") == -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("NotoSerif-Bold", "unicode", "");

    /* Start the page */
    $p->begin_page_ext(0, 0, "width=a4.height height=a4.width");

    $p->setfont($font, $fontsize);

    /*
     * *******************************************************
     * Define a shading color based on two Pantone spot colors
     */
    $p->fit_textline(
            "Shading with spot colors PANTONE 123 U, PANTONE 289 U:", $x,
            $y -= 2 * $leading, "fillcolor=black");

    $y -= $height + $fontsize;

    /* Create axial shading based on two Pantone spot colors */
    $sh = $p->shading("axial", $x, $y, $x + $width, $y, 0, 0, 0, 0,
            "startcolor={spotname {PANTONE 123 U} 1} "
                    . "  endcolor={spotname {PANTONE 289 U} 1}");

    $shp = $p->shading_pattern($sh, "");
    $p->set_graphics_option("fillcolor={pattern " . $shp . "}");

    /* Fill a rectangle with the shading */
    $p->rect($x, $y, $width, $height);
    $p->fill();

    /*
     * *******************************************************
     * Define a shading based on three Pantone spot colors
     */
    $p->fit_textline(
            "Shading with spot colors PANTONE 123 U, PANTONE 289 U, PANTONE Rubine Red U:",
            $x, $y -= 2 * $leading, "fillcolor=black");

    $y -= $height + $fontsize;

    /*
     * Create axial shading based on three Pantone spot colors, using
     * 100% of each component at 0%, 50%, 100%, respectively.
     */
    $sh = $p->shading("axial", $x, $y, $x + $width, $y, 0, 0, 0, 0,
            "stopcolors={  0%  {spotname {PANTONE 123 U} 1} "
                    . " 50%  {spotname {PANTONE 289 U} 1} "
                    . "100%  {spotname {PANTONE Rubine Red U} 1} }");

    $shp = $p->shading_pattern($sh, "");
    $p->set_graphics_option("fillcolor={pattern " . $shp . "}");

    /* Fill a rectangle with the shading */
    $p->rect($x, $y, $width, $height);
    $p->fill();

    /*
     * *******************************************************
     * Define a shading based on four Pantone spot colors
     */
    $p->fit_textline(
            "Shading with spot colors PANTONE 123 U, PANTONE 289 U, PANTONE Rubine Red U, PANTONE 1215 U:",
            $x, $y -= 2 * $leading, "fillcolor=black");
    $y -= $height + $fontsize;

    /*
     * Create axial shading based on DeviceN color, using 100% of each
     * component distributed over the range 0%-100%
     */
    $sh = $p->shading("axial", $x, $y, $x + $width, $y, 0, 0, 0, 0,
            "stopcolors={  0%  {spotname {PANTONE 123 U} 1} "
                    . " 33%  {spotname {PANTONE 289 U} 1} "
                    . " 66%  {spotname {PANTONE Rubine Red U} 1} "
                    . "100%  {spotname {PANTONE 1215 U} 1} }");

    $shp = $p->shading_pattern($sh, "");
    $p->set_graphics_option("fillcolor={pattern " . $shp . "}");

    /* Fill a rectangle with the shading */
    $p->rect($x, $y, $width, $height);
    $p->fill();

    $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; spot_color_shading.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;