TCPDF UTF-8 符号未显示

发布于 2024-10-22 05:50:02 字数 151 浏览 6 评论 0原文

我使用最新的 TCPDF 版本(5.9)。但在编码方面遇到一些奇怪的问题。我需要立陶宛语语言符号,例如:ąčęėįšųūž。但只能得到其中的一小部分。其他的还是这样???? 那我该怎么办呢?我使用默认的 times 字体(它带有 TCPDF 下载)。

任何帮助将不胜感激。

Im using latest TCPDF version(5.9). But have some strange problems with encoding. I need Lithuanian language symbols like: ąčęėįšųūž. But get only few of it. Other remain like ?????
So what should I do ? I use default times font(it comes with TCPDF download).

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

森林散布 2024-10-29 05:50:02

TCPDF 对于 utf8 来说相当棘手。实现您想要的效果的最佳方法是将字体嵌入到生成的 PDF 文件本身中。您可以使用 TCPDF 包中的 freeserif 字体,它包含所有 utf8 符号,绝对显示任何语言的任何字符,但会向输出文件添加约 700kb。如果文件大小不重要,这可能是获取所需符号的最简单方法。

您还可以制作自己的嵌入字体,其中包含您需要的字符。这可能是最好的解决方案,保持通用性和小尺寸,但更复杂。

或者,您可以使用从系统中获取的核心字体,如果找不到,则用替代品替换。这使得输出文件非常轻,但增加了字体子集以获得外来字符的必要性。就我个人而言,我在这方面还没有取得成功,所以我仍然认为嵌入字体是最好的解决方案,而且它也恰好更通用。

TCPDF is quite tricky with utf8. Best way to achieve what you want is to embed the font in generated PDF file itself. You can use freeserif font from the TCPDF package, it contains all the utf8 symbols, shows absolutely any character of any language, but adds ~700kb to the output file. That's probably the easiest way to get symbols you need if file size doesn't matter.

You could also make your own font to embed, containing the characters you need. That's probably the best solution, keeping it universal and small in size, but is more complex.

Alternatively, you can relay on core fonts, which are taken from the system, and if not found, replaced by a substitute. This makes output file extremely light, but adds the necessity of font subsetting to obtain exotic chars. Personally I haven't had a success with this, so I still think embedding font is the best solution, which also happens to be more universal..

西瑶 2024-10-29 05:50:02

CPDF 核心字体中包含一种字体 - dejavusans,它显示所有立陶宛字符。只需添加以下内容:

$pdf->setHeaderFont(Array('dejavusans', '', 10, '', false));
$pdf->setFooterFont(Array('dejavusans', '', 8, '', false));
$pdf->SetFont('dejavusans', '', 10, '', false);

there is a font included in the CPDF core fonts - dejavusans, it shows all the lithuanian characters. Just add the following:

$pdf->setHeaderFont(Array('dejavusans', '', 10, '', false));
$pdf->setFooterFont(Array('dejavusans', '', 8, '', false));
$pdf->SetFont('dejavusans', '', 10, '', false);
小鸟爱天空丶 2024-10-29 05:50:02

将 TCPDF 构造函数上的 $unicode 参数设置为 false,并将 $encoding 参数设置为 'ISO-8859-1' 或其他一些字符映射表。

这个< /a> 将帮助您:

UTF-8 unicode 的默认值:

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

欧洲字符集的构造函数示例:

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'ISO-8859-1', false);

Set the $unicode parameter on the TCPDF constructor to false and the $encoding parameter to 'ISO-8859-1' or some other character map.

This will help you:

Default for UTF-8 unicode:

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

Example of constructor for European charset:

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'ISO-8859-1', false);
﹂绝世的画 2024-10-29 05:50:02

将字体设置为 freeserif 就可以了。
我测试过。

$pdf->SetFont('freeserif', '', 14, '', true);

Set font to freeserif it will work.
I tested.

$pdf->SetFont('freeserif', '', 14, '', true);
安静被遗忘 2024-10-29 05:50:02

在尝试使用默认 Helvetica 字体渲染罗马尼亚语文本时刚刚发现了同样的情况。在进行一些调查时,我发现 tcpdf 库将其默认字体(称为“核心”字体)视为 Latin1 字符,因此即使您告诉它使用 UTF-8 编码并设置 unicode 标志,它也会逐字翻译您的文本在渲染之前转换为 Latin1 等效项。该库的默认行为是,如果它找到 Latin1 等效项,则翻译它可以找到等效项的每个字符,否则它将将该字符翻译为“?”。

这可以在 TCPDF 类的以下方法链中找到:
Write() -> Cell() -> getCellCode() -> _escapetext()

_escapetext() 内部,您可以看到它正在检查 $this->isunicode,然后检查所选字体以查看其类型是否为 core|TrueType|类型1。如果是,它将通过 UTF8ToLatin1() 方法对字符串进行“拉丁化”。这就是“?”的地方。翻译正在进行中。

我的建议是使用与您使用的默认字体类似的自定义 unicode 字体(例如 Deja Vu Sans)。在我目前的情况下,这对我有用。

