문제

I converted the .ttf font i need (3 files), and included them inside the tcpdf fonts folder.

It works just fine when i use the classic $pdf->SetFont(); call, but the problem is i dont know how to change font inside the html which is later called using $pdf->writeHTML();

Let us call it the font X.

For example:

$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, '');

What i want, is to use the dejavu sans font through all the document, except for the <span> inside the first <td> tag, where i need the font X. I tried using the font-family property but it does not work so far.

도움이 되었습니까?

해결책

You won't be able to do that with only one writeHTML. You can use DomPDF if you want a real HTML to PDF transform.

Notice than you can do it using several writeHTML() functions, but it will be hard to place correctly your words.

다른 팁

As of 2019, the accepted answer is not correct anymore. In short — it works nowadays!

I've tested the following example in TCPDF 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>Ö)')

I see the following (screenshot)

enter image description here

Note that I've tried writing the font family name as DejaVuSans, dejavusans, Deja Vu Sans, 'Deja Vu Sans' (with quotes) — all of these work, provided the font has the needed characters. I suppose that TCPDF lowercases the font family and strips whitespace from it.

$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');

Basically we need to use tag <font face="bebasneue" ></font> to apply the loaded font into HTML code.

<font face="Arial" >Test تست </font>

change the font inside Html

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top