You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用PDF_get_option()获取字体名?替换pdf_get_parameter()的方法

解决PDF_get_parameter()弃用问题,改用PDF_get_option()获取字体名

直接替换获取字体名的代码行即可解决弃用警告,同时修正原代码里的语法错误:

替换前的代码行

$font_name = pdf_get_parameter($my_pdf, "fontname", 0);

替换后的代码行

$font_name = PDF_get_option($my_pdf, "fontname", "");

说明

  • PDF_get_option()的第三个参数是默认返回值,这里使用空字符串和获取字号的写法保持统一,逻辑更一致。
  • 原代码最后一行的PDF_load_font存在语法错误(缺少闭合括号),修正后应为:
$font_id = PDF_load_font($my_pdf, $font_name, "host", "");

完整修正后的函数代码

function nvpdf_WrapString($my_pdf,$string,$width,$width_in_chars = "") {

    // known problem: can only be called after the pdf document has been
    // started, font size set, etc as it relies on the pdf being in the
    // page scope to use pdf_stringwidth (see PDFLib manual p.83).
    // returns an array: 1st level column
    //                   2nd level data string broken up into lines that fit the column
    $string = str_replace('\n', "\n", $string);
    $inlines = explode("\n",$string);
    $outlines = array();

    $font_size = PDF_get_option($my_pdf, "fontsize", "");
    $font_name = PDF_get_option($my_pdf, "fontname", "");
    $font_id = PDF_load_font($my_pdf, $font_name, "host", "");
    // 补充后续逻辑代码...
}

内容的提问来源于stack exchange,提问作者ashraful006

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 15:10:27