TCPDF UTF-8 符号未显示
我使用最新的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
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..
CPDF 核心字体中包含一种字体 - dejavusans,它显示所有立陶宛字符。只需添加以下内容:
there is a font included in the CPDF core fonts - dejavusans, it shows all the lithuanian characters. Just add the following:
将 TCPDF 构造函数上的
$unicode
参数设置为false
,并将$encoding
参数设置为'ISO-8859-1' 或其他一些字符映射表。
这个< /a> 将帮助您:
UTF-8 unicode 的默认值:
欧洲字符集的构造函数示例:
Set the
$unicode
parameter on the TCPDF constructor tofalse
and the$encoding
parameter to'ISO-8859-1'
or some other character map.This will help you:
Default for UTF-8 unicode:
Example of constructor for European charset:
将字体设置为 freeserif 就可以了。
我测试过。
Set font to freeserif it will work.
I tested.
在尝试使用默认 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 theUTF8ToLatin1()
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.
将 TCPDF 与特殊字符(例如 ฿、포 或其他字符)一起使用
您需要使用 unicode 字体:
在此处下载字体:
ftp://ftp.fu-berlin.de/unix/ X11/multimedia/MPlayer/contrib/fonts/arialuni.ttf.bz2
创建一个测试 pdf 文件并将该字体加载到 TCPDF 中
示例:
$fontname = $pdf->addTTFfont('/var/www/app/images/fonts/arialuni.ttf', 'TrueTypeUnicode', '', 32);
<块引用>
application/libraries/tcpdf/fonts/arialuni.ctg.z
应用程序/库/tcpdf/fonts/arialuni.php
应用程序/库/tcpdf/fonts/arialuni.z
现在您可以使用以下命令设置新字体:
$pdf->SetFont('arialuni', '', 10.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:
download the font here:
ftp://ftp.fu-berlin.de/unix/X11/multimedia/MPlayer/contrib/fonts/arialuni.ttf.bz2
create a test pdf file and load this font into TCPDF
example:
$fontname = $pdf->addTTFfont('/var/www/app/images/fonts/arialuni.ttf', 'TrueTypeUnicode', '', 32);
this will create the fonts like:
now you can set the new font with :
$pdf->SetFont('arialuni', '', 10.5);
and now you can use special unicode characters like ฿ and more....
Source :
http://myridia.com/dev_posts/view/852
您无法从数据库中读取像 Karnātaka 这样的字符并像这样显示 karn?taka 我的意思是 "?" 我们不想要然后执行以下操作:
定义连接的字符集(
mysql_set_charset()
):使用
$pdf->SetFont('DejaVuSerif', '', 10);
而不是$pdf->SetFont('helvetica', 'B', 12);
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 :
Define charset for the connection (
mysql_set_charset()
):Use
$pdf->SetFont('DejaVuSerif', '', 10);
instead of$pdf->SetFont('helvetica', 'B', 12);
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.
使用默认 TCPDF 包测试了 dejavusans 和 freeserif,这两种字体都适用于立陶宛字符。我还输入了一些俄语字符,它们也起作用了。我用这段代码来测试它:
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:
使用 dejavusans 字体,它可以很好地处理俄语和拉脱维亚语字母。
With dejavusans font it worked fine for both Russian and Latvian letters.
对我来说这是一个字体问题。我使用了
times
字体,但我的本地多字节 chras 无法正确显示。当我将其更改为freeserif
时,它们工作正常:)With me it was a font problem. I used the font
times
and my local multibyte chras wouldn't show up properly. When I changed it tofreeserif
they were working normally :)为此,使用参数 TCPDF 构造函数的以下代码
它将对您有所帮助。
For this use the following code of the parameter TCPDF constructor
It will help you.
我对罗马尼亚字符也有同样的问题,问题不在于编码、LC_CTYPE 或 TCPDF 的其他设置,而是我使用的字体。
我提到我使用了带有 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
更改字体以正常显示 $ 和立陶宛符号
change the font to show normally ₹ and Lithuanian symbols
为了面对这个问题,我不得不使用 dejavusans 字体,它是一种 UTF-8 Unicode 字体。这有助于解决渲染 UTF-8 单词(例如
ąčęėįšųūž
)时的编码问题。但是,我使用的是标准 ASCII 字体
helvetica
字体,并将文档的字体更改为dejavusans
使其变得丑陋。为了解决这个问题,我设置了两种要在文档中加载的字体,并指定文档的一部分使用 dejavusans 字体
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 todejavusans
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