Home > Backend Development > PHP Tutorial > How to Customize Font Families in PDF Generation with Dompdf?

How to Customize Font Families in PDF Generation with Dompdf?

Patricia Arquette
Release: 2024-12-04 12:42:12
Original
658 people have browsed it

How to Customize Font Families in PDF Generation with Dompdf?

Dompdf: Customization of Font Families in PDF Generation

In dompdf, a popular PHP library for generating PDF documents, the default font family may not align with the intended design. This can be frustrating when trying to achieve a specific look and feel.

Dompdf's Font Support

Internally, PDF documents support a limited set of fonts by default: Helvetica, Times-Roman, Courier, Zapf-Dingbats, and Symbol. When encountering a custom font specified through CSS, dompdf attempts to embed it within the PDF if it has been previously loaded or is referenced via a CSS @font-face rule.

Font Loading Options for Dompdf

To load a desired font and enable its use in dompdf, you can leverage the following methods:

  1. CSS @font-face Rules: This method allows for runtime font loading if the font is accessible online. Simply define the font properties and specify the font's URL.
  2. load_font.php Script: Using the command line, you can run the load_font.php script included with dompdf to load fonts. Provide the font name, normal font file path, and optionally the bold, italic, and bold-italic font files.
  3. Fonts Admin Site: If you prefer a web-based approach, navigate to dompdf/www/fonts.php within the included admin site. This allows you to load fonts through a graphical interface.

Example Usage

To set the font-family to "NeutraText-Book" using the @font-face method:

@font-face {
  font-family: 'NeutraText-Book';
  src: url(/path/to/neutratext.ttf) format('truetype');
}
Copy after login

With the load_font.php script:

load_font.php "NeutraText-Book" /path/to/neutratext.ttf
Copy after login

The above is the detailed content of How to Customize Font Families in PDF Generation with Dompdf?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template