用 PHP 处理 FPDF 中的特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3514076/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:03:45  来源:igfitidea点击:

Special Characters in FPDF with PHP

phpcharacterfpdf

提问by Carson

I have a web form that users can fill out and that content fills up a PDF with FPDF and PHP. When a user enters a word with an apostrophe, a slash appears before it on the PDF.

我有一个用户可以填写的网络表单,并且该内容使用 FPDF 和 PHP 填写 PDF。当用户输入带有撇号的单词时,PDF 上会在它前面出现一个斜线。

Similarly, special characters like trademark symbols are encoded wrong.

同样,商标符号等特殊字符编码错误。

The FPDF FAQs say to use:

FPDF 常见问题解答说使用:

$str = utf8_decode($str);

But I'm just not sure how to apply that to the whole PDF. I'm trying to think about it as if it was an HTML page but that isn't helping.

但我只是不确定如何将其应用于整个 PDF。我试图将它视为一个 HTML 页面,但这无济于事。

Any ideas?

有任何想法吗?

回答by Carson

Figured this out by doing the following (pagesubtitle is the name of the text field in the form):

通过执行以下操作来解决这个问题(pagesubtitle 是表单中文本字段的名称):

$reportSubtitle = stripslashes($_POST['pagesubtitle']);
$reportSubtitle = iconv('UTF-8', 'windows-1252', $reportSubtitle);

Then print it out:

然后打印出来:

$pdf->Write (6, $reportSubtitle);

This will remove any unwanted slashes following apostrophes, as well as use the 'iconv' function to print special characters such as ?

这将删除撇号后面的任何不需要的斜线,并使用 'iconv' 函数打印特殊字符,例如 ?

回答by Oscar Bout

All of the above did not work for me, but I did get it to work.

以上所有对我都不起作用,但我确实让它起作用了。

I managed to do it "the barbarian way" by just translating every weird character to its url value. Then simply decode the url and voila!

我设法通过将每个奇怪的字符转换为其 url 值来“以野蛮的方式”做到这一点。然后只需解码 url 就可以了!

function em($word) {

    $word = str_replace("@","%40",$word);
    $word = str_replace("`","%60",$word);
    $word = str_replace("¢","%A2",$word);
    $word = str_replace("£","%A3",$word);
    $word = str_replace("¥","%A5",$word);
    $word = str_replace("|","%A6",$word);
    $word = str_replace("?","%AB",$word);
    $word = str_replace("?","%AC",$word);
    $word = str_replace("ˉ","%AD",$word);
    $word = str_replace("o","%B0",$word);
    $word = str_replace("±","%B1",$word);
    $word = str_replace("a","%B2",$word);
    $word = str_replace("μ","%B5",$word);
    $word = str_replace("?","%BB",$word);
    $word = str_replace("?","%BC",$word);
    $word = str_replace("?","%BD",$word);
    $word = str_replace("?","%BF",$word);
    $word = str_replace("à","%C0",$word);
    $word = str_replace("á","%C1",$word);
    $word = str_replace("?","%C2",$word);
    $word = str_replace("?","%C3",$word);
    $word = str_replace("?","%C4",$word);
    $word = str_replace("?","%C5",$word);
    $word = str_replace("?","%C6",$word);
    $word = str_replace("?","%C7",$word);
    $word = str_replace("è","%C8",$word);
    $word = str_replace("é","%C9",$word);
    $word = str_replace("ê","%CA",$word);
    $word = str_replace("?","%CB",$word);
    $word = str_replace("ì","%CC",$word);
    $word = str_replace("í","%CD",$word);
    $word = str_replace("?","%CE",$word);
    $word = str_replace("?","%CF",$word);
    $word = str_replace("D","%D0",$word);
    $word = str_replace("?","%D1",$word);
    $word = str_replace("ò","%D2",$word);
    $word = str_replace("ó","%D3",$word);
    $word = str_replace("?","%D4",$word);
    $word = str_replace("?","%D5",$word);
    $word = str_replace("?","%D6",$word);
    $word = str_replace("?","%D8",$word);
    $word = str_replace("ù","%D9",$word);
    $word = str_replace("ú","%DA",$word);
    $word = str_replace("?","%DB",$word);
    $word = str_replace("ü","%DC",$word);
    $word = str_replace("Y","%DD",$word);
    $word = str_replace("T","%DE",$word);
    $word = str_replace("?","%DF",$word);
    $word = str_replace("à","%E0",$word);
    $word = str_replace("á","%E1",$word);
    $word = str_replace("a","%E2",$word);
    $word = str_replace("?","%E3",$word);
    $word = str_replace("?","%E4",$word);
    $word = str_replace("?","%E5",$word);
    $word = str_replace("?","%E6",$word);
    $word = str_replace("?","%E7",$word);
    $word = str_replace("è","%E8",$word);
    $word = str_replace("é","%E9",$word);
    $word = str_replace("ê","%EA",$word);
    $word = str_replace("?","%EB",$word);
    $word = str_replace("ì","%EC",$word);
    $word = str_replace("í","%ED",$word);
    $word = str_replace("?","%EE",$word);
    $word = str_replace("?","%EF",$word);
    $word = str_replace("e","%F0",$word);
    $word = str_replace("?","%F1",$word);
    $word = str_replace("ò","%F2",$word);
    $word = str_replace("ó","%F3",$word);
    $word = str_replace("?","%F4",$word);
    $word = str_replace("?","%F5",$word);
    $word = str_replace("?","%F6",$word);
    $word = str_replace("÷","%F7",$word);
    $word = str_replace("?","%F8",$word);
    $word = str_replace("ù","%F9",$word);
    $word = str_replace("ú","%FA",$word);
    $word = str_replace("?","%FB",$word);
    $word = str_replace("ü","%FC",$word);
    $word = str_replace("y","%FD",$word);
    $word = str_replace("t","%FE",$word);
    $word = str_replace("?","%FF",$word);
    return $word;
}

