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

如何结合FPDF与FPDI实现导入PDF并自动静默打印?

我来帮你搞定这个整合问题!其实核心思路就是调整类的继承关系,让FPDI能同时拥有自动打印扩展的功能——毕竟FPDI本身是基于FPDF扩展的,只要把继承链串对,就能无缝结合加载现有PDF和自动打印的能力。

具体实现步骤

首先,确保你已经有那两个扩展类(PDF_JavaScript和PDF_AutoPrint),如果还没定义,先把它们写上(你说代码能正常运行,这里就直接放标准实现方便参考):

// PDF_JavaScript 扩展:负责嵌入JS代码到PDF
class PDF_JavaScript extends FPDF {
    var $javascript;
    var $n_js;

    function IncludeJS($script) {
        $this->javascript = $script;
    }

    function _putjavascript() {
        $this->_newobj();
        $this->n_js = $this->n;
        $this->_out('<<');
        $this->_out('/Names [(EmbeddedJS) '.($this->n+1).' 0 R]');
        $this->_out('>>');
        $this->_out('endobj');
        $this->_newobj();
        $this->_out('<<');
        $this->_out('/S /JavaScript');
        $this->_out('/JS '.$this->_textstring($this->javascript));
        $this->_out('>>');
        $this->_out('endobj');
    }

    function _putresources() {
        parent::_putresources();
        if (!empty($this->javascript)) $this->_putjavascript();
    }

    function _putcatalog() {
        parent::_putcatalog();
        if (!empty($this->javascript)) {
            $this->_out('/Names <</JavaScript '.($this->n_js).' 0 R>>');
        }
    }
}

// PDF_AutoPrint 扩展:封装自动打印的JS逻辑
class PDF_AutoPrint extends PDF_JavaScript {
    // 通用自动打印(静默打印传false,弹出对话框传true)
    function AutoPrint($dialog = false) {
        $script = 'print('.(($dialog) ? 'true' : 'false').');';
        $this->IncludeJS($script);
    }

    // 指定打印机的打印(需要浏览器支持,适合内网场景)
    function AutoPrintToPrinter($server, $printer, $dialog = false) {
        $script = "var pp = getPrintParams();";
        $script .= $dialog ? 
            "pp.interactive = pp.constants.interactionLevel.full;" : 
            "pp.interactive = pp.constants.interactionLevel.automatic;";
        $script .= "pp.printerName = '\\\\\\\\".$server."\\\\".$printer."';";
        $script .= "print(pp);";
        $this->IncludeJS($script);
    }
}

接下来是关键的整合步骤:让FPDI继承PDF_AutoPrint,或者用FPDI 2.x的Trait来更灵活地组合功能:

// 整合FPDI和自动打印功能(以FPDI 2.x为例,用Trait更简洁)
class FPDI_AutoPrint extends PDF_AutoPrint {
    use \setasign\Fpdi\FpdiTrait;
}

// 实例化自定义的整合类
$pdf = new FPDI_AutoPrint();

// 加载你现有的PDF文件
$totalPages = $pdf->setSourceFile('your_target_file.pdf');

// 逐页导入原PDF的内容
for ($pageNum = 1; $pageNum <= $totalPages; $pageNum++) {
    $templateId = $pdf->importPage($pageNum);
    $pageSize = $pdf->getTemplateSize($templateId);
    // 根据原页面的方向和尺寸添加新页面
    $pdf->AddPage($pageSize['orientation'], [$pageSize['width'], $pageSize['height']]);
    // 把原页面内容渲染到新PDF里
    $pdf->useTemplate($templateId);
}

// 添加静默打印逻辑(传false就是无对话框自动打印)
$pdf->AutoPrint(false);

// 输出PDF:用'I'模式会直接在浏览器打开并触发打印;用'F'则保存为文件
$pdf->Output('I', 'auto_print_output.pdf');

关键注意点

  1. 继承链/ Trait选择:如果是FPDI 1.x版本,没法用Trait,就调整继承关系为class FPDI_AutoPrint extends FPDI,然后把PDF_AutoPrint的代码整合进去,或者让PDF_AutoPrint直接继承FPDI。
  2. 浏览器权限:浏览器默认可能会阻止自动打印,需要用户第一次手动允许该网站的自动打印权限,之后才能实现真正的静默打印。
  3. 输出模式:Output('I', ...)是直接在浏览器打开并触发打印,这是最符合你需求的方式;如果需要服务器端保存带自动打印逻辑的PDF,就用Output('F', 'save_path.pdf')。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:28:30