简体   繁体   中英

Display non-english characters with TCPDF

I am using the TCPDF library to export Album / Catalog Release information for a music site, the Album consists of songs labels that could be in English, chinese or any other language, I am having a problem to print the non-English character correctly inside the tcpdf. Some points I would like to tell.

  1. I am using Zend framework 1.12 .
  2. Database Connection collation utf8 .
  3. Table field collation utf8_unicode_ci .

My scenario is that the information is coming from the database and I use a view to display information related to the album in a specific format along with the HTML . The release name is in Chinese 奥马尔阿斯拉姆 , I tried all the options by searching on stackoverflow which were related to setting the font and changing character collation while initializing the TCPDF object, but whenever I print it displays the name like this 奥马尔阿斯拠i tried to utf8_encode() and utf8_decode() around the Release Name inside the HTML but nothing helps.

Another is an example is with the Latin characters where the name of the album is Extraña EP and when printed inside the pdf it displays it like Extraña EP .

Here is the HTML I use inside my view.

<table width="100%" cellpadding="0" cellspacing="0">
    <tr>
        <td align="left" valign="top">
            <img src="/images/release_details.png">
        </td>
    </tr>
    <tr>
        <td align="left" valign="top">
            <table width="100%" cellpadding="0" cellspacing="0" class="release_table">
                <tr>
                    <td align="left" valign="top" class="label">CATALOG #:</td>
                    <td align="right" valign="top" class="data"><?=$this->release->Catalog?></td>
                </tr>
                <tr>
                    <td align="left" valign="top" class="label">RELEASE NAME:</td>
                    <td align="right" valign="top" class="data"><?=$this->release->Name?></td>
                </tr>
                <tr>
                    <td align="left" valign="top" class="label">RELEASE TYPE:</td>
                    <td align="right" valign="top" class="data"><?=$releaseType?></td>
                </tr>
                <tr>
                    <td align="left" valign="top" class="label">UPC CODE:</td>
                    <td align="right" valign="top" class="data"><?=$this->release->UPCCode?></td>
                </tr>
                <tr>
                    <td align="left" valign="top" class="label">NUMBER OF SONGS:</td>
                    <td align="right" valign="top" class="data"><?=$this->release->TracksQty?></td>
                </tr>
                <tr>
                    <td align="left" valign="top" class="label">PRIMARY ARTIST:</td>
                    <td align="right" valign="top"
                        class="data"><?=$this->release->PrimaryArtist?></td>
                </tr>
                <tr>
                    <td align="left" valign="top" class="label">LABEL:</td>
                    <td align="right" valign="top" class="data"><?=$this->release->LabelName?></td>
                </tr>
                <tr>
                    <td align="left" valign="top" class="label">MAIN GENRE:</td>
                    <td align="right" valign="top" class="data"><?=$this->release->MainGenre?></td>
                </tr>
                <tr>
                    <td align="left" valign="top" class="label">GENRE:</td>
                    <td align="right" valign="top" class="data"><?=$this->release->Genre?></td>
                </tr>
                <tr>
                    <td align="left" valign="top" class="label">(C):</td>
                    <td align="right" valign="top"
                        class="data"><?=$this->release->CLine . " " . $this->release->CYear?></td>
                </tr>
                <tr>
                    <td align="left" valign="top" class="label">(P):</td>
                    <td align="right" valign="top"
                        class="data"><?=$this->release->PLine . " " . $this->release->PYear?></td>
                </tr>
                <?php if ($this->release->Howlong > 0 && $this->release->EffectiveDate > 0) {?>
                    <tr>
                        <td align="left" valign="top" class="label">EXCLUSIVE DATE:</td>
                        <td align="right" valign="top"
                            class="data"><?=$this->release->EffectiveDate?></td>
                    </tr>
                <?php }?>

                <tr>
                    <td align="left" valign="top" class="label">RELEASE DATE:</td>
                    <td align="right" valign="top" class="data"><?=$this->release->ReleaseDate?></td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td align="center" valign="middle">&nbsp;</td>
    </tr>
    <tr>
        <td align="center" valign="middle">

            <img src="/images/symphonic_logo.png"/>

        </td>
    </tr>
</table>

And here is the code where i call this view before i send the content to print in the pdf.

require_once APPLICATION_PATH . 'library/tcpdf/tcpdf.php';
$pdf = new TCPDF("L", "mm", "A4", false, 'ISO-8859-1', false);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetFont('helvetica', '', 11, '', true);
$pdf->SetMargins(1, -1, -1, false);
//$pdf->SetFont('dejavusans', '', 12);
//whole TCPDF's settings goes here
$this->view->songs = $releaseSongs;
$this->view->release = $releasInformation;
$htmlcontent = $this->view->render('exportpdf.phtml');

// output the HTML content
$pdf->AddPage();
$pdf->writeHTML($htmlcontent, true, false, true, false, '');
$pdf->lastPage();
$pdf->deletePage($pages + 1);
$pdf->Output($filename, 'D');
exit();

here are the few things that i did to correct my code.

  1. used the custom fonts option to add the Ms arial unicode true type font.

  2. i was adding the style tags with css classes where i was setting the font family inside the view which resulted in the garbage characters in case of non-english characters. This was the main mistake i was making.

Here is he final working code

 require_once(APPLICATION_PATH . 'library/tcpdf/tcpdf.php');
    $pdf = new TCPDF("L", "mm", "A4", TRUE, 'utf-8', false);
    $fontname   =   TCPDF_FONTS::addTTFfont(APPLICATION_PATH.'/../library/tcpdf/fonts/ARIALUNI.TTF','TrueTypeUnicode', '', 32);
    $pdf->AddFont($fontname);
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);
    $pdf->setFontSubsetting(true);
    $pdf->SetFont($fontname, '', 12, '', true);
    $pdf->SetMargins(1, -1, -1, false);
    //$pdf->SetFont('dejavusans', '', 12);
    //whole TCPDF's settings goes here


    $htmlcontent = $this->view->render('exportpdf.phtml');

    // output the HTML content
    $pdf->AddPage();
    $pdf->writeHTML($htmlcontent, true, false, true, false, '');
    $pdf->lastPage();
    $pdf->deletePage($pages + 1);
    $pdf->Output($filename, 'D');

The collation doesn't help if you want to work with UTF-8, it only determines the sorting order. What you propably need is to set the charset of the database connection, this you should set before you do any query/inserts:

// tells the mysqli connection to deliver UTF-8 encoded strings.
$db = new mysqli($dbHost, $dbUser, $dbPassword, $dbName);
$db->set_charset('utf8');

// tells the pdo connection to deliver UTF-8 encoded strings.
$dsn = "mysql:host=$dbHost;dbname=$dbName;charset=utf8";
$db = new PDO($dsn, $dbUser, $dbPassword);

Reading and writing your data with this connection, you should be able to set the encoding of your PDF to UTF-8.

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