PDFlib Cookbook

cookbook

type3_fonts/type3_rasterlogo

Create a Type 3 font which contains a single logo derived from an image.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Type 3 raster logo font:
 * Create a Type 3 font which contains a single logo derived from an image
 *
 * Import image data from a bitmap TIF image to create a Type 3 logo font
 * containing one glyph. Output text with that glyph.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: bitmap TIFF image
 */

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

$logofile = "phone.tif";

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

    /*
     * Create the font PhoneLogoFont. The matrix entries are chosen to
     * create the common 1000x1000 coordinate system. These numbers are
     * also used when placing the logo within the glyph box below
     * (option "boxsize").
     */
    $p->begin_font("PhoneLogoFont", 0.001, 0.0, 0.0, 0.001, 0.0, 0.0, "");

    /* The .notdef (fallback) glyph should be contained in all Type 3
     * fonts to avoid problems with some PDF viewers. It is usually
     * empty.
     */
    $p->begin_glyph_ext(0x0000, "width=1000");
    $p->end_glyph();

    /*
     * Add a glyph with the Unicode value U+260F WHITE TELEPHONE, glyph
     * name "phone" and width 1000.
     */
    $p->begin_glyph_ext(0x260F, "width=1000 boundingbox={0 0 1000 1000} "
        . "glyphname=phone");

    /* Load the bitmap data for the glyph from the file "phone.tif".  */
    $image = $p->load_image("auto", $logofile, "mask");
    if ($image == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    /* Fit the image in a box similar to the dimensions of the glyph box.
     */
    $p->fit_image($image, 0, 0, "boxsize={1000 1000} fitmethod=meet");

    $p->close_image($image);

    $p->end_glyph();

    $p->end_font();

    /* Load the new "PhoneLogoFont" font with the encoding "unicode" */
    $logofont = $p->load_font("PhoneLogoFont", "unicode", "");
    if ($logofont == 0)
        throw new Exception("Error: " . $p->get_errmsg());

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

    /* Start page */
    $p->begin_page_ext(0, 0, "width=300 height=100");

    /* For subsequent text switch on substitution of character references */
    $p->set_text_option("charref=true");
    
    /* Output the character U+260F of the "PhoneLogoFont" font */
    $p->fit_textline("&#x260F;", 20, 50, "font=" . $logofont 
                    . " fontsize=14");

    $textxpos = $p->get_option("textx", "");
    
    /* Output standard text */
    $p->fit_textline("This is the phone logo", $textxpos + 4 , 50, "font=" . $normalfont .
                   " fontsize=14");

    $textxpos = $p->get_option("textx", "");
    
    /*
     * Alternatively, we can select the logo glyph via a character
     * reference which refers to the glyph name "phone". Use the
     * "charref" option to enable character referencing.
     */
    $p->fit_textline("&.phone;", $textxpos + 4, 50, "font=" . $logofont . " fontsize=14");

    /* 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=type3_rasterlogo.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;

?>