PDFlib Cookbook

cookbook

color/svg_color_extension

Demonstrate SVG color extensions: Load SVG graphics with non-sRGB color spaces including CMYK and spot color.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Demonstrate SVG color extensions:
 * Load SVG graphics with non-sRGB color spaces including CMYK and spot color.
 * 
 * The SVG uses color extension for device-gray/rgb/cmyk, cielab,
 * icc-color (RGB and CMYK), icc-named-color for known (Pantone) spot color
 * as well as custom spot color, and device-nchannel color.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9.1
 * Required data: SVG graphics
 */

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

$graphicsfile = "svg_color_extension.svg";
$x = 50; $y = 200;
$boxwidth = 500; $boxheight = 600;
$exitcode = 0;

try {
    $p = new pdflib();

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

    /* This means we must check return values of load_graphics() 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", 'svg_color_extension');

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

    /* Define custom spot color "CompanyRed" with Lab alternate values
     * This color is used in the SVG file; its definition must be
     * supplied in the code. 
     */
    $p->set_graphics_option("fillcolor={spotname {CompanyRed} 1.0 {lab 60 65 65}}");
    
    // Define DeviceN color based on two process colors

    $devicen = $p->create_devicen(
        "names={Magenta Yellow} alternate=devicecmyk transform={{0 0 4 1 roll}}");

    /* Load the graphics. The option "devicencolors" contains the
     * prepared DeviceN color space handle.
     */
    $graphics = $p->load_graphics("auto", $graphicsfile,
                "devicencolors={" . $devicen . "}");

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

    /* Fit SVG graphics into the specified box */
    $optlist = "boxsize={ " . $boxwidth . " " . $boxheight
        . "} position={center} fitmethod=meet ";

    /*
     * Before actually fitting the graphics we check whether fitting is
     * possible.
     */
    if ($p->info_graphics($graphics, "fittingpossible", $optlist) == 1) {
        $p->fit_graphics($graphics, $x, $y, $optlist);
    }
    else {
        throw new Exception("Cannot place graphics: " . $p->get_errmsg());
    }

    $p->end_page_ext("");

    $p->close_graphics($graphics);

    $p->end_document("");

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

    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=svg_color_extension.pdf");
    print $buf;
}
catch (PDFlibException $e) {
    echo("PDFlib exception occurred in svg_color_extension sample:" .
    "[" . $e->get_errnum() . "] " . $e->get_apiname()
            . ": " . $e->get_errmsg());
    exit(1);
}
catch (Throwable $e) {
    echo($e);
    exit(1);
}
$p= 0;