Skip to content

Edoardo Vignati

– Always looking for something awesome –

Menu
  • About me
  • Studies
  • Publications
  • Skills
  • Portfolio
  • Projects
  • Career
  • Blog
Menu

How to install custom fonts in html2pdf

Posted on 01/12/202001/12/2020 by Edoardo

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


Buy Me A Coffee


This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  1. Edoardo on [SOLVED] XMLHttpRequest error in Flutter web is a CORS error06/01/2023
  2. Fabi on [SOLVED] XMLHttpRequest error in Flutter web is a CORS error20/12/2022
  3. Edoardo on How to install hplip on Ubuntu28/11/2022
  4. Alex K on How to install hplip on Ubuntu28/11/2022

© 2023 Edoardo Vignati | Powered by Minimalist Blog WordPress Theme
Manage Cookie Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage vendors Read more about these purposes
View preferences
{title} {title} {title}