Laravel 5.1-Barryvdh/laravel-dompdf,PDF文件下载无法正常工作 [英] Laravel 5.1 - barryvdh/laravel-dompdf, PDF file download not working properly

查看:317
本文介绍了Laravel 5.1-Barryvdh/laravel-dompdf,PDF文件下载无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用composer安装了"barryvdh/laravel-dompdf"并添加了这些行

I have installed 'barryvdh/laravel-dompdf' using composer and added these lines

Barryvdh\DomPDF\ServiceProvider::class
'PDF'=>  Barryvdh\DomPDF\Facade::class

到"config"文件夹中的"app.php"文件,并创建了PdfController,就像这样

to 'app.php' file in 'config' folder and also created PdfController, like this

<?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');
    }
}

现在代码可以正常工作,内容以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

return $pdf->stream('testfile.pdf')
                   ->header('Content-Type','application/pdf');

但是现在,页面直接提供名称为"document.pdf"的文件下载 在下载时显示错误(不要知道此名称"document.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.

推荐答案

我遇到了同样的问题,在搜索无结果后,我决定使用该代码,这对我有用.

I had the same problem, after searching all over without results, I decided to play with the code and this worked for me.

内部config/app.php

Inside config/app.php

->添加代码行,

'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,

->在您的控制器中添加代码行, 使用App;

-> In your controller add the line of code, use 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();

成功后,您的浏览器将打开一个带有白色PDF的PDF查看器,其单词为Test

On success, your browser will open a PDF Viewer with a white PDF with the word Test

干杯,祝您编码愉快;)

Cheers, enjoy your coding ;)

这篇关于Laravel 5.1-Barryvdh/laravel-dompdf,PDF文件下载无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