简体   繁体   中英

TCPDF - Not displaying UTF-8, latin characters

Im using TCPDF to generate pdf document with latin characters:

//TCPDF
require_once('class/tcpdf.php');
require_once('class/fpdi.php');

$pdf = new FPDI();
$template = "template.pdf";
$pagecount = $pdf->setSourceFile($template);
$tplidx = $pdf->importPage(1);

$pdf->addPage();
$pdf->useTemplate($tplidx, 0, 0);
$pdf->SetFont('freeserif', '', 14, '', false);
$pdf->setFontSubsetting(false);
$pdf->SetTextColor(0,0,0);
$pdf->SetXY(10,18);
$pdf->Cell(0,10,"šđžč",0,1, 'C');

$file_name = str_replace('.','_'.date('Y-m-d').'.',$template);
$pdf->Output($file_name, 'D');

Problem is that all non standard characters are converted to symbol "?"

只需在$pdf->AddPage()之前添加这一行

$pdf->setFont('freeserif');

This is the solution to all the problems about writing special characters using TCPDF php script .

Follow these steps and you will get it solved:

  1. Edit your PHP using ANSI codification . You set it easily with Notepad++ editor.

  2. When you need to write something with TCPDF methods, dont forget enclosing the texts inside utf8_encode() php function.

This way:

$pdf->Cell(210, 18, utf8_encode('Camión/pícaro/rúbrica/áéíóú...  Hello world, this is Aberasturi from the Moon'),0, $ln=0, 'C', 0, '', false, false, 'T', 'C');

Ondo ibili!!!!

X.Aberasturi

From the tcpdf source code:

  • IMPORTANT: Please note that this method sets the mb_internal_encoding to ASCII, so if you are using the mbstring module functions with TCPDF you need to correctly set/unset the mb_internal_encoding when needed.

This just cost me an hour, maybe it helps someone else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM