Here is a short guide on how to install custom fonts in the famous html2pdf php tool. If you don’t know how to install or how to use it, please checkout my other post here.
I hope this can be the final and easy guide to accomplish this task.
First of all let’s download the desired font. Personally I use DaFont but remember that fonts could be under different licenses so please check it up before to use the font in public.
The font comes in a zip folder in different formats: woff, ttf, otf… in our case we need to use the ttf format so if you don’t have it please convert it online, for example using convert.io tool.
When you have the ttf file we are ready for the installation. The installation of the font requires the ttf to be splitted in 3 different files: myfont.afm, myfont.z and myfont.php
This online tool can be very helpful to create these 3 different files: simply upload the ttf file, give it a name (avoid spaces and special chars) and wait for the conversion.
If no errors or warnings are promped out you should be able to download all of the needed files.
Now open your server and upload these files into:
html2pdf/vendor/tecnickcom/tcpdf/fonts/
You are now able to use the font in your pdf files. The following snippet could be helpful:
<?php /* Snippet from: www.edoardovignati.it/how-to-install-custom-fonts-in-html2pdf/ */ require_once dirname(__FILE__).'/html2pdf/vendor/autoload.php'; use Spipu\Html2Pdf\Html2Pdf; use Spipu\Html2Pdf\Exception\Html2PdfException; use Spipu\Html2Pdf\Exception\ExceptionFormatter; try { $html2pdf = new Html2Pdf('P', 'A4', 'it', true, 'UTF-8', array(0, 0, 0, 0)); $html2pdf->pdf->SetDisplayMode('fullpage'); $html2pdf->setDefaultFont("advanced-dot"); ob_start(); $content = ob_get_clean(); $html = "<html>"; $html .= "<head></head>"; $html .= "<body>"; $html .= "Hello world!"; $html .= "</body>"; $html .= "</html>"; $html2pdf->writeHTML($html); ob_end_clean(); $html2pdf->output('mypdf.pdf'); } catch (Html2PdfException $e) { $html2pdf->clean(); $formatter = new ExceptionFormatter($e); echo $formatter->getHtmlMessage(); } ?>
If you want to use custom fonts in an HTML page please checkout my other post here.
Cheers
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.