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 / chines or any other language , i am having problem to print the non-english character correctly inside the tcpdf. Some points i would like to tell.

  • I am using zend framework 1.12 .
  • Database Connection collation utf8 .
  • 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 stackover flow 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 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.

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

  • 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.

    链接地址: http://www.djcxy.com/p/88682.html

    上一篇: 印地文在TCPDF中不能正确显示

    下一篇: 英文字符与TCPDF