簡體   English   中英

如何在使用 DOMPdf 生成的 PDF 中使用 google fonts?

[英]How can I use google fonts in PDFs generated with DOMPdf?

在我的 pdf 模板中,我使用以下樣式:

 @font-face {
    font-family: 'Open Sans', sans-serif;
    font-style: normal;
    font-weight: normal;
    src: url('https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;600;700;800&display=swap');
  }
  table thead tr td{
    font-family: 'Open Sans', sans-serif;
  }
  table tbody tr td{
    font-family: 'Open Sans', sans-serif;
  }

這工作正常。 我在 pdf 中看到了正確的字體。 但是當我想將字體更改為 source sans Pro 字體時,它不起作用。 字體設置為 Arial 而不是 source sans pro:

  @font-face {
    font-family: 'Source Sans Pro', sans-serif;
    font-style: normal;
    font-weight: normal;
    src: url('https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&display=swap');
  }
  table thead tr td{
    font-family: 'Source Sans Pro', sans-serif;
  }
  table tbody tr td{
    font-family: 'Source Sans Pro', sans-serif;
  }

這可能是什么原因? 另一個問題,所有加載粗體的內容現在都設置為正常,即使我刪除了字體粗細。

解決方案之一是將鏈接中的 css 包含到本地文件中。 然后提供正確的路徑以從您的應用程序加載外部 css 字體文件。

$dompdf = new DOMPDF();
$dompdf->setBasePath(realpath(APPLICATION_PATH . '/path/to/googleapis/fonts.css'));
$dompdf->loadHtml($html);
$dompdf->render();

還要檢查https://github.com/dompdf/dompdf/wiki/CSSCompatibility中不同 css 標簽的 css 兼容性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM