How to embed fonts into a PDF with TCPDF?

蓝咒 提交于 2019-12-25 19:02:33

问题


I have tried to use TCPDF whose code is given below:

require_once($_SERVER['DOCUMENT_ROOT'].'/tcpdf/tcpdf.php'); 
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 061');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
if (@file_exists($_SERVER['DOCUMENT_ROOT'].'/tcpdf/config/lang/eng.php')) {
    require_once($_SERVER['DOCUMENT_ROOT'].'/tcpdf/config/lang/eng.php');
    $pdf->setLanguageArray($l);
}
$pdf->SetFont('helvetica', '', 10); 
$html ="";
$pdf->AddPage();
$html .='<style>'. file_get_contents('http://localhost/Projects/PDF/css/style_4.css').'</style>';
$html .= file_get_contents("http://localhost/Projects/PDF/mycon.php"); 
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->lastPage();
$pdf->Output('example_061.pdf', 'I');

The above code generates a PDF successfully but is not taking my CSS and fonts that I have defined into "style_4.css" and included externally.

I have also tried to include CSS on the page itself "mycon.php" but its not taking from there as well. What am I doing wrong?

Note: I have different fonts which will be dynamically attached with layout based on certain conditions.


回答1:


See TCPDF's documentation:

Using the addTTFfont() method you can directly create a TCPDF font starting from a TrueType, OpenType or Type1 font. NOTE: The 'fonts' folder must be writeable by the webserver.

For example:

$fontname = $pdf->addTTFfont('/path-to-font/DejaVuSans.ttf', 
                                'TrueTypeUnicode', '', 32);


来源:https://stackoverflow.com/questions/22663625/how-to-embed-fonts-into-a-pdf-with-tcpdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!