-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
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();