php - Laravel DomPDF Google 字体未加载

标签 php laravel dompdf

尝试在 Laravel DomPDF 0.7.* 中使用 Google 字体,但我很难让它们显示出来。事实上,我无法使用任何自定义字体,不仅仅是 Google 字体。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link href="https://fonts.googleapis.com/css?family=Caveat" rel="stylesheet">
        <style type="text/css">

            .signature {
                font-family: 'Caveat', cursive !important;
                font-size: 30px;
            }
        </style>
    </head>

    <body>
        <p class="signature">Name</p>
    </body>
</html>

出现的是这个,应该是手写字体。

enter image description here

dompdf.php 配置文件中,字体目录和字体缓存都是可写的。

"DOMPDF_FONT_DIR" => storage_path('app/tmp/'), 
"DOMPDF_FONT_CACHE" => storage_path('app/tmp/'),

Controller 代码如下:

public function download(Request $request, string $contractKey)
{
    $item = Item::findOrFail($contractKey);

    $body = @$item->contract;

    $pdf = PDF::loadView('pdf.contract', [
        'body'  => $this->cleanContract($body),
        'docId' => md5($item->id),
        'item'  => $item,
        'ip'    => $ip = @$request->ips()[1] ?: $request->ip(),
    ]);

    $pdf->output();
    $dom_pdf = $pdf->getDomPDF();

    $canvas = $dom_pdf->get_canvas();
    $canvas->page_text($canvas->get_width() - 70, $canvas->get_height() - 30, "Page {PAGE_NUM} of {PAGE_COUNT}", null, 10, [0, 0, 0]);


    return $pdf->stream();
}

最佳答案

字体位置答案

我只能通过在我的服务器上安装字体来实现它。

您需要告诉 DomPDF 字体在哪里。

public function download(Request $request, string $contractKey)
{
    $item = Item::findOrFail($contractKey);

    $body = @$item->contract;

    $pdf = PDF::loadView('pdf.contract', [
        'body'  => $this->cleanContract($body),
        'docId' => md5($item->id),
        'item'  => $item,
        'ip'    => $ip = @$request->ips()[1] ?: $request->ip(),
    ]);

    $pdf->output();
    $dom_pdf = $pdf->getDomPDF();

    // set font dir here
    $dom_pdf->set_option('font_dir', storage_path('fonts/'));

    $canvas = $dom_pdf->get_canvas();
    $canvas->page_text(
        $canvas->get_width() - 70, 
        $canvas->get_height() - 30, 
        "Page {PAGE_NUM} of {PAGE_COUNT}", null, 10, [0, 0, 0]);    
    return $pdf->stream();
}

将字体下载到storage/fonts

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <style type="text/css">

            .signature {
                font-family: 'Caveat', cursive;
                font-size: 30px;
            }
        </style>
    </head>

    <body>
        <p class="signature">Name</p>
    </body>
</html>

我使用 DOMPDF Wrapper for Laravel 5将其与 Laravel 集成。这要容易得多。

错字答案

您需要从 'Caveat', cursive 中删除 !important; !important; 参见 jsfiddle .多了一个分号。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link href="https://fonts.googleapis.com/css?family=Caveat" rel="stylesheet">
        <style type="text/css">

            .signature {
                font-family: 'Caveat', cursive;
                font-size: 30px;
            }
        </style>
    </head>

    <body>
        <p class="signature">Name</p>
    </body>
</html>

关于php - Laravel DomPDF Google 字体未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44004167/

相关文章:

php - 如何在 Apache 中将 php 文件显示为纯文本

php - 为 Amazon SES 服务附加自定义电子邮件 header

php - 从下拉菜单中选择选项后无法更新记录

php - 如何在 Heroku 上部署 Laravel 4 应用程序?

php - 如何同时生成使用 DOMPDF 生成的 PDF 并通过电子邮件发送?

php - 在 PHP 中使用 DOMPDF 的 PDF 页面中的页眉

php - 将 MySQL 代码和 PHP 代码放在不同的文件中有何好处?

php - Laravel Forge - Composer 问题

php - Laravel - 按数字(整数)排序​​,即使列类型是字符串

html - DOMPDF - 有序列表 - 计数器问题