PDFlib Cookbook

cookbook

graphics/dashed_lines

Create some dash patterns to be used as line styles.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
* Dashed lines:
* Create some dash patterns to be used as line styles
*
* Required software: PDFlib/PDFlib+PDI/PPS 9
* Required data: none
*/

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

$startx = 10; $starty = 200; $x = 200; $y = 200;

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");
    $p->set_option("stringformat=utf8");

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

    $p->set_info("Creator", "PDFlib Cookbook");
    $p->set_info("Title", $title );

    $font = $p->load_font("Helvetica", "unicode", "");
    if ($font == 0)
    throw new Exception("Error: " . $p->get_errmsg());

    $p->begin_page_ext(250, 250, "");

    /* Set the drawing properties for the dash patterns */
    $p->setlinewidth(0.6);

    /* Set the first dash pattern with a dash and gap length of 3 */
    $p->set_graphics_option("dasharray={3 3}");

    /* Stroke a line with that pattern */
    $p->moveto($startx, $starty);
    $p->lineto($x, $y);
    $p->stroke();

    /* Set the second dash pattern with a dash length of 3 and
     * a gap length of 6
     */
    $p->set_graphics_option("dasharray={3 6}");

    /* Stroke a line with that pattern */
    $p->moveto($startx, $starty-20);
    $p->lineto($x, $y-20);
    $p->stroke();

    /* Set the third dash pattern with a dash length of 6 and
     * a gap length of 3
     */
    $p->set_graphics_option("dasharray={6 3}");

    /* Stroke a line with that pattern */
    $p->moveto($startx, $starty-40);
    $p->lineto($x, $y-40);
    $p->stroke();

    /* Set the fourth dash pattern with a dash length of 5 and
     * a gap length of 3, then a dash of 0.5 and a gap of 3, then a dash
     * of 3 and a gap of 3, then a dash of 0.0 and a gap of 3 and so forth
     */
    $p->set_graphics_option("dasharray={5 3 0.5 3 3 3 0.5 3}");
    /* Stroke a line with that pattern */
    $p->moveto($startx, $starty-60);
    $p->lineto($x, $y-60);
    $p->stroke();

    $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=dashed_lines.pdf");
    print $buf;


} catch (PDFlibException $e){
    echo("PDFlib exception occurred:\n" .
        "[" . $e->get_errnum() . "] " . $e->get_apiname() .
        ": " . $e->get_errmsg() . "\n");
    exit(1);
} catch (Exception $e) {
    echo($e->getMessage());
    exit(1);
}
$p = 0;
?>