PDFlib Cookbook

cookbook

fonts/font_configuration

Configure font resources and search for fonts.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Font resources:
 * Configure font resources and search for fonts
 *
 * In the easiest case, load a host font on Windows or Mac which is already
 * installed in the system. Then, use the "SearchPath" resource category to
 * define the directory with your fonts. In the next case, use a font
 * where the font file name equals the font name. If the font name and font
 * file name are not equal, use the "FontOutline" resource category to define
 * a font name to be used by PDFlib. With info_font() get the font name and 
 * the font filen ame.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: font file
 */

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

$x=20; 
$y=260;


try {
    $p = new PDFlib();

    /* 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(0, 0, "width=300 height=300");

    /* The convenient way: load a host font on Windows or Mac which is
     * already installed in the system. In this case no prerequisites
     * are required provided that you know the exact (case-sensitive)
     * name of the font (see the PDFlib Tutorial for information about
     * how to retrieve the exact name of a host font). For example, if
     * you installed the font "Verdana" in the system you can load it
     * just as follows.
     */

    /* The following will only work on Mac or Windows, provided the
     * font Verdana is installed in the system.
    $font = $p->load_font("Verdana", "unicode", "");
    if ($font == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    $p->setfont($font, 10);
    $p->fit_textline("Font file is installed in the system", $x, $y, "");
    $fontname = $p->get_string($p->info_font(font, "fontname", "api"), "");
    $p->fit_textline("Font name used in PDFlib: " . $fontname, $x, $y-=20, "");
     */

    /* If you do not want to use a host font the location of the font files
     * must be provided. Use the "SearchPath" resource category to define
     * the folder for your fonts to be searched in. Add similar
     * "Searchpath" commands to define further folders if necessary. PDFlib
     * will first search the font in all folders defined with "Searchpath".
     */
    $p->set_option("searchpath={" . $searchpath . "}");

    /* If you know the font name and the font file name (excluding the
     * file name extension such as ".ttf", ".otf", ".pfb" etc.) is equal
     * to the font name, you can simply load the font.
     */

    /* Alternatively, you can supply an absolute path name such as
     *
     * $p->set_option(FontOutline=NotoSerif-Regular=/usr/fonts/NotoS-R.ttf");
     *
     * In this case the font is loaded from the location defined above
     * without the searchpath being applied.
     */

    /* Now you can load the font using the name "NotoSerif-Regular".
     */
    $font = $p->load_font("NotoSerif-Regular", "unicode", "");
    if ($font == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    $p->setfont($font, 10);
    $p->fit_textline("Font file name: NotoSerif-Regular.ttf", $x, $y-=40, "");
    $fontname = $p->get_string($p->info_font($font, "fontname", "api"), "");
    $p->fit_textline("Font name used in PDFlib: " . $fontname, $x, $y-=20, "");
   
    /* 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=font_configuration.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;

?>