我需要使用 pdflib 版本 8 撰写 pdf, 我需要在其中打印某些unicode字符
但是它们没有被渲染,而是显示下面的字符
可能是什么原因以及如何渲染角色?
下面是代码
$p = PDF_new();
/* open new PDF file; insert a file name to create the PDF on disk */
if (PDF_begin_document($p, "", "") == 0) {
die("Error: " . PDF_get_errmsg($p));
}
PDF_set_info($p, "Creator", "Abc");
PDF_set_info($p, "Author", "Abc");
PDF_set_info($p, "Title", "Test");
pdf_set_option($p, "textformat=utf8");
PDF_begin_page_ext($p, 595, 842, "");
$fontdir = '/usr/share/fonts/truetype/dejavu';
pdf_set_parameter($p, "FontOutline", "Dejavu=$fontdir/DejaVuSans.ttf");
$font = pdf_load_font($p, "Dejavu", "unicode","");
PDF_setfont($p, $font, 24.0);
PDF_set_text_pos($p, 50, 700);
pdf_show_xy($p,"dejb €",100,490);
pdf_show_xy($p,"dejb 𝑥 𝑎",200,490);
PDF_end_page_ext($p, "");
PDF_end_document($p, "");
$buf = PDF_get_buffer($p);
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=hello.pdf");
print $buf;
PDF_delete($p);
输出
编辑:
尝试使用 freesans 字体代替 dejavu,但输出没有变化。
$fontdir = '/usr/share/fonts/truetype/freefont';
pdf_set_parameter($p, "FontOutline", "FreeSans=$fontdir/FreeSans.ttf");
$font = pdf_load_font($p, "FreeSans", "unicode","")
您可以通过使用包含所需字形的字体来解决您的问题。当您检查链接页面“数学斜体小 A”的页面时,您可以看到“支持 U+1D44E 的字体”的链接:
如您所见,只有少数字体支持此字形,例如“DejaVu Serif Italic”。当我使用 DejaVu 包中的 DejaVu Serif Italic (DejaVuSerif-Italic.ttf) 时,我得到了预期的输出:
当然,其他字体也可能支持此字形,并且您不限于 DejaVuSans Serif。
只需对您的代码添加一个注释:该行:
pdf_set_option($p, "textformat=utf8");
需要 PDFlib 9。请使用
PDF_set_parameter($p, "textformat", "utf8");
相反。
对于与以下相关的问题:
像“ff”这样的文本被渲染成像“⍰”这样奇怪的字符。
--
我所做的是:
加载 pdf 后使用
embedFont
。
const doc_Pdflib = await PDFDocument.load(bytesPdf);
const font = await doc_Pdflib.embedFont(StandardFonts.TimesRoman, {
subset: true,
features: { liga: false },
});
const list_page_pdflib = doc_Pdflib.getPages();
--
您可以查看以下链接。 他们说pdf中的字体有问题。
字符“ff”未从 getTextContent() 的返回值中捕获 · 问题 #11016 · mozilla/pdf.js https://github.com/mozilla/pdf.js/issues/11016
特定字符组合的字体问题 · 问题 #871 · Hopding/pdf-lib https://github.com/Hopding/pdf-lib/issues/871