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

PrestaShop 1.7.8.7后台自定义PDF发票模块失效问题排查

PrestaShop 1.7.8.7 后台PDF重写失效解决方案

问题根源

PrestaShop 1.7.8+版本对后台订单发票PDF的生成逻辑做了调整,后台订单列表的PDF按钮不再调用AdminPdfController的generateInvoicePDFByIdOrder方法,转而通过AdminOrdersController的相关方法生成,导致原重写规则失效。

解决方案

方法1:重写AdminOrdersController替代原重写

创建/修改重写文件/override/controllers/admin/AdminOrdersController.php,替换后台发票PDF生成逻辑:

<?php
class AdminOrdersController extends AdminOrdersControllerCore
{
    /**
     * 重写后台订单发票PDF生成方法
     */
    public function generateInvoicePdf()
    {
        $id_order = (int)Tools::getValue('id_order');
        $order = new Order($id_order);
        
        if (!Validate::isLoadedObject($order)) {
            $this->errors[] = $this->trans('The order cannot be found within your database.', array(), 'Admin.Orderscustomers.Notification');
            $this->redirectAfter();
            return;
        }

        $reference = $order->reference;
        // 查找自定义PDF文件
        $file = @glob(_PS_ROOT_DIR_ . "/../fatture/*_$reference*.PDF");
        
        if (!empty($file[0]) && file_exists($file[0])) {
            // 输出自定义PDF
            $data = file_get_contents($file[0]);
            header("Content-type: application/pdf");
            header("Content-Disposition: attachment; filename=\"Fattura_$reference.pdf\"");
            echo $data;
            exit;
        } else {
            // 自定义PDF不存在时,调用原生逻辑
            $this->errors[] = $this->trans('la Fattura AS400 non è ancora disponibile', array(), 'Admin.Orderscustomers.Notification');
            parent::generateInvoicePdf();
        }
    }
}

方法2:使用全局钩子拦截PDF生成(兼容前后台)

如果希望同时覆盖前后台PDF生成逻辑,推荐使用actionPDFRender钩子,在模块主文件中添加以下代码:

  1. 安装时注册钩子:
public function install()
{
    return parent::install() && $this->registerHook('actionPDFRender');
}
  1. 实现钩子处理逻辑:
public function hookActionPDFRender($params)
{
    $pdf = $params['pdf'];
    $template = $pdf->template;
    
    // 仅处理发票模板
    if ($template !== PDF::TEMPLATE_INVOICE) {
        return;
    }

    // 获取订单参考号
    $reference = '';
    if ($pdf->objects instanceof Collection) {
        /** @var OrderInvoice $invoice */
        $invoice = $pdf->objects->first();
        if ($invoice) {
            $reference = $invoice->order_reference;
        }
    }

    if (empty($reference)) {
        return;
    }

    // 查找自定义PDF文件
    $file = @glob(_PS_ROOT_DIR_ . "/../fatture/*_$reference*.PDF");
    if (!empty($file[0]) && file_exists($file[0])) {
        // 输出自定义PDF并终止原生逻辑
        $data = file_get_contents($file[0]);
        header("Content-type: application/pdf");
        header("Content-Disposition: attachment; filename=\"Fattura_$reference.pdf\"");
        echo $data;
        exit;
    }
}

注意事项

  • 修改重写文件后,必须删除/var/cache/dev/class_index.php和/var/cache/prod/class_index.php,清除PrestaShop缓存以激活重写。
  • 确保自定义PDF文件路径正确,且服务器拥有该路径的读取权限。
  • 使用钩子方案时,需保证对应模块已安装并启用。

内容的提问来源于stack exchange,提问作者Simone ToNe Ceschi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 03:06:22