从Windows XAMPP迁移至Ubuntu后Laravel-snappy/wkhtmltopdf不渲染PDF页眉页脚
跨系统迁移后 Laravel Snappy PDF生成异常排查
问题场景
- 原运行环境:Windows + XAMPP 部署Laravel项目,PDF生成功能全部正常
- 切换到Ubuntu系统后,未改动任何业务代码,安装
laravel-snappy并配置好wkhtmltopdf执行路径后,出现两类功能异常:- 配置的自定义页眉、页脚完全无法渲染
- 表格跨页时,设置过的表头不会在新页面自动重复显示
- 前期排查尝试:
- 调整各类CSS布局属性、路由配置,均未解决问题
- 测试替代PDF生成库:dompdf无法支撑单份3000页规模的大体量PDF生成需求;tcpdf文档不完善;mpdf运行速度过慢,均不符合项目使用要求
涉及核心配置与代码
控制器PDF生成逻辑
public function createPDF(){ $userData = $this->getUserData(); // 查询并返回带关联关系的全量用户数据 $pdf = App::make('snappy.pdf.wrapper'); $footer = \view('supporting.footer')->render(); $header = \view('supporting.header')->render(); $userData = \collect([$this->userData[1]]); // 测试阶段缩小数据范围加快渲染速度 $pdf->loadView('orders', ['users' => $userData]) ->setOption('margin-top', '20mm') ->setOption('margin-bottom', '20mm') ->setOption('minimum-font-size', 25) ->setOption('header-html', $header) ->setOption('footer-html', $footer); $pdf->save($this->path); }
自定义页眉视图
<!DOCTYPE html> <html lang="en"> <head> </head> <body> <div style="background: blanchedAlmond;color:green;">My Header is amazing<div> </body> </html>
自定义页脚视图
<!DOCTYPE html> <html lang="en"> <head> </head> <body> <div style="background: blanchedalmond; color:green;">Copyright © 2020</div> </body> </html>
主订单页面视图核心样式与结构
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Order Layout</title> <style> *{ font-family: cursive; } .wrapper { display: block; } .invoice { display: block; width: 80%; margin: 0 auto; margin-top: 10px; padding-top: 10px; padding-bottom: 10px; background: #d3d3d3; page-break-inside: avoid !important; padding-left: 20px; } .order-items { padding-left: 100px; } .table { width: 90%; align-self: center; border: 1px solid black; page-break-inside: avoid !important; orphans: 15; } .table>* { text-align: center; } </style> </head> <body> <main class="wrapper"> @foreach ($users as $user) @php $orders = $user->orders // 变量重命名简化业务逻辑 @endphp @foreach ($orders as $order) <div class="invoice"> @php $sum=0; @endphp <h2>{{$user->name}}: {{$order->id}}</h2> <div class="order-items"> <table class="table" border="1"> <thead> <tr> <th>Product Name</th> <th>Unit Price</th> <th>Qty</th> <th>subtotal</th> </tr> </thead> <tbody> @foreach ($order->products as $product) <tr> <th> {{$product->name}}<br> </th> <td>{{$product->unit_price}}</td> <td>{{$product->pivot->quantity}}</td> @php $sum+= $product->pivot->quantity*$product->unit_price @endphp <td>{{$product->pivot->quantity*$product->unit_price}}</td> </tr> @endforeach </tbody> <tfoot> <tr> <th colspan="3">Total:</th> <td>{{$sum}}</td> </tr> </tfoot> </table> </div> </div> @endforeach @endforeach </main> </body> </html>
Ubuntu环境Apache站点配置
<VirtualHost *:80> ServerAdmin user@email.com DocumentRoot /home/frapto/frapto/pdf-mock/public ErrorLog "/home/frapto/example.error.log" CustomLog "/home/frapto/example.access.log" combined <Directory "/home/frapto/frapto/pdf-mock/public"> Options -Indexes DirectoryIndex index.php index.html AllowOverride All Require all granted </Directory> </VirtualHost>
问题根因与解决方案
Ubuntu系统默认软件源提供的wkhtmltopdf包是裁剪掉QT补丁的精简版本,缺失对自定义页眉页脚HTML渲染、跨页表头自动重复这类核心功能的支持。
无需调整任何原有业务代码、配置逻辑,仅需替换安装带官方QT补丁的wkhtmltopdf库,即可完全恢复所有PDF生成能力。
内容的提问来源于stack exchange,提问作者Ali
相关产品推荐
相关产品推荐

