某些 UTF-8 字符(土耳其语)在 jsPDF 中显示为 HTML 数字代码
Some UTF-8 Characters (Turkish) are Shown as HTML Numeric Code in jsPDF
我正在尝试使用 jsPDF 库创建包含土耳其语字符的 PDF 文件。在我改变任何东西之前,它正在创建这样的 PDF:
"Ğ" 字符看起来是空的,"Ö" 字符显示为 HTML 数字代码,即 Ö
本例代码:
var doc = new jsPDF(('p','pt','a4'));
doc.setFontType("bold");
doc.text(60, 28, 'Given name(s):');
doc.setFontType("normal");
doc.text(88, 28, 'OĞUZHAN');
doc.setFontType("bold");
doc.text(60, 32, 'Place and date of birth:');
doc.setFontType("normal");
doc.text(101, 32, 'KADIKÖY,');
doc.save('test.pdf');
然后我选择了一种带有土耳其语字符和 .tff 扩展名的字体。我使用开发人员创建的 Font Converter 对其进行转换。
它给了我一个 .js 文件。在我将该文件和 .tff 文件包含到项目中之后,现在我看到了这样的最终 PDF:
"Ğ" 字符现在看起来不错,但"Ö" 字符仍然看起来 Ö
我只是使用 doc.setFont("timestr");
更改字体:
var doc = new jsPDF(('p','pt','a4'));
doc.setFont("timestr");
doc.setFontType("bold");
doc.text(60, 28, 'Given name(s):');
doc.setFontType("normal");
doc.text(88, 28, 'OĞUZHAN');
doc.setFontType("bold");
doc.text(60, 32, 'Place and date of birth:');
doc.setFontType("normal");
doc.text(101, 32, 'KADIKÖY,');
doc.save('test.pdf');
当然我包含转换器给我的 .js 文件:
<script src="/fonts/timestr-normal.js"></script>
<script src="/fonts/timestr-bold.js"></script>
<script src="/fonts/timestr-italic.js"></script>
<script src="/fonts/timestr-bolditalic.js"></script>
其中一个这样的 .js 文件:
(function (jsPDFAPI) {
var font = 'AAEAAAAZAQA...'; // A very long string, I shorted it to show you
var callAddFont = function () {
this.addFileToVFS('timestr-normal.ttf', font);
this.addFont('timestr-normal.ttf', 'timestr', 'normal');
};
jsPDFAPI.events.push(['addFonts', callAddFont])
})(jsPDF.API);
问题出在哪里?很奇怪,“Ğ”字符在所有这些过程之后看起来很正常,但是“Ö”字符看起来不正常,不是吗? ?
好的,问题已解决:因为我使用的是 C#,所以我从我的模式中获取这些文本,我不得不在 Html.Raw()
方法中使用它们。所以我这样写 Html.Raw(@model.Name)
问题就解决了。
我正在尝试使用 jsPDF 库创建包含土耳其语字符的 PDF 文件。在我改变任何东西之前,它正在创建这样的 PDF:
"Ğ" 字符看起来是空的,"Ö" 字符显示为 HTML 数字代码,即 Ö
本例代码:
var doc = new jsPDF(('p','pt','a4'));
doc.setFontType("bold");
doc.text(60, 28, 'Given name(s):');
doc.setFontType("normal");
doc.text(88, 28, 'OĞUZHAN');
doc.setFontType("bold");
doc.text(60, 32, 'Place and date of birth:');
doc.setFontType("normal");
doc.text(101, 32, 'KADIKÖY,');
doc.save('test.pdf');
然后我选择了一种带有土耳其语字符和 .tff 扩展名的字体。我使用开发人员创建的 Font Converter 对其进行转换。
它给了我一个 .js 文件。在我将该文件和 .tff 文件包含到项目中之后,现在我看到了这样的最终 PDF:
"Ğ" 字符现在看起来不错,但"Ö" 字符仍然看起来 Ö
我只是使用 doc.setFont("timestr");
更改字体:
var doc = new jsPDF(('p','pt','a4'));
doc.setFont("timestr");
doc.setFontType("bold");
doc.text(60, 28, 'Given name(s):');
doc.setFontType("normal");
doc.text(88, 28, 'OĞUZHAN');
doc.setFontType("bold");
doc.text(60, 32, 'Place and date of birth:');
doc.setFontType("normal");
doc.text(101, 32, 'KADIKÖY,');
doc.save('test.pdf');
当然我包含转换器给我的 .js 文件:
<script src="/fonts/timestr-normal.js"></script>
<script src="/fonts/timestr-bold.js"></script>
<script src="/fonts/timestr-italic.js"></script>
<script src="/fonts/timestr-bolditalic.js"></script>
其中一个这样的 .js 文件:
(function (jsPDFAPI) {
var font = 'AAEAAAAZAQA...'; // A very long string, I shorted it to show you
var callAddFont = function () {
this.addFileToVFS('timestr-normal.ttf', font);
this.addFont('timestr-normal.ttf', 'timestr', 'normal');
};
jsPDFAPI.events.push(['addFonts', callAddFont])
})(jsPDF.API);
问题出在哪里?很奇怪,“Ğ”字符在所有这些过程之后看起来很正常,但是“Ö”字符看起来不正常,不是吗? ?
好的,问题已解决:因为我使用的是 C#,所以我从我的模式中获取这些文本,我不得不在 Html.Raw()
方法中使用它们。所以我这样写 Html.Raw(@model.Name)
问题就解决了。