Laravel 5.1 - barryvdh/laravel-dompdf,PDF 文件下载无法正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34493647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.1 - barryvdh/laravel-dompdf, PDF file download not working properly
提问by SJB
I have installed 'barryvdh/laravel-dompdf' using composer and added these lines
我已经使用 composer 安装了“barryvdh/laravel-dompdf”并添加了这些行
Barryvdh\DomPDF\ServiceProvider::class
'PDF'=> Barryvdh\DomPDF\Facade::class
to 'app.php' file in 'config' folder and also created PdfController, like this
到“config”文件夹中的“app.php”文件,还创建了PdfController,就像这样
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App;
use PDF;
class PdfController extends Controller
{
public function invoice()
{
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1>');
return $pdf->stream('testfile')
->header('Content-Type','application/pdf');
}
}
now the code works fine , contents are show in browser in pdf format, but when i download this file, the file doesn't have any extension.I tried to modify the line containing return code like
现在代码工作正常,内容以pdf格式显示在浏览器中,但是当我下载此文件时,该文件没有任何扩展名。我试图修改包含返回代码的行,如
return $pdf->stream('testfile.pdf')
->header('Content-Type','application/pdf');
But now, page directly provides file to download with name 'document.pdf' which shows error while downloading,(dont konw how and from where this name 'document.pdf' came from). Also i m working on localhost.
但是现在,页面直接提供名称为“document.pdf”的文件下载,下载时显示错误,(不知道这个名称“document.pdf”的来源和方式)。我也在本地主机上工作。
回答by xngigez
I had the same problem, after searching all over without results, I decided to play with the code and this worked for me.
我遇到了同样的问题,在搜索完没有结果后,我决定使用代码,这对我有用。
Inside config/app.php
在 config/app.php 里面
-> under providers add the line of code,
-> 在提供者下添加代码行,
'Barryvdh\DomPDF\ServiceProvider' not Barryvdh\DomPDF\ServiceProvider::class,
-> under aliases add the line of code,
-> 在别名下添加代码行,
'PDF' => 'Barryvdh\DomPDF\Facade' not 'PDF' => Barryvdh\DomPDF\Facade::class,
Mind the quotes and commas! Check the images above,
注意引号和逗号!检查上面的图像,
-> In your controller add the line of code, use App;
-> 在你的控制器中添加一行代码,使用 App;
Hopefully, you will be good to go....test using the lines of code..place them inside a method in your controller.
希望您能顺利开始……使用代码行进行测试……将它们放在控制器的方法中。
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1>');
return $pdf->stream();
On success, your browser will open a PDF Viewer with a white PDF with the word Test
成功后,您的浏览器将打开一个 PDF 查看器,其中包含一个带有 Test 字样的白色 PDF
Cheers, enjoy your coding ;)
干杯,享受你的编码 ;)
回答by Adnan
What do you want to do with pdf ? To show in browser or to download to disc? this is example where pdf is downloaded
你想用 pdf 做什么?在浏览器中显示还是下载到光盘?这是下载pdf的示例
return $pdf->download('pdfName.pdf');
example from my code where i show notebook information in pdf
我的代码中的示例,其中我以 pdf 格式显示笔记本信息
$laptop = Notebook::findOrFail($id);
$data['laptop'] = $laptop;
$pdf = \PDF::loadView('pdf.detaljiLaptop', $data);
return $pdf->download($laptop->modelName.' Computers.pdf');
回答by Myat Htut
I use this code view()->share($data);
$pdf = PDF::loadview('sale.pdf');
return $pdf->download('slip_out.pdf');
我用这个代码 view()->share($data);
$pdf = PDF::loadview('sale.pdf');
return $pdf->download('slip_out.pdf');
instead of
代替
return $pdf->stream('testfile.pdf')
->header('Content-Type','application/pdf');