Laravel DomPDF无法加载Google字体

3

我想使用Google字体,与Laravel DomPDF 0.7.*一起使用,但我很难让它们显示。事实上,我无法让任何自定义字体工作,不仅仅是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/'),

这里是控制器代码:

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();
}

请在.signature的CSS中尝试使用letter-spacing: 1px; - Quynh Nguyen
@QuỳnhNguyễn 这不是正确的字体,字母间距也不是问题。 - Wasim
你可以尝试更新到最新版本,看看是否能解决问题。当前版本是 0.8.0 - Sandeesh
你能添加你的控制器代码吗? - whoacowboy
@whoacowboy 添加了它。 - Wasim
@Wasim,我更新了我的答案,抱歉耽搁了。 - whoacowboy
1个回答

0

字体位置答案

我只能通过将字体放在我的服务器上才能让它们正常工作。

您需要告诉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>

我使用Laravel 5的DOMPDF包装器将其与Laravel集成。这样做要容易得多。

错别字答案

您需要从'Caveat',草书; !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>

不确定为什么会出现语法错误,但这也不是问题所在。同样的问题。 - Wasim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接