Skip to content

Cache .afm.json is not saved, because it's looking at wrong folder for .afm file #3569

@ridgey-dev

Description

@ridgey-dev

I use DomPDF to get the width of a specific text. When the font is not found, it throws

ErrorException: Undefined array key ""
/var/www/test/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:290
/var/www/test/vendor/dompdf/dompdf/lib/Cpdf.php:5180
/var/www/test/vendor/dompdf/dompdf/src/Adapter/CPDF.php:851

at

$current_font = $this->fonts[$cf];

It looks like the default font (.afm.json) is not saved in the cache folder.

In Cpdf.php:3337 it tries to find the Helvetica.afm to save it in the cache folder.

if (!isset($this->fonts[$font]) && file_exists("$dir/$metrics_name")) {

It tries to find the .afm file in './fonts/Helvetica.afm' (that is a fixed value of Cpdf.php :: $defaultFont). The afm file can indeed be found in ./fonts/Helvetica.afm, but because the working directory is not that file it can't find it.

When I change the $dir variable to the absolute path like this:

Cpdf.php:3337

$dir = '/var/www/test/vendor/dompdf/dompdf/lib/fonts/';

if (!isset($this->fonts[$font]) && file_exists("$dir/$metrics_name")) {
	....
}

it works, and the .afm.json file is saved in the cache folder, and there is no exception throwed.

It looks like this dir can't be configured somewhere. How can I fix this without modifying the source file?

$options = new Options();
$options->set('isRemoteEnabled', false);
$options->set('isPhpEnabled', false);
$options->set('fontCache', 'storage/dompdf-font-cache');
$domPdf = new Dompdf($options));


$domPdf->loadHtml($html);
$canvas = $domPdf->getCanvas();
$fonts = $domPdf->getFontMetrics();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions