我正在使用TCPDF库导出音乐网站的专辑/目录发布信息,专辑包含可能是英文,中文或任何其他语言的歌曲标签,我有打印非英语的问题在tcpdf中正确的字符。我想说几点。
Zend framework 1.12
。utf8
。utf8_unicode_ci
。我的方案是信息来自数据库,我使用视图以特定格式显示与相册相关的信息以及HTML
。版本名称是中文奥马尔阿斯拉姆
,我通过搜索stackoverflow尝试了所有选项,这些选项与初始化TCPDF对象时设置字体和更改字符排序有关,但每当我打印它时都会显示名称,如奥马尔阿斯æ‹Â
1}}我尝试围绕HTML中的版本名称utf8_encode()
和utf8_decode()
但没有任何帮助。
另一个例子是拉丁字符,其中专辑的名称为Extraña EP
,当在pdf中打印时,它显示为Extraña EP
。
以下是我在视图中使用的HTML。
<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"> </td>
</tr>
<tr>
<td align="center" valign="middle">
<img src="/images/symphonic_logo.png"/>
</td>
</tr>
</table>
以下是我发送内容以在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();
答案 0 :(得分:0)
以下是我为纠正代码所做的一些事情。
使用自定义字体选项添加Ms arial unicode true type font。
我正在使用css类添加样式标记,其中我在视图中设置了字体系列,如果是非英文字符,则会产生垃圾字符。这是我犯的主要错误。
这是他最后的工作代码
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');
答案 1 :(得分:0)
如果您想使用UTF-8,排序规则无效,它只会确定排序顺序。你可能需要的是设置数据库连接的字符集,这应该在你进行任何查询/插入之前设置:
// 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);
使用此连接读取和写入数据,您应该能够将PDF的编码设置为UTF-8。