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'); }
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
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.
以上がPDF 生成のために Dompdf でカスタム フォントを設定する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。