HTML2PDF with TCPDF not rendering Chinese characte

2019-08-15 05:56发布

问题:

I am starting a new thread for this one, will try to be as specific as possible.

This is a portion of an app for our sales people that allows them to customize a page with their contact information, after they hit submit, a PDF page is created which is then attached to the end of a larger document.

The first page of the document is also dynamically created from the same form that they fill out. After they press submit, 2 PDF files are created, a front page and a back page, then both pages are attached to a larger document creating a single report.

Everything works great unless they enter Chinese characters then all I get is garbage characters like this - (质釕俕试æμ•ç¨‹). The document still looks like this even before it is merged with the larger document.

I have tried every possible scenario (it seems) from using a Unicode font, to using my own created font and one by one trying each of the fonts in the TCPDF folder. I am at a total loss, and have been for some time now. I feel like I have been on just about every website that mentions TCPDF.

Here is the code that creates the back page, back to the basic commands I started with. I am hoping it is a simple command down here that I am missing:

require_once(dirname(__FILE__).'/html2pdf.class.php');
try
{
    $html2pdf = new HTML2PDF('L','LETTER','en', false, 'ISO-8859-15', array(mL, mT, mR, mB)); 
    $html2pdf->setDefaultFont('Arial');
    $html2pdf->writeHTML($html, isset($_GET['vuehtml']));

//Line below saves PDF to file
    $html2pdf->Output('created/'.$lastname[1].'_html2pdf.pdf', 'F');

}

catch(HTML2PDF_exception $e) {
    echo $e;

}

And for what it is worth, running a Centos 5.5 server to create the documents. Thank you in advance.


After doing some investigation on this issue with HTML2PDF, I was told that the font arialuni.ttf should support all of the characters I need. With that being said, HTML2PDF will not load this font when I use it in my script seen below.

require_once(dirname(__FILE__).'/html2pdf.class.php');
try
{

    $html2pdf = new HTML2PDF('L','LETTER','en', false, '', array(mL, mT, mR, mB));      
    $html2pdf->pdf->AddTTFFont('ARIALUNI.TTF');
    $html2pdf->pdf->setFont('arialuni');
    $html2pdf->writeHTML($html, isset($_GET['vuehtml']));

    //Line below sends output to the screen only
    //$html2pdf->Output('exemple00.pdf');

    //Line below saves PDF to file
   $html2pdf->Output('created/'.$lastname[1].'_html2pdf.pdf', 'F');

}

catch(HTML2PDF_exception $e) {
    echo $e;

}

*All I can think is that the syntax is not 100% correct. I have ran the tt2fm and makefont to get the files but do not feel that those routines were a 100% successful.

Please help - I have been working on this thing off and on for months. I am almost ready to just scrap it and hit the door.

Thank you.

回答1:

According to your codes:

require_once(dirname(__FILE__).'/html2pdf.class.php');
try
{
    $html2pdf = new HTML2PDF('L','LETTER','en', false, 'ISO-8859-15', array(mL, mT, mR, mB));
    //$html2pdf->setDefaultFont('Arial');  //Please remove this
    $html2pdf->setDefaultFont('stsongstdlight'); //And change with this, it works 101% for me
    $html2pdf->writeHTML($html, isset($_GET['vuehtml']));

//Line below saves PDF to file
    $html2pdf->Output('created/'.$lastname[1].'_html2pdf.pdf', 'F');

}

catch(HTML2PDF_exception $e) {
    echo $e;

}

...And here is my 101% working codes to have the CHINESE characters in pdf output:

<?php
require_once ROOT_DIRECTORY.'/{it's your own location where you put the class at}/html2pdf-4.4.0/html2pdf.class.php'; //ROOT DIRECTORY is my own "defined" so do not use this line.
/* or use this:
* require_once(dirname(__FILE__).'/html2pdf.class.php');
* it depends on what you need
*/ 
try
{
    $html2pdf = new HTML2PDF('L', 'A6', 'en', true, 'UTF-8', 0); //I'm using UTF-8 //Also I'm working on A6 page in my project here, so if you need to have A4 size change A6 back to A4 or change to the size you need
    $html2pdf->pdf->SetDisplayMode('fullpage');
    $html2pdf->setDefaultFont('stsongstdlight'); //this is all you need to have the CHINESE character.
    $content = ob_get_clean();
    $html2pdf->writeHTML($content);

    $html2pdf->Output('Sticker-'.$this->invNumber.'-'.$this->custCode.'.pdf');
    exit;
}
catch(HTML2PDF_exception $e) {
    echo $e;
    exit;
}

Hopefully it works TOO in your codes there!

Happy coding :)



回答2:

You can render Chinese character in your PDF document by using

cid0jp font in TCPDF.

For your reference :-

PHP Code :-

http://www.tcpdf.org/examples/example_038.phps

Generated PDF :-

http://www.tcpdf.org/examples/example_038.pdf

Please ensure file cid0jp.php should be in font directory.

tcpdf/fonts/cid0jp.php 


回答3:

$html2pdf = new HTML2PDF('L','LETTER','en', true, 'UTF-8', array(mL, mT, mR, mB));


回答4:

So html2pdf is based on tcpdf, but they are a little bit different: html2pdf does not support Chinese language.

However, you can get the javiergb.php from the include directory /tcpdf/fonts/. Then you copy this file to the html2pdf/_tcpdf5.xxxx/fonts/ directory.

Afterwards, you just have to add the line $html2pdf->setDefaultFont('javiergb'); to your html2pdf set up as following:

$html2pdf = new HTML2PDF('P', 'A4', 'en');
$html2pdf->setDefaultFont('javiergb');
$html2pdf->writeHTML($html);
$html2pdf->Output('example_zh_cn.pdf');

You can look my fork: https://github.com/cychai/html2pdf