DOMPDF 自定义字体

问题描述

我正在尝试在 DomPDF 中加载自定义字体,但没有成功,结果还是一样。

这就是我使用字体的方式:

require_once __DIR__ . '/dompdf/lib/html5lib/Parser.PHP';

// Sabberworm
spl_autoload_register(function($class)
{
    if (strpos($class,'Sabberworm') !== false) {
        $file = str_replace('\\',DIRECTORY_SEParaTOR,$class);
        $file = realpath(__DIR__ . '/lib/PHP-css-parser/lib/' . (empty($file) ? '' : DIRECTORY_SEParaTOR) . $file . '.PHP');
        if (file_exists($file)) {
            require_once $file;
            return true;
        }
    }
    return false;
});

// PHP-font-lib
require_once __DIR__ . '/dompdf/lib/PHP-font-lib/src/FontLib/Autoloader.PHP';

//PHP-svg-lib
require_once __DIR__ . '/dompdf/lib/PHP-svg-lib/src/autoload.PHP';


/*
 * New PHP 5.3.0 namespaced autoloader
 */
require_once __DIR__ . '/dompdf/src/Autoloader.PHP';

Dompdf\Autoloader::register();

use Dompdf\Dompdf;
use Dompdf\Options;


//PDF
$html = '
<html>
    <head>
        <title>Test</title>
        <Meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <style>
            @font-face {
                font-family: "Roboto";
                src: url("https://' . $_SERVER['SERVER_NAME'].'/dompdf/lib/fonts/Roboto-Regular.ttf") format(\'truetype\');
                font-weight: normal;
                font-style: normal;
            }

            * {
                font-family: "Roboto";
            }

        </style>
    </head>
    <body>
        <header>
            <h1>Test</h1>
        </header>
        <div class="content">
            <h2>Test</h2>
            <p>Test</p>
        </div>

        <div class="footer">
            <h3>Test</h3>
            <p>test</p>
        </div>
    </body>
</html>
';

$options = new Options();
$options->setIsRemoteEnabled(true);
$options->isHtml5ParserEnabled(true);

$dompdf = new Dompdf($options);

$dompdf->loadHtml($html);

$dompdf->setPaper('A4','landscape');

$dompdf->render();
    
$dompdf->stream('Test.pdf',array("Attachment" => false));  

echo $html;

我正在使用 DomPDF 的自定义安装,我按照 PHPDOM github 页面上的 git 安装说明进行操作。我还尝试手动安装 font-loader.PHP 文件,但它总是因大量内存使用而崩溃(我将 PHP.ini 更新为使用 512MB,结果相同)......请求的字体文件存在,并且当我使用该代码时不导出为 PDF,它按预期工作......

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)