PDFlib Cookbook

cookbook

fonts/barcode_font

Output text in a barcode font.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Barcode font:
 * Output text in a barcode font.
 * 
 * Load a barcode font and output text. Enclose the text in the start and stop 
 * characters which are individually defined by the respective barcode font.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: font file
 */

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


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

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

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

    /* Start page */
    $p->begin_page_ext(170, 120, "");

    /* Load the barcode font.
     * For a symbol barcode font, please change "unicode" to "builtin".
     */
    $barcodefont = $p->load_font("FRE3OF9X", "unicode", "");
    if ($barcodefont == 0)
        throw new Exception("Error: " . $p->get_errmsg());
    
    /* Output text with the barcode font. Note the following when creating
     * barcode text: To create a valid 3 of 9 barcode you have to begin and
     * end it with a special character. Scanners look for this character to
     * know where to start and stop reading the barcode. It is represented
     * in this font with the '*' character. So, to create a barcode for the
     * text "ABC123" you have to type out "*ABC123*". Note that barcode
     * readers will not include the *'s in the text they return. They will
     * just give you the "ABC123".
     */

    $p->fit_textline("*ABC123*", 10, 75, "font=" . $barcodefont .
        " fontsize=20");
    
    $normalfont = $p->load_font("NotoSerif-Regular", "unicode", "");
    if ($normalfont == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    $p->fit_textline("*ABC123* ", 10, 60, "font=" . $normalfont .
        " fontsize=10");
    $p->fit_textline("which will be returned by the", 10, 30, "font=" .
        $normalfont . " fontsize=10");
    $p->fit_textline("barcode reader as ABC123", 10, 10, "font=" .
        $normalfont . " fontsize=10");

    /* Finish page */
    $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=barcode_font.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;

?>