PDFlib Cookbook

cookbook

fonts/retain_font

Performance benefits of keeping a font open across multiple documents.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * 
 * Retain fonts:
 * Demonstrate performance benefits of keeping a font open across multiple 
 * documents.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: Fallback font
 */

/**
 * Name of the font to load.
 */
define("FONTNAME", "NotoSerif-Regular");

/**
 * Number of documents to generate.
 */
define("N_DOCS", 100);

/**
 * This is where the data files are. Adjust as necessary.
 */
define("SEARCH_PATH", dirname(__FILE__,3)."/input");

/**
 * Page width
 */
define("WIDTH", 595);

/**
 * Page height
 */
define("HEIGHT", 842);

/**
 * Method that creates N_DOCS documents in memory.
 * 
 * @param keepfont
 *            if true, retain font across all generated documents, otherwise
 *            load it again for each document
 */
function make_test_docs($keepfont) {
    try {
        
        $p = new PDFlib();

        $p->set_option("searchpath={" . SEARCH_PATH . "}");

        /*
         * Load a font
         */
        if ($keepfont) {
            /*
             * keepfont=true is default here, so it does not need to be
             * specified explicitly.
             */
            $font = $p->load_font(FONTNAME, "unicode", "keepfont=true");
            if ($font == 0)
                throw new Exception("Error: " . $p->get_apiname() . ": "
                        . $p->get_errmsg());
        }

        for ($i = 0; $i < N_DOCS; $i += 1) {
            /*
             * Create a simple document that makes use of the font. The
             * document is generated in memory and immediately discarded.
             */
            if ($p->begin_document("", "") == 0)
                throw new Exception("Error: " . $p->get_apiname() . ": "
                        . $p->get_errmsg());

            $p->set_info("Creator", "PDFlib Cookbook");
            $p->set_info("Title", "Dummy test document");

            $p->begin_page_ext(WIDTH, HEIGHT, "");

            if (!$keepfont) {
                /*
                 * keepfont=false is default here.
                 */
                $font = $p->load_font(FONTNAME, "unicode", "keepfont=false");

                if ($font == 0)
                    throw new Exception("Error: " . $p->get_apiname() . ": "
                            . $p->get_errmsg());
            }

            $p->fit_textline("Hello world!", 50, 700,
                    "font=" . $font . " fontsize=24");

            $p->end_page_ext("");

            $p->end_document("");
        }
    } catch (PDFlibException $e) {
        echo("PDFlib exception occurred:\n".
            "[" . $e->get_errnum() . "] " . $e->get_apiname() .
            ": " . $e->get_errmsg() . "\n");
    } catch (Throwable $e) {
        echo("PHP exception occurred: " . $e->getMessage() . "\n");
    }

    $p=0;

}

$outfile = "";
$title = "Retain Fonts";

/*
 * Time creation of test documents with and without retaining of font.
 */
$start_date1 = microtime(true);
make_test_docs(false);
$time_diff1 = sprintf("%.2f", microtime(true) - $start_date1);

$start_date2 = microtime(true);
make_test_docs(true);
$time_diff2 = sprintf("%.2f", microtime(true) - $start_date2);

try {
    $p = new PDFlib();

    $p->set_option("searchpath={" . SEARCH_PATH . "}");

    /* 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_apiname() . ": "
                . $p->get_errmsg());

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

    $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");

    $tf = 0;
            
    $tf = $p->add_textflow($tf,
        "Performance benefit of retaining a font across documents:\n\n",
        "fontname=NotoSerif-Regular encoding=unicode fontsize=18");
    
    $tf = $p->add_textflow($tf, "Time spent for creating " . N_DOCS 
        . " documents without retaining font:\n", "fontsize=16");
    $tf = $p->add_textflow($tf, $time_diff1 . " seconds\n\n", "");
    
    $tf = $p->add_textflow($tf, "Time spent for creating " . N_DOCS 
        . " documents while retaining font:\n", "");
    $tf = $p->add_textflow($tf, $time_diff2 . " seconds\n\n", "");
    
    $tf = $p->add_textflow($tf,
        "Note: Actual results will vary depending on various factors,\n" . 
        "including font, complexity of the document and platform.", "");
    
    $p->fit_textflow($tf, 50, 50, 545, 716, "");
    
    $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=retain_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;

?>