1 | initial version |
To utilize Unicode in conjunction with TCPDF, follow these steps:
header("Content-Type: text/html; charset=UTF-8");
// Include the TCPDF library
require_once('/path/to/tcpdf/tcpdf.php');
// Set the TCPDF global font to a Unicode font
TCPDF_FONTS::addTTFfont('/path/to/dejavu-sans/DejaVuSans.ttf', 'TrueTypeUnicode', '', 32);
TCPDF_FONTS::addTTFfont('/path/to/dejavu-sans/DejaVuSans-Bold.ttf', 'TrueTypeUnicode', '', 32);
TCPDF_FONTS::addTTFfont('/path/to/dejavu-sans/DejaVuSans-Oblique.ttf', 'TrueTypeUnicode', '', 32);
TCPDF_FONTS::addTTFfont('/path/to/dejavu-sans/DejaVuSans-BoldOblique.ttf', 'TrueTypeUnicode', '', 32);
TCPDF_FONT::setGlobalFont('dejavusans');
SetFont()
and SetFontSubset()
methods of TCPDF. For example: // Create a new TCPDF object
$pdf = new TCPDF();
// Set the font and encoding to use Unicode
$pdf->SetFont('dejavusans', '', 14, '', true);
$pdf->SetFontSubset('dejavusans');
// Add some text to the PDF
$pdf->Cell(0, 10, 'Hello, 世界', 0, 1);
// Output the PDF
$pdf->Output('example.pdf', 'D');
In the example above, we set the font to use DejaVu Sans Unicode, and set the encoding to use Unicode by calling SetFontSubset('dejavusans')
.
This will allow TCPDF to properly handle Unicode characters in your PDF.