PDFlib Cookbook

cookbook

pdfx/starter_pdfx5n

Create PDF/X-5n conforming output with an image and DeviceN color.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 *
 * PDF/X-5n starter:
 * Create PDF/X-5n conforming output with an image and DeviceN color
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9.1
 * Required data: font file, image file, n-colorant ('xCLR') ICC output
 *                intent profile
 *                
 * This program uses an n-colorant ICC output intent profile. As there are no
 * freely distributable n-colorant ICC profiles available for download, this
 * example program is disabled by default. To use this program put an
 * n-colorant ICC profile into the "extra_input" directory and assign its
 * name to the "profilename" variable below.
 */

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

$imagefile = "zebra.tif";
/*
 * N-colorant (xCLR) output intent profile. Replace name with
 * available n-colorant ICC profile name that is stored in the
 * "extra_input" directory.
 */
$profilename = "7C Indigo_TAC370_ColorLogic.icc";

$transform2 =
    "% DeviceN transform function for N=2 in CIE L*a*b* color space\n" .
    "% Lab color values of input colors must be listed here:\n"   .
    "71.6640  53.2335  99.3307        % color 1: Orange\n"        .
    "30.3471  56.1673 -76.4086        % color 2: Violet\n"        .
    "% blend L values\n"                                          .
    "7 index 6 index mul              % t1*L1\n"                  .
    "7 index 4 index mul              % t2*L2\n"                  .
    "add\n"                                                       .
    "9 1 roll                         % bottom: L\n"              .
    "% blend a values\n"                                          .
    "7 index 5 index mul              % t1*a1\n"                  .
    "7 index 3 index mul              % t2*a2\n"                  .
    "add\n"                                                       .
    "9 1 roll                         % bottom: a\n"              .
    "% blend b values\n"                                          .
    "7 index 4 index mul              % t1*b1\n"                  .
    "7 index 2 index mul              % t2*b2\n"                  .
    "add\n"                                                       .
    "9 1 roll                         % bottom: b\n"              .
    "pop pop pop pop pop pop pop pop\n";

try {
    $p = new pdflib();

    /* check if PDFlib 9.1 or newer is installed */
    if (($p->get_option("major", "")*100)+($p->get_option("minor", "")*10) <910)
        throw new Exception("Error: PDFlib 9.1.0 or newer required, but " .
            "PDFlib " . $p->get_string(-1, "version") . " installed" );

    /*
     * Set errorpolicy to return, this means we must check return
     * values of load_font() etc.
     * Set the search path for fonts and images etc.
     */
    $p->set_option("errorpolicy=return SearchPath={{" . $searchpath1 . "} {" . $searchpath2 . "}}");

    $p->set_option("stringformat=utf8");

    if ($p->begin_document("", "pdfx=PDF/X-5n") == 0) {
        throw new Exception("Error: " + $p->get_errmsg());
    }

    $p->set_info("Creator", "PDFlib starter sample");
    $p->set_info("Title", 'starter_pdfx5n');

    if ($p->load_iccprofile($profilename, "usage=outputintent") == 0) {
        print("Error: " . $p->get_errmsg(). "\n");
        print("An n-colorant ('xCLR') printer profile is required as output intent for PDF/X-5n.\n");
        exit(1);
    }

    $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");

    /* Font embedding is required for PDF/X */
    $font = $p->load_font("NotoSerif-Regular", "unicode", "embedding");

    if ($font == 0) {
        throw new Exception("Error: " . $p->get_errmsg());
    }
    
    $p->fit_textline("PDF/X-5n starter sample with n-colorant output intent",
                    50, 800, "fontsize=18 font=" . $font);

    /*
     * Define the alternate colors for spot colors Orange and Violet for
     * future use in a DeviceN color space. If these colors are
     * described as colorants in the xCLR output intent profile PDFlib
     * fetches the definitions automatically and it is not required to
     * define them in the code.
     */
    $p->set_graphics_option("fillcolor={spotname {Orange} 1 "
                    . "{ lab 71.6640 53.2335 99.3307 }}");
    $p->set_graphics_option("fillcolor={spotname {Violet} 1 "
                    . "{ lab 30.3471  56.1673 -76.4086 }}");
    
    /* Create a DeviceN color space with two of the device's spot
     * colors.
     * The following types of spot colors can be used for PDF/X-5n:
     * - all built-in Pantone and HKS colors
     * - spot colors described in the n-colorant output intent profile
     * - custom spot colors defined with makespotcolor()
     */
    $devicen = $p->create_devicen(
                "names={Orange Violet} alternate=lab transform={{"
                    . $transform2 . "}} errorpolicy=exception");
    $p->set_graphics_option("fillcolor={devicen " . $devicen . " 0.9 0.2}");

    /* Fill a circle with the DeviceN color defined above */
    $p->circle(400, 600, 100);
    $p->fill();

    /* The RGB image needs an ICC profile; we use sRGB. */
    $icc = $p->load_iccprofile("sRGB", "");
    if( $icc == 0)
    {
        throw new Exception("Error: " . p.get_errmsg() ."<br/>\n" . 
            "See www.pdflib.com for ICC profiles.");
    }
    $optlist = "iccprofile=" . $icc;
    $image = $p->load_image("auto", $imagefile, $optlist);

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

    /* Place a diagonal stamp across the image area */
    $width = $p->info_image($image, "width", "");
    $height = $p->info_image($image, "height", "");

    /* Place the image on the page and close it */
    $p->fit_image($image, 0.0, 0.0, "");
    $p->close_image($image);

    $optlist = "fontsize=48 font=" . $font . " boxsize={" . $width . " "
                    . $height . "} stamp=ll2ur";
    $p->fit_textline("Zebra", 0, 0, $optlist);

    $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=starter_pdfx5n.pdf");
    print $buf;
}
catch (PDFlibException $e) {
    echo("PDFlib exception occurred in starter_pdfx5n sample:" .
    "[" . $e->get_errnum() . "] " . $e->get_apiname()
            . ": " . $e->get_errmsg());
    exit(1);
}
catch (Exception $e) {
    echo($e);
    exit(1);
}

$p = 0;

?>