Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To utilize Unicode in conjunction with TCPDF, follow these steps:

  1. Set the encoding of your PHP file to UTF-8 by including the following code at the top of your PHP file:
header("Content-Type: text/html; charset=UTF-8");
  1. Set the TCPDF global font to a Unicode font that supports the characters you need. For example, to use the DejaVu Sans Unicode font, include the following code before creating your TCPDF object:
// 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');
  1. Use the Unicode font in your PDF by setting the font and encoding of your text using the 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.