Skip to content
Advertisement

PDFlib bad font handle

so I’m currently writing a PHP program with PDFlib and got the following error when generating the PDF: “PDFlib exception occurred: [1434] info_textline: Option ‘font’ has bad font handle 0”.

Here’s the function in which the error occurs:

function createHeaderText(PDFlib $p, int $textStartLeft, int $fontMedium, int $fontRegular) {
    $y = 719;
    $x = $textStartLeft;

    $pageHeading = "Lorem ipsum dolor sit amet, consetetur sadipscing";
    $reference = "Referenzobjekt";

    $optlist = "font=" . $fontMedium .
        " fontsize=16" .
        " fillcolor=white" .
        " wordspacing=0";

    $infoHeight = $p->info_textline($pageHeading, 'objectheight', $optlist);
    $p->fit_textline($pageHeading, $x, $y,  $optlist);
    $y = $y - ($infoHeight + 11);

    $optlist = "font=" . $fontRegular .
        " fontsize=12" .
        " fillcolor=white" .
        " wordspacing=0";

    $infoHeight = $p->info_textline($pageHeading, 'objectheight', $optlist);
    $p->fit_textline($pageHeading, $x, $y, $optlist);
    return $y - ($infoHeight + 27.5);
}

And here’s my font handle:

$fontMedium = $p->load_font('fonts/NotoSerif-Medium', 'unicode', 'embedding');
$fontRegular = $p->load_font('fonts/NotoSerif-Regular', 'unicode', 'embedding');
$fontItalic = $p->load_font('fonts/NotoSerif-Italic', 'unicode', 'embedding');

Maybe someone can help me with this, it would help me a lot.

Advertisement

Answer

You must always check whether the font handle returned by load_font() is actually valid. In the PDFlib PHP binding you must check that the font handle returned by load_font() is non-zero. If it is zero, an error occurred and you must retrieve the error reason by calling $p->get_errmsg() to find out what went wrong.

An alternative for explicitly checking the handle is to set option errorpolicy=exception like this:

$p->set_option("errorpolicy=exception");

Then an error like the failure to load a font will cause an exception.

Now to the potential root cause of the problem: Probably the PHP process does not have access to the fonts directory, either because the current working directory of the PHP process is not the parent directory of the fonts directory where your Noto fonts are stored, or because of a permission problem.

In any case checking the return value of load_font() and retrieving the error message is a first step towards resolving the problem.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement