![]() |
I've been trying for 2 days to get custom fonts working in dompdf package, not having much luck.
https://packagist.org/packages/dompdf/dompdf
I am using php composer to install dompdf package. This means I should not manually modify the vendor folder in my theme contents because I am not committing this to the my project repository.
https://getcomposer.org/
https://packagist.org/packages/dompdf/dompdf
This is not massively a wordpress related question but I am using this in wordpress so if anyone can get this working would be great thanks.
I've made a demo for this question using docker compose so you can quickly spin up a php local environment for testing dompdf...
https://github.com/joshmoto/dompdf-composer-font
https://i.stack.imgur.com/vVeyb.png
In the above repository run these commands to start the demo...
- npm install
- npm production
- composer require dompdf/dompdf
- docker-compose up -d
https://i.stack.imgur.com/y3QE0.png
In pdf.scss I am importing fontawesome (via an npm package) and also including 4 custom ttf fonts.
I am then importing dist pdf.css into the create-pdf.php file... So if you run create-pdf.php via http://localhost/create-pdf.php, but uncomment the // output html on page block so only the pdf html outputs, this is what it outputs... https://i.stack.imgur.com/HjECQ.png But if you re-comment that block back and stream the pdf using dompdf, this is what it outputs... https://i.stack.imgur.com/zfRI3.png Annoyingly these fonts are not getting automatically added to the dompdf fonts folder... https://i.stack.imgur.com/HjECQ.png But I have experienced dompdf automatically adding these fonts and updating dompdf_font_family_cache.dist.php, but in the most simplest example above and my current project, they are not getting added. Is there a proper way to do add fonts dompdf? I've read about 20 posts with no definitive answer for a composer environment. A simple solution for this type of environment, with out manually modifying the vendors directory would be awesome. Thanks ---------- I have also tried using load_font.php to manually load fonts into dompdf fonts folder following this guide... https://github.com/dompdf/dompdf/issues/1928 Even though it seems like a filthy way to manage this because you are manually modifying the vendor folder which is generated via composer. Step 1 Get your .ttf fonts and place inside vendor/dompdf/dompdf... https://i.stack.imgur.com/N4aYo.png Step 2 Download dompdf/utils package from github (doesn't exist on packagist) then copy load_font.php and autoload.inc.php to /vendor/dompdf/dompdf... https://github.com/dompdf/utils https://i.stack.imgur.com/WCOil.png Step 3 Ensure you have php-svg-lib and php-font-lib folders inside /vendor/dompdf/dompdf/lib... https://github.com/PhenX/php-svg-lib https://github.com/PhenX/php-font-lib *I copied these from /vendor/phenx as they were included in dompdf composer package.* https://i.stack.imgur.com/nAbPk.png Step 4 Edit the load_font.php file make sure that the first require_once points to the dompdf autoloader... Step 5 Install the fonts by running command php load_font.php SolPro SolPro-Black.ttf SolPro-Bold.ttf SolPro-Light.ttf SolPro-Regular.ttf in /vendor/dompdf/dompdf... Which then does add the fonts to /vendor/dompdf/dompdf/lib/fonts and it does update dompdf_font_family_cache.dist.php array... Unfortunately it does not support numerical font-weight values. But when I run create-pdf.php via http://localhost/create-pdf.php it still outputs pdf with fonts missing... https://i.stack.imgur.com/zfRI3.png Solution - 1are you stil looking for help? Solution - 2Have you tried with font-face? https://makitweb.com/how-to-set-different-font-family-in-dompdf/#create I had used DOMPDF with this method and I had fewer problems. |
Wordpress |
Type: | Code Example |
Category: | Coding |
Sub Category: | Code Example |
Uploaded by: | Admin |
Views: | 30 |