And of course me calling the function

当然我调用函数

$weirdword = "Días, Miércoles, Sábado,miércoles"; //Some spanish days
$weirdword = em($weirdword);
$weirdword = urldecode($weirdword);
echo $weirdword;

Giving output:

给出输出:

Días, Miércoles, Sábado,miércoles

Días, Miércoles, Sábado,miércoles

回答by MH2K9

None of above had solved my problem. I had solved it by the following way:

以上都没有解决我的问题。我通过以下方式解决了它:

setlocale(LC_CTYPE, 'en_US');

$value = iconv('UTF-8', 'ASCII//TRANSLIT', $value);
$fpdf->Cell(140, 6, $value, 1);

Hope you will be helpful.

希望你会有所帮助。

Reference:Link

参考:链接

回答by tomazahlin

none of above solutions worked for me, so I solved the problem like this:

以上解决方案都不适合我,所以我解决了这样的问题:

$this->AddFont('Arial','','arial.php');
$this->SetFont('Arial','',12);
$this->Cell(0,5,iconv("UTF-8", "CP1250//TRANSLIT", $string),0,1,'L');

Before trying the above lines, do the following:

在尝试以上几行之前,请执行以下操作:

Copy from c:/Windows/Fonts/Arial.ttf to the /tutorial folder of FPDF.

从 c:/Windows/Fonts/Arial.ttf 复制到 FPDF 的 /tutorial 文件夹。

Edit the content of makefont.php

编辑makefont.php的内容

require('../makefont/makefont.php');
MakeFont('arial.ttf','cp1250');

Execute makefont.php

执行 makefont.php

Copy the following files to the /font folder of FPDF: arial.php arial.ttf arial.z

将以下文件复制到 FPDF 的 /font 文件夹中: arial.php arial.ttf arial.z

Finally, define the "font folder". Open fpdf.php (main library file) and add:

最后,定义“字体文件夹”。打开 fpdf.php(主库文件)并添加:

define('FPDF_FONTPATH','font');

The PDF works for me with all special characters, I believe it was the problem in the Arial font itself, which FPDF originally uses. It should work with other fonts aswell, if they support your characters. Good luck!

PDF适用于所有特殊字符,我相信这是FPDF最初使用的Arial字体本身的问题。如果其他字体支持您的字符,它也应该适用于其他字体。祝你好运!

回答by Jaspal Saini

Below works for me (Using FPDF):

以下对我有用(使用 FPDF):



