如何在 FPDF 生成的 PHP/PDF 文档中使用扩展 ASCII 字符?

How do I use Extended ASCII characters in a PHP/PDF document generated by FPDF?

我正在尝试创建包含扩展 ASCII 字符的文档。对于来自客户端的文本,以下工作:

// Convert from UTF-8 to ISO-8859-1 - Deal with Spanish characters
setlocale(LC_ALL, 'en_US.UTF-8');
foreach ($_POST as $key => $value){
    $post[$key] = iconv("UTF-8", "ISO-8859-1", $value);
}

$pdf->Cell(0, 0, $post["Name"], 0, 1);

但是,我无法使 PHP 文件中的文本生效。例如:

$name = "José";

我不知道变量使用什么编码。因此,我无法将其转换为 ISO-8859-1。 é 被破坏了。

编辑: 我正在重写一个生成 PDF 文档的程序(一些是西班牙语)。如果我从现有的 PDF 中复制文本,我会得到以下信息:(在 PDF 文档和 IDE 中看起来很正常,但不能使用 CP1252 或 ISO-8859-1 字体在 FPDF 中打印)。

$Name = "José" // Jos\x65\xcc\x81 - I have no idea what encoding is used for the é

将扩展字符改为UTF-8解决问题:

$Name = "José" // Jos\xC3\xA9  - UTF-8
  1. 有谁知道我从现有 PDF 中复制的是哪种编码?
  2. 有没有办法将其转换为 UTF-8?
  3. 用户可以将这些东西输入浏览器吗?

当我将 UTF-8 编码字符转换为 ISO-8859-1 以输出到 FPDF 时,PDF 包含 é 的三字符编码版本。

第二次编辑: Unicode equivalence from Wikipedia

Unicode provides two notions, canonical equivalence and compatibility. Code point sequences that are defined as canonically equivalent are assumed to have the same appearance and meaning when printed or displayed. For example, the code point U+006E (the Latin lowercase "n") followed by U+0303 (the combining tilde "◌̃") is defined by Unicode to be canonically equivalent to the single code point U+00F1 (the lowercase letter "ñ" of the Spanish alphabet). Therefore, those sequences should be displayed in the same manner, should be treated in the same way by applications such as alphabetizing names or searching, and may be substituted for each other.

解释@smith 的评论说我只需要获得 TCPDF 或能够正确处理 UTF-8 的东西,这是很长的路要走。应该注意的是,我在 PHP 的 iconv 中收到错误,所以我不完全确定是否可以通过切换到 TCPDF 来消除它。

事实证明,要使用扩展的 ASCII 字符,需要选择和编码并在整个过程中使用它。在我的例子中,我使用了 UTF-8 编码的字符并在任何地方使用它们。我最初的问题源于我在从以规范等效格式编码的 PDF 文档中复制文本时犯了错误。一旦我在所有地方使用 UTF-8 编码字符,我的问题就消失了。