我正在升级从Laravel 7到8,并希望切换到barryvdh/laravel-dompdf的PDF生成。到目前为止,我一直在使用niklasravnsborg/laravel-pdf,但是由于该软件包不支持Laravel 8,所以我需要切换。因此,我正在处理修改现有代码以使用barryvdh/laravel-dompdf的过程,但我遇到了一个问题。
这是我的(简化)控制器:
public function update(Request $request) {
$invoice = Invoice::find($request->invoice_id);
if(isset($request->export) AND $request->export == 1) {
$this->exportInvoice($invoice, $request);
}
}此exportInvoice函数位于同一个控制器文件中。
我用它来生成一个测试PDF:
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1>');
return $pdf->stream();现在,我设法将问题缩小到代码中PDF生成失败的位置。
如果我将PDF生成代码放在上面更新函数中的if语句中,那么我将得到预期的结果:一个简单的PDF文件。
但是,一旦我将这段代码移到exportInvoice函数中,我就会得到一个简单的空白网页。
我一直在谷歌搜索,但我找不到类似的问题。
我试着把我所有的代码放在update函数中,猜猜.这如预期的那样起作用。好像我对子函数做错了什么,但我想不出是什么。
有人看到我做错什么了吗?
发布于 2021-03-09 09:44:03
从您的update()方法,这将流一个PDF返回到浏览器:
return $pdf->stream();但是,从exportInvoice() (由update()方法调用)中,它只会将流返回到update()方法。如果你不对它做任何事情,它就不会到达浏览器。您需要返回从exportInvoice()返回的响应。
public function update(Request $request) {
$invoice = Invoice::find($request->invoice_id);
if(isset($request->export) AND $request->export == 1) {
// Note we need to *return* the response to the browser
return $this->exportInvoice($invoice, $request);
}
}
public function exportInvoice($invoice, $request) {
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1>');
return $pdf->stream();
}https://stackoverflow.com/questions/66543366
复制相似问题