【问题标题】:TCPDF change font inside htmlTCPDF更改html内的字体
【发布时间】:2014-06-06 22:23:51
【问题描述】:

我转换了我需要的.ttf 字体(3 个文件),并将它们包含在 tcpdf 字体文件夹中。

当我使用经典的 $pdf->SetFont(); 调用时它工作得很好,但问题是我不知道如何更改 html 中的字体,后来使用 $pdf->writeHTML(); 调用它

我们称它为X字体。

例如:

$pdf->SetFont('DejaVu Sans','',10);

$html = '
<table>
<tr>
<td><span style="font-family:X;"></span></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
';

$pdf->writeHTML($html, true, false, true, false, '');

我想要的是在所有文档中使用 dejavu sans 字体,除了第一个 &lt;td&gt; 标签内的 &lt;span&gt;,我需要字体 X。我尝试使用 font-family 属性,但到目前为止它不起作用。

【问题讨论】:

  • 只有一个 writeHTML 无法做到这一点。如果你想要一个真正的 HTML 到 PDF 的转换,你可以使用 DomPDF。
  • @VincentDecaux 如果您希望我接受,请将其写为答案。

标签: php html fonts tcpdf


【解决方案1】:

您将无法仅使用一个 writeHTML 来做到这一点。如果你想要一个真正的 HTML 到 PDF 的转换,你可以使用 DomPDF。

请注意,您可以使用几个 writeHTML() 函数来完成,但很难正确放置您的单词。

【讨论】:

    【解决方案2】:

    &lt;font face="Arial" &gt;Test تست &lt;/font&gt;

    更改 Html 中的字体

    【讨论】:

      【解决方案3】:

      截至 2019 年,接受的答案不再正确。简而言之 - 它现在可以使用!

      我在 TCPD 6.2.9 中测试了以下示例:

      $pdf->AddFont('PTSans', '', 'ptsans.php');
      $pdf->AddFont('DejaVuSans', '', 'dejavusans.php')
      
      /* Setting the font family: */
      $pdf->setFont('PTSans', '', 9);
      
      /* outputting with writeHTMLCell, which internally calls MultiCell(), 
       * which in turn uses writeHTML() for HTML 
       */
      $pdf->writeHTMLCell(100, 10, '', '', '<span style="font-family:DejaVuSans;">А</span>A (<span style="font-family:DejaVuSans;">Ö</span>Ö)')
      

      我看到以下内容(截图)

      请注意,我尝试将字体系列名称写为 DejaVuSans、dejavusans、Deja Vu Sans、'Deja Vu Sans'(带引号)——只要字体具有所需的字符,所有这些都可以工作。我想 TCPDF 将字体系列小写并从中去除空格。

      【讨论】:

        【解决方案4】:
        $pdf = new TCPDF('P', 'px', $defaultSize, true, 'UTF-8', false);
        $pdf->SetCreator('abcd.com');
        $pdf->SetAuthor('Viraj');
        $pdf->SetTitle('HTML Pages');
        $pdf->SetMargins(0,0,0);
        $pdf->SetHeaderMargin(0);
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
        $pdf->setPrintFooter(false);
        $pdf->setPrintHeader(false);
        $pdf->SetAutoPageBreak(false, 0);
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
        $pdf->SetAutoPageBreak(true, 0);
        $pdf->AddPage(null;
        $pdf->setPageMark();
        $pdf->SetFontSize(12);
        
        //Load font front file path url
        $fontname = TCPDF_FONTS::addTTFfont('/Users/Work/storage/app/public/Arial regular.ttf', '', '', 32);
        echo $fontname;//"arial" use this font in html code
        $pdf->AddFont($fontname, '', 14, '', false);
        
        //Load font front file path url
        $fontname_ = TCPDF_FONTS::addTTFfont('/Users/Work/storage/app/public/BebasNeue-Regular.ttf', '', '', 32);
        echo $fontname;//"bebasneue" use this font in html code
        $pdf->AddFont($fontname_, '', 14, '', false);
        
        //HTML code start
        $html = '<table border="0" cellpadding="10" style="width: 100%;"  >';
        $html .= '<tr nobr="true" >';
        $html .= '<td>';
        
        //use loaded font here
        $html .= '<font face="arial"  >The formatting rules are not configurable but are already optimized for the best possible output.</font>';
        //use loaded font here
        $html .= '<font face="bebasneue"  >The formatting rules are not configurable but are already optimized for the best possible output.</font>';
        
        $html .= '</td>';
        $html .=   "</tr>";
        $html .=   "</table>";
        
        //write html into pdf
        $pdf->writeHTML($html);
        
        //save file in output path
        $filePath = storage_path('app/public/samplePDF.pdf");  
        echo "PDF File - ".$filePath;
        $pdf->Output($filePath, 'F');
        

        基本上我们需要使用标签&lt;font face="bebasneue" &gt;&lt;/font&gt;将加载的字体应用到HTML代码中。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-03
          • 2013-10-21
          • 1970-01-01
          • 2021-10-21
          • 2021-06-11
          • 2020-02-24
          相关资源
          最近更新 更多