【问题标题】:pdflib certain unicode characters not renderingpdflib 某些 unicode 字符未呈现
【发布时间】:2016-04-27 10:20:20
【问题描述】:

我需要使用 pdflib version 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","")

【问题讨论】:

  • 尝试使用 freesans 而不是 dejavusans
  • @donald123 不,它不起作用。输出没有变化。检查更新的问题

标签: php unicode truetype pdflib


【解决方案1】:

您可以通过使用包含所需字形的字体来解决您的问题。当您检查链接页面“数学斜体小 A”的页面时,您可以看到指向“Fonts that support 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");

改为。

【讨论】:

  • 使用支持的帮助确实可以解决问题。但是我的内容是动态的,需要我每隔几个字符在DevajuSerif-ItalicDejaVuSerif 之间切换。解决此问题的一种方法是拆分字符串并不断更改子字符串之间的字体。有没有优雅的解决方案?
  • 您可以使用fallbackfonts 选项。 (请参阅包中的 starter_fallback.php 示例或pdflib.com/en/pdflib-cookbook/fonts/starter-fallback)在这种情况下,您可以指定一种字体,也应在其中搜索丢失的字形。
猜你喜欢
  • 1970-01-01
  • 2016-04-15
  • 2014-10-27
  • 2017-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-05
  • 1970-01-01
相关资源
最近更新 更多