Just discovered this same situation when trying to render Romanian text using the default Helvetica font. In doing some investigation I found that the tcpdf library treats it's default fonts (referred to as "core" fonts) as Latin1 characters so even if you tell it to use UTF-8 encoding and set the unicode flag, it will literally translate your text to Latin1 equivalents prior to rendering. The default behavior of the library is, if it finds a Latin1 equivalent, to translate each character that it can find an equivalent for otherwise it translates the character as '?'.

This can be found inside the TCPDF class in the following method chain:
Write() -> Cell() -> getCellCode() -> _escapetext().

Inside of _escapetext() you can see it is checking for $this->isunicode then checking the selected font to see if it's type is core|TrueType|Type1. If it is, it will take the string an "latinize" it for you by way of the UTF8ToLatin1() method. This is where the '?' translations are taking place.

My recommendation would be to use a custom unicode font (like Deja Vu Sans) that is similar to the default font you are after. That worked for me in my current situation.

痴梦一场 2024-10-29 05:50:02

将 TCPDF 与特殊字符(例如 ฿、포 或其他字符)一起使用
您需要使用 unicode 字体:

  1. 在此处下载字体:
    ftp://ftp.fu-berlin.de/unix/ X11/multimedia/MPlayer/contrib/fonts/arialuni.ttf.bz2

  2. 创建一个测试 pdf 文件并将该字体加载到 TCPDF 中
    示例:

    $fontname = $pdf->addTTFfont('/var/www/app/images/fonts/arialuni.ttf', 'TrueTypeUnicode', '', 32);

  3. this将创建如下字体:

    <块引用>

    application/libraries/tcpdf/fonts/arialuni.ctg.z
    应用程序/库/tcpdf/fonts/arialuni.php
    应用程序/库/tcpdf/fonts/arialuni.z

  4. 现在您可以使用以下命令设置新字体:
    $pdf->SetFont('arialuni', '', 10.5);

  5. 现在您可以使用特殊的 unicode 字符,如 ฿ 等...

来源:
http://myridia.com/dev_posts/view/852

To use TCPDF with special characters like ฿, 포 or others
you need to use a unicode font:

  1. download the font here:
    ftp://ftp.fu-berlin.de/unix/X11/multimedia/MPlayer/contrib/fonts/arialuni.ttf.bz2

  2. create a test pdf file and load this font into TCPDF
    example:

    $fontname = $pdf->addTTFfont('/var/www/app/images/fonts/arialuni.ttf', 'TrueTypeUnicode', '', 32);

  3. this will create the fonts like:

    application/libraries/tcpdf/fonts/arialuni.ctg.z
    application/libraries/tcpdf/fonts/arialuni.php
    application/libraries/tcpdf/fonts/arialuni.z

  4. now you can set the new font with :
    $pdf->SetFont('arialuni', '', 10.5);

  5. and now you can use special unicode characters like ฿ and more....

Source :
http://myridia.com/dev_posts/view/852

初心未许 2024-10-29 05:50:02

您无法从数据库中读取像 Karnātaka 这样的字符并像这样显示 karn?taka 我的意思是 "?" 我们不想要然后执行以下操作:

  1. 定义连接的字符集(mysql_set_charset()):

    $con = mysql_connect("localhost","root","");
    
    如果 (!$con)
    {
        die('无法连接:' .mysql_error());
    }
    mysql_select_db("database_name", $con) 或 die(mysql_error());
    mysql_set_charset('utf8',$con);
    
  2. 使用 $pdf->SetFont('DejaVuSerif', '', 10); 而不是 $pdf->SetFont('helvetica', 'B', 12);

    • 对于 PHP 的 TCPDF 库,从数据库中读取类似 Rājasthān 的字符,而不是 R?jasth?n

You u have problem to read character like Karnātaka from database and display like this karn?taka I mean "?" which we don't want then do following things :

  1. Define charset for the connection (mysql_set_charset()):

    $con = mysql_connect("localhost","root","");
    
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("database_name", $con) or die(mysql_error());
    mysql_set_charset('utf8',$con);
    
  2. Use $pdf->SetFont('DejaVuSerif', '', 10); instead of $pdf->SetFont('helvetica', 'B', 12);

    • For TCPDF Library of the PHP read character like Rājasthān instead of R?jasth?n from database
沧桑㈠ 2024-10-29 05:50:02

IIRC,您可以在创建新字体时定义编码,如此处所述。否则,您必须使用创建字体时定义的编码。听起来 TCPDF 附带的字体都使用 WinAnsiEncoding...又名代码页 1252。

笨拙,但有效。

IIRC, you can define an encoding when you create a new font, as described here. Otherwise, you have to use the encoding that was defined when the font was created. It sounds like the fonts that ship with TCPDF all use WinAnsiEncoding... a.k.a. code page 1252.

Clunky, but effective.

乖乖公主 2024-10-29 05:50:02

使用默认 TCPDF 包测试了 dejavusansfreeserif,这两种字体都适用于立陶宛字符。我还输入了一些俄语字符,它们也起作用了。我用这段代码来测试它:

