How to Set a Custom Font in Dompdf for PDF Generation?

Barbara Streisand
Release: 2024-11-13 17:06:02
Original
370 people have browsed it

How to Set a Custom Font in Dompdf for PDF Generation?

Dompdf: Setting a Custom Font for PDF Generation

Question:

When generating PDFs with Dompdf, the font-family attribute defined in CSS is ignored, resulting in a default font (e.g., Times New Roman) being used. How can a specific font be set within a PDF document?

Answer:

To set a custom font in Dompdf PDFs, you have several options:

1. CSS @font-face Rules:

By including @font-face rules in your CSS, you can load fonts online at runtime. For example:

@font-face {
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: normal;
  src: url(http://themes.googleusercontent.com/static/fonts/opensans/v8/cJZKeOuBrn4kERxqtaUH3aCWcynf_cDxXwCLxiixG1c.ttf) format('truetype');
}
Copy after login

2. Command-Line Loading:

If command-line access is available, you can use the dompdf/load_font.php script to load fonts. The syntax is:

[php] load_font.php "Font Name" /path/to/font.ttf
Copy after login

3. Admin Site (Depreciated):

Dompdf versions prior to 0.7.0 included an admin site (dompdf/www/fonts.php) where fonts could be loaded manually.

Font Support and Loading:

Dompdf supports PDF-compatible fonts such as Type 1 (.pfb) and TrueType (.ttf) fonts. Font loading is required to generate font metrics for typesetting. The bundled php-font-lib provides support for loading and subsetting fonts.

Note:

The command-line loading script and admin site will no longer be included by default in Dompdf versions starting from 0.7.0.

The above is the detailed content of How to Set a Custom Font in Dompdf for PDF Generation?. 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