You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel中Dompdf生成PDF时Bookman Old Style字体不生效求助

问题:Dompdf生成PDF时Bookman Old Style字体未生效

我用Dompdf生成报表,要求使用Bookman Old Style字体,但PDF中该字体未生效,仅在HTML预览时显示正常。以下是我的代码:

PHP代码

function print_ta()
{
    //return view('calendaractivities.calendar_activities.generate_ta');

    // Create a new instance of Dompdf
    $dompdf = new Dompdf();


    // Set options for Dompdf
    $options = new Options();
    $options->setIsHtml5ParserEnabled(true);
    $options->setIsRemoteEnabled(true);
    $dompdf->setOptions($options);

    // Render the view file and convert it to HTML
    $html = view('calendaractivities.calendar_activities.generate_ta')->render();

    // Load the HTML into Dompdf
    $dompdf->loadHtml($html);

    // Render the PDF
    $dompdf->render();

    // Output the generated PDF to the browser
    $dompdf->stream('document.pdf', ['Attachment' => false]);
}

HTML代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html>

      <head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Header & Footer test</title>

<style>
    @page {
        margin: 10mm;
    }

    body {
        line-height: 1.3;
        /* Avoid fixed header and footer to overlap page content */
        margin-top: 10px;
        /* Adjust this value as needed */
        margin-bottom: 50px;
    }

    .first-page #header {
        position: fixed;
        top: -105px;
        left: -90px;
        width: 100%;
        height: 100px;
        /* For testing */
    }

    .other-pages #header {
        display: none;
    }

    #footer {
        position: fixed;
        bottom: 0;
        left: -90px;
        width: 100%;
        height: 70px;
        font-size: 6pt;
        color: #777;
        /* For testing */
        background: red;
    }

    /* Print progressive page numbers */
    .page-number:before {
        /* counter-increment: page; */
        content: "Pagina "counter(page);
    }

    /* table css 1 px solid black and collapse all borders */
    table {
        border-collapse: collapse;
        border: 1px solid black;
    }

    /* add border to tr th and td */
    table tr th,
    table tr td {
        border: 1px solid black;
    }

    .th_header {
        height: 2cm;
        font-size: 16.66px;
        text-decoration: underline;
        font-family: "Bookman Old Style", serif !important;
        color: red;
    }
</style>
 </head>

<body>
<div class="first-page">
    <header id="header">
        
        <img src="{{ url('images/header_image.png') }}" width="123%" height="130%">
    </header>
</div>

<div class="other-pages">
    <header id="header">
        {{-- add an empty header for other pages --}}
    </header>
</div>

<footer id="footer">
    <img src="{{ url('images/footer_image.png') }}" width="123%">
</footer>

<div id="content">
    <br>
    <table style="width:100%">
        <tr>
            <th class="th_header" colspan="4">
                AUTHORITY TO TRAVEL</th>
        </tr>
    </table>
</div>
</body>

</html>

解决方案

Dompdf不会自动识别并嵌入系统中的非标准字体,必须手动配置字体加载或通过@font-face引入字体文件,确保PDF能正确嵌入目标字体。

方法1:通过@font-face引入字体文件

  1. 下载Bookman Old Style字体的TTF/OTF格式文件(如BookmanOldStyle.ttf),放置到项目公共资源目录,例如public/fonts/。
  2. 在HTML的<style>标签中添加@font-face规则:
@font-face {
    font-family: 'Bookman Old Style';
    src: url('{{ url('fonts/BookmanOldStyle.ttf') }}') format('truetype');
    font-weight: normal;
    font-style: normal;
}
  1. 确认你的PHP代码中已开启isRemoteEnabled(代码中已设置$options->setIsRemoteEnabled(true);,无需额外修改),确保Dompdf能加载字体文件。

方法2:配置Dompdf加载系统字体

若服务器已安装Bookman Old Style字体,可直接配置Dompdf读取系统字体目录:

  1. 在PHP代码中添加字体目录配置:
$options->setFontDir('/path/to/system/fonts/'); // 替换为服务器字体实际路径,如Linux的/usr/share/fonts/、Windows的C:/Windows/Fonts/
$options->setFontCache(storage_path('fonts')); // 设置字体缓存目录,确保目录有写入权限
  1. 配置完成后,即可直接在CSS中使用font-family: "Bookman Old Style", serif;。

验证要点

  • 检查字体文件路径是否正确,可通过浏览器直接访问字体URL测试。
  • 生成PDF后,查看PDF属性中的字体列表,确认Bookman Old Style已被嵌入。
  • 确保@font-face定义的字体名称与CSS引用的名称完全一致,避免因名称不匹配导致失效。

内容的提问来源于stack exchange,提问作者beginner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 19:40:06