php TCPDF UTF-8。立陶宛符号未显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5333702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TCPDF UTF-8. Lithuanian symbols not showing up
提问by Bounce
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).
我使用最新的 TCPDF 版本(5.9)。但是编码有一些奇怪的问题。我需要立陶宛语符号,如:???????ū?。但是只得到很少的。其他保持像 ????? 所以我该怎么做 ?我使用默认时间字体(它带有 TCPDF 下载)。
Any help would be appreciated.
任何帮助,将不胜感激。
采纳答案by Elisa
Set the $unicode
parameter on the TCPDF constructor to false
and the $encoding
parameter to 'ISO-8859-1'
or some other character map.
将$unicode
TCPDF 构造函数上的参数设置为false
并将$encoding
参数设置为'ISO-8859-1'
或其他一些字符映射。
Thiswill help you:
这将帮助您:
Default for UTF-8 unicode:
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);
回答by squirrely
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.
TCPDF 使用 utf8 非常棘手。实现您想要的最佳方法是将字体嵌入生成的 PDF 文件本身。您可以使用 TCPPDF 包中的 freeserif 字体,它包含所有 utf8 符号,绝对可以显示任何语言的任何字符,但会向输出文件添加 ~700kb。如果文件大小无关紧要,这可能是获取所需符号的最简单方法。
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..
或者,您可以中继从系统中获取的核心字体,如果找不到,则替换为替代品。这使得输出文件非常轻,但增加了字体子集的必要性以获得异国情调的字符。就我个人而言,我还没有成功,所以我仍然认为嵌入字体是最好的解决方案,而且它也更通用。
回答by Laurynas Mali?auskas
there is a font included in the CPDF core fonts - dejavusans, it shows all the lithuanian characters. Just add the following:
CPDF 核心字体中包含一种字体 - dejavusans,它显示了所有立陶宛字符。只需添加以下内容:
$pdf->setHeaderFont(Array('dejavusans', '', 10, '', false));
$pdf->setFooterFont(Array('dejavusans', '', 8, '', false));
$pdf->SetFont('dejavusans', '', 10, '', false);
回答by Truongnq
Set font to freeserif it will work. I tested.
将字体设置为 freeserif 它将起作用。我测试过。
$pdf->SetFont('freeserif', '', 14, '', true);
回答by RobertGonzalez
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 '?'.
刚刚在尝试使用默认 Helvetica 字体呈现罗马尼亚语文本时发现了同样的情况。在进行一些调查时,我发现 tcpdf 库将其默认字体(称为“核心”字体)视为 Latin1 字符,因此即使您告诉它使用 UTF-8 编码并设置 unicode 标志,它也会逐字翻译您的文本在渲染之前到 Latin1 等价物。库的默认行为是,如果它找到一个Latin1 等价物,就翻译它可以找到等价物的每个字符,否则它将字符翻译为“?”。
This can be found inside the TCPDF class in the following method chain:
Write()
-> Cell()
-> getCellCode()
-> _escapetext()
.
这可以在以下方法链中的 TCPDF 类中找到:
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.
在里面_escapetext()
你可以看到它正在检查$this->isunicode
然后检查所选字体以查看它的类型是否为core|TrueType|Type1。如果是,它将通过该UTF8ToLatin1()
方法为您将字符串“拉丁化” 。这是'?' 翻译正在进行中。
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.
我的建议是使用与您使用的默认字体相似的自定义 unicode 字体(如 Deja Vu Sans)。在我目前的情况下,这对我有用。
回答by Ledadu
To use TCPDF with special characters like ?, ? or othersyou need to use a unicodefont:
将 TCPPDF 与特殊字符一起使用,例如 ?, ? 或其他需要使用unicode字体的字体:
downloadthe 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:
application/libraries/tcpdf/fonts/arialuni.ctg.z
application/libraries/tcpdf/fonts/arialuni.php
application/libraries/tcpdf/fonts/arialuni.znow you can set the new font with : $pdf->SetFont('arialuni', '', 10.5);
and now you can use special unicode characters like ? and more....
在这里下载字体: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
application/libraries/tcpdf/fonts/arialuni.php
application/libraries/tcpdf/fonts/arialuni.z现在您可以使用以下命令设置新字体: $pdf->SetFont('arialuni', '', 10.5);
现在您可以使用特殊的 unicode 字符,例如 ? 和更多....
回答by Aki
You u have problem to read character like Karnātakafrom database and display like this karn?takaI mean "?"which we don't want then do following things :
你有问题从数据库中读取像Karnātaka这样的字符并像这样显示 karn?taka我的意思是“?” 我们不想要的然后做以下事情:
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);
Use
$pdf->SetFont('DejaVuSerif', '', 10);
instead of$pdf->SetFont('helvetica', 'B', 12);
- For TCPDF Library of the PHP read character like Rājasthāninstead of R?jasth?nfrom database
为连接定义字符集 (
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);
使用
$pdf->SetFont('DejaVuSerif', '', 10);
代替$pdf->SetFont('helvetica', 'B', 12);
- 对于 PHP 的 TCPDF 库,从数据库中读取字符,如Rājasthān而不是R?jasth?n
回答by Mark Storer
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.
IIRC,您可以在创建新字体时定义编码,如此处所述。否则,您必须使用在创建字体时定义的编码。听起来像 TCPDF 附带的字体都使用 WinAnsiEncoding ... aka 代码页 1252。
Clunky, but effective.
笨拙,但有效。
回答by JustAMartin
With dejavusans font it worked fine for both Russian and Latvian letters.
使用 dejavusans 字体,它适用于俄语和拉脱维亚语字母。
回答by Klemen Tu?ar
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 to freeserif
they were working normally :)
对我来说,这是一个字体问题。我使用了字体times
,我的本地多字节字符无法正确显示。当我将其更改为freeserif
它们时,它们正常工作:)