$this->pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); 
$this->pdf->AddPage();
$this->pdf->SetFont('dejavusans', 'B', 20); // UTF8 fonts with lithuanian support: freeserif, dejavusans
$this->pdf->Write(0, 'ąžūčšęėųįĄŽŪČŠĘĖŲĮ Превед Кросавчег!', '', 0, 'C', true, 0, false, false, 0);

With default TCPDF package tested dejavusans and freeserif and both fonts works with lithuanian characters. I also typed few russian characters and they worked too. I used this code to test it:

$this->pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); 
$this->pdf->AddPage();
$this->pdf->SetFont('dejavusans', 'B', 20); // UTF8 fonts with lithuanian support: freeserif, dejavusans
$this->pdf->Write(0, 'ąžūčšęėųįĄŽŪČŠĘĖŲĮ Превед Кросавчег!', '', 0, 'C', true, 0, false, false, 0);
以为你会在 2024-10-29 05:50:02

使用 dejavusans 字体,它可以很好地处理俄语和拉脱维亚语字母。

With dejavusans font it worked fine for both Russian and Latvian letters.

桃气十足 2024-10-29 05:50:02

对我来说这是一个字体问题。我使用了 times 字体,但我的本地多字节 chras 无法正确显示。当我将其更改为 freeserif 时,它们工作正常:)

With me it was a font problem. I used the font timesand my local multibyte chras wouldn't show up properly. When I changed it to freeserif they were working normally :)

梦开始←不甜 2024-10-29 05:50:02

为此,使用参数 TCPDF 构造函数的以下代码

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'ISO-8859-1', false);

它将对您有所帮助。

For this use the following code of the parameter TCPDF constructor

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'ISO-8859-1', false);

It will help you.

鸠书 2024-10-29 05:50:02
$fontname = $pdf->addTTFfont('C:\xampp\htdocs\copyshop\fonts\07-TH-Sarabun-PSK\THSarabun.ttf', 'TrueTypeUnicode', '', 32);
    $pdf->SetFont($fontname, '', 16,'',FALSE); //Working
$fontname = $pdf->addTTFfont('C:\xampp\htdocs\copyshop\fonts\07-TH-Sarabun-PSK\THSarabun.ttf', 'TrueTypeUnicode', '', 32);
    $pdf->SetFont($fontname, '', 16,'',FALSE); //Working
三岁铭 2024-10-29 05:50:02

我对罗马尼亚字符也有同样的问题,问题不在于编码、LC_CTYPETCPDF 的其他设置,而是我使用的字体。
我提到我使用了带有 Courier 字体的 TWIG 模板。
您可以尝试将字体更改为freeserif

I had the same issue with Romanian characters and the problem wasn't the encoding, LC_CTYPE or other setting from TCPDF, but the font I used.
I mention that I used TWIG templating with Courier font.
You can try to change your font to freeserif

萌无敌 2024-10-29 05:50:02

更改字体以正常显示 $ 和立陶宛符号

$pdf->SetFont('cid0cs', '', 12);

change the font to show normally ₹ and Lithuanian symbols

$pdf->SetFont('cid0cs', '', 12);
同尘 2024-10-29 05:50:02

为了面对这个问题,我不得不使用 dejavusans 字体,它是一种 UTF-8 Unicode 字体。这有助于解决渲染 UTF-8 单词(例如 ąčęėįšųūž)时的编码问题。

但是,我使用的是标准 ASCII 字体 helvetica 字体,并将文档的字体更改为 dejavusans 使其变得丑陋。

为了解决这个问题,我设置了两种要在文档中加载的字体,并指定文档的一部分使用 dejavusans 字体

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

/*
 ......
 set margins and other options
 ......
 */

// Set font
// dejavusans is a UTF-8 Unicode font, 
// if you only need to print standard ASCII chars, 
// you can use core fonts like helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 12, '', true);
$pdf->SetFont('helvetica', '', 12, '', true);

$html = '<div style="font-family: helvetica;">' . $clientName . '</div><div style="font-family: dejavusans;">' . $clientAddress . '</div>';

// Print text using writeHTMLCell()
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);

$pdf->Output('invoice.pdf', 'I');

To faced this issue and I had to use dejavusans font which is a UTF-8 Unicode font. This help fix the encoding issue when render UTF-8 words like ąčęėįšųūž.

However, I was using helvetica font which is standard ASCII font, and changing document's font to dejavusans make it ugly.

To fix this I set two fonts to be loaded in the document and specified a part of the document to use dejavusans font

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

/*
 ......
 set margins and other options
 ......
 */

// Set font
// dejavusans is a UTF-8 Unicode font, 
// if you only need to print standard ASCII chars, 
// you can use core fonts like helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 12, '', true);
$pdf->SetFont('helvetica', '', 12, '', true);

$html = '<div style="font-family: helvetica;">' . $clientName . '</div><div style="font-family: dejavusans;">' . $clientAddress . '</div>';

// Print text using writeHTMLCell()
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);

$pdf->Output('invoice.pdf', 'I');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文