如何在DomPdf库中添加Arial字体?

问题描述 投票:0回答:1

嗯,正如标题所说。我正在使用 codeigniter 4,并且一直在尝试添加 Arial 字体以在 Dompdf 中使用它。我尝试将 Airal.ttf、Arial.afm 和 Arial.php 文件添加到 dompdf 文件夹中的“lib/fonts”文件夹中,但它不起作用。也许我错过了什么?我有这个用于测试目的,但它不起作用:

<?php
require 'vendor/autoload.php';
use Dompdf\Dompdf;
use Dompdf\Options;

// Set Dompdf options
$options = new Options();
$options->set('fontDir', 'vendor/dompdf/dompdf/lib/fonts');
$options->set('fontCache', 'vendor/dompdf/dompdf/lib/fonts');
$options->set('defaultFont', 'Arial'); // Make sure this matches the font you registered

// Create Dompdf instance
$dompdf = new Dompdf($options);

// Simple HTML content for testing
$html = '
    <html>
        <head>
            <style>
                body {
                    font-family: Arial;
                }
            </style>
        </head>
        <body>
            <h1>This is a test PDF using Arial font</h1>
            <p>Check if the Arial font appears correctly in the PDF.</p>
        </body>
    </html>';

$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();

// Output the generated PDF to the browser
$dompdf->stream("output.pdf", ["Attachment" => false]);
?>
php pdf dompdf
1个回答
0
投票
<?php
require 'vendor/autoload.php';
use Dompdf\Dompdf;
use Dompdf\Options;

// Set Dompdf options
$options = new Options();

$options->set('isHtml5ParserEnabled', true);
$options->set('isPhpEnabled', true); // Enabling PHP inside the HTML (like inline scripts)

//$options->set('fontDir', 'vendor/dompdf/dompdf/lib/fonts');
//$options->set('fontCache', 'vendor/dompdf/dompdf/lib/fonts');
//$options->set('defaultFont', 'Arial'); // Make sure this matches the font you registered


// Create Dompdf instance
$dompdf = new Dompdf($options);

$dompdf->getOptions()->set('fontDir', '/path/to/font/directory');
$dompdf->getOptions()->set('fontCache', '/path/to/font/cache');


// Simple HTML content for testing
$html = '
    <html>
        <head>
            <style>
                body {
                    font-family: Arial;
                }
            </style>
        </head>
        <body>
            <h1>This is a test PDF using Arial font</h1>
            <p>Check if the Arial font appears correctly in the PDF.</p>
        </body>
    </html>';

$dompdf->loadHtml($html);
$font = 'path/to/font.ttf';
$dompdf->getCanvas()->getFontMetrics($font);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();

// Output the generated PDF to the browser
$dompdf->stream("output.pdf", ["Attachment" => false]);
?>
© www.soinside.com 2019 - 2024. All rights reserved.