function em_jaz($word) {
$word = str_replace('+', ' ', $word);
$word = str_replace("%C3%A9","%E9",$word);          /* é */
$word = str_replace("%C3%A8","%E8",$word);          /* è */
$word = str_replace("%C3%AE","%EE",$word);          /* ? */
$word = str_replace("%26rsquo%3B","%27",$word);     /* ' */
$word = str_replace("%C3%89","%C9",$word);          /* é */
$word = str_replace("%C3%8A","%CA",$word);          /* ê */ 
$word = str_replace("%C3%8B","%CB",$word);          /* ? */
$word = str_replace("%C3%8C","%CC",$word);          /* ì */
$word = str_replace("%C3%8D","%CD",$word);          /* í */
$word = str_replace("%C3%8E","%CE",$word);          /* ? */
$word = str_replace("%C3%8F","%CF",$word);          /* ? */
$word = str_replace("%C3%90","%D0",$word);          /* D */
$word = str_replace("%C3%91","%D1",$word);          /* ? */
$word = str_replace("%C3%92","%D2",$word);          /* ò */
$word = str_replace("%C3%93","%D3",$word);          /* ó */
$word = str_replace("%C3%94","%D4",$word);          /* ? */
$word = str_replace("%C3%95","%D5",$word);          /* ? */
$word = str_replace("%C3%96","%D6",$word);          /* ? */
$word = str_replace("%C3%98","%D8",$word);          /* ? */                 
$word = str_replace("%C3%99","%D9",$word);          /* ù */
$word = str_replace("%C3%9A","%DA",$word);          /* ú */
$word = str_replace("%C3%9B","%DB",$word);          /* ? */
$word = str_replace("%C3%9C","%DC",$word);          /* ü */
$word = str_replace("%C3%9D","%DD",$word);          /* Y */
$word = str_replace("%C3%9E","%DE",$word);          /* T */
$word = str_replace("%C3%9F","%DF",$word);          /* ? */
$word = str_replace("%C3%A0","%E0",$word);          /* à */
$word = str_replace("%C3%A1","%E1",$word);          /* á */
$word = str_replace("%C3%A2","%E2",$word);          /* a */
$word = str_replace("%C3%A3","%E3",$word);          /* ? */
$word = str_replace("%C3%A4","%E4",$word);          /* ? */
$word = str_replace("%C3%A5","%E5",$word);          /* ? */
$word = str_replace("%C3%A6","%E6",$word);          /* ? */
$word = str_replace("%C3%A7","%E7",$word);          /* ? */
$word = str_replace("%C3%AA","%EA",$word);          /* ê */
$word = str_replace("%C3%AB","%EB",$word);          /* ? */
$word = str_replace("%C3%AC","%EC",$word);          /* ì */
$word = str_replace("%C3%AD","%ED",$word);          /* í */
$word = str_replace("%C3%AF","%EF",$word);          /* ? */
$word = str_replace("%C3%B0","%F0",$word);          /* e */
$word = str_replace("%C3%B1","%F1",$word);          /* ? */
$word = str_replace("%C3%B2","%F2",$word);          /* ò */
$word = str_replace("%C3%B3","%F3",$word);          /* ó */
$word = str_replace("%C3%B4","%F4",$word);          /* ? */
$word = str_replace("%C3%B5","%F5",$word);          /* ? */
$word = str_replace("%C3%B6","%F6",$word);          /* ? */
$word = str_replace("%C3%B7","%F7",$word);          /* ÷ */
$word = str_replace("%C3%B8","%F8",$word);          /* ? */
$word = str_replace("%C3%B9","%F9",$word);          /* ù */
$word = str_replace("%C3%BA","%FA",$word);          /* ú */
$word = str_replace("%C3%BB","%FB",$word);          /* ? */
$word = str_replace("%C3%BC","%FC",$word);          /* ü */
$word = str_replace("%C3%BD","%FD",$word);          /* y */
$word = str_replace("%C3%BE","%FE",$word);          /* t */
$word = str_replace("%C3%BF","%FF",$word);          /* ? */ 
$word = str_replace("%40","%40",$word);             /* @ */
$word = str_replace("%60","%60",$word);             /* ` */
$word = str_replace("%C2%A2","%A2",$word);          /* ¢ */
$word = str_replace("%C2%A3","%A3",$word);          /* £ */
$word = str_replace("%C2%A5","%A5",$word);          /* ¥ */
$word = str_replace("%7C","%A6",$word);             /* | */
$word = str_replace("%C2%AB","%AB",$word);          /* ? */
$word = str_replace("%C2%AC","%AC",$word);          /* ? */
$word = str_replace("%C2%AF","%AD",$word);          /* ˉ */
$word = str_replace("%C2%BA","%B0",$word);          /* o */
$word = str_replace("%C2%B1","%B1",$word);          /* ± */
$word = str_replace("%C2%AA","%B2",$word);          /* a */
$word = str_replace("%C2%B5","%B5",$word);          /* μ */
$word = str_replace("%C2%BB","%BB",$word);          /* ? */
$word = str_replace("%C2%BC","%BC",$word);          /* ? */
$word = str_replace("%C2%BD","%BD",$word);          /* ? */
$word = str_replace("%C2%BF","%BF",$word);          /* ? */
$word = str_replace("%C3%80","%C0",$word);          /* à */
$word = str_replace("%C3%81","%C1",$word);          /* á */
$word = str_replace("%C3%82","%C2",$word);          /* ? */
$word = str_replace("%C3%83","%C3",$word);          /* ? */
$word = str_replace("%C3%84","%C4",$word);          /* ? */
$word = str_replace("%C3%85","%C5",$word);          /* ? */
$word = str_replace("%C3%86","%C6",$word);          /* ? */
$word = str_replace("%C3%87","%C7",$word);          /* ? */
$word = str_replace("%C3%88","%C8",$word);          `/`* è */
return $word;
}

$content = urlencode($content);

$content = urlencode($content);

$content = urldecode($pdf->em_jaz($content));

$content = urldecode($pdf->em_jaz($content));

回答by Pascalmh

This class is a modified version of FPDF that adds UTF-8 support. Moreover, it embeds only the necessary parts of the fonts that are used in the document, making the file size much smaller than if the whole fonts were embedded. These features were originally developed for the mPDF project.

此类是 FPDF 的修改版本,增加了 UTF-8 支持。此外,它仅嵌入文档中使用的字体的必要部分,使文件大小比嵌入整个字体小得多。这些功能最初是为 mPDF 项目开发的。

http://fpdf.org/en/script/script92.php

http://fpdf.org/en/script/script92.php

回答by Sergio Lopez Loya

Try this simple function: utf8_encode($txt). It works for me.

试试这个简单的功能:utf8_encode($txt). 这个对我有用。