PDFlib Cookbook

cookbook

textflow/avoid_linebreaking

Create a Textflow and define various options for line breaking.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Avoid line breaking:
 * Create a Textflow and define various options for line breaking
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: none
 */

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

$tf = 0; $tf_avoid = 0;

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");

    /* Set an output path according to the name of the topic */
    if ($p->begin_document($outfile, "") == 0)
        throw new Exception("Error: " . $p->get_errmsg());

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

    /* Create an A4 Landscape page */
    $p->begin_page_ext(0, 0, "width=a4.height height=a4.width");

    /* ---------------------------------------------------------------------
     * Output text which does not contain any options to avoid line breaking
     * ---------------------------------------------------------------------
     */
    $text = "For more information about the Giant Wing Paper Plane see " .
        "our Web site <underline=true>www.kraxi-systems.com" .
        "<underline=false>.<nextline>Alternatively, contact us by email " .
        "via <underline=true>questions@kraxi-systems.com" .
        "<underline=false>. You'll get all information about how to fly " .
        "the Giant Wing in a thunderstorm as soon as possible.";
 
    $p->fit_textline("Text without any options to avoid line breaking", 
        50, 430, "fontname=NotoSerif-Bold fontsize 12");

    /* Create the Textflow from the $text */
    $optlist = "fontname=NotoSerif-Regular fontsize=20 " .
        "leading=140%";
    $tf = $p->create_textflow($text, $optlist);
    if ($tf == 0)
        throw new Exception("Error: " . $p->get_errmsg());
    
    $result = $p->fit_textflow($tf, 50, 100, 300, 400,
        "fitmethod=auto showborder");
    if (!$result == "_stop")
    {
        /* Check for errors */
    }
    $p->delete_textflow($tf);
    
    
    /* --------------------------------------------------------------
     * Output the same $text but with additional options to avoid line
     * breaking
     * --------------------------------------------------------------
     */
    
    /* Text containing options to avoid any line breaking in the Web 
     * address with "<avoidbreak>...<noavoidbreak>" and to avoid any line
     * breaking after the "-" and "." characters in the email address with
     * "charclass={letter {- .}}>...<charclass=default>".
     */ 
    $text_avoid = "For more information about the Giant Wing Paper Plane " .
        "see our Web site <underline=true avoidbreak>" .
        "www.kraxi-systems.com<underline=false noavoidbreak>.<nextline>" .
        "Alternatively, contact us by email via <underline=true " .
        "charclass={letter {- .}}>questions@kraxi-systems.com" .
        "<charclass={default {- .}} underline=false>. You'll get all " .
        "information about how to fly the Giant Wing in a thunderstorm " .
        "as soon as possible.";
    
    $p->fit_textline("Text with \"charclass\" and \"avoidbreak\" options",
        450, 430, "fontname=NotoSerif-Bold fontsize=12");
    
    /* Create the Textflow from the $text */
    $tf_avoid = $p->create_textflow($text_avoid, $optlist);
    if ($tf_avoid == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    $result = $p->fit_textflow($tf_avoid, 450, 100, 700, 400,
        "fitmethod=auto showborder");
    if (!$result == "_stop")
    {
        /* Check for errors */
    }
    $p->delete_textflow($tf_avoid);

    $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=avoid_linebreaking.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;

?>