Laravel 5.0 - 从模板生成 PDF 并添加数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33081306/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:29:40  来源:igfitidea点击:

Laravel 5.0 - Generate PDF from template and add data

phplaravelpdflaravel-5dompdf

提问by nielsv

This is the case:

情况是这样的:

I have a formwhere the user has to fill in 5 fields. I also have a PDF templatewith some layout with 5 empty colored boxes.

我有一个表单,用户必须填写5 个字段。我还有一个带有5 个空彩色框的布局的PDF 模板

Now when the user fills in the form a PDF should be generated from the pdf templateand the fields that the user has given in should be added to the template.

现在,当用户填写表单时,应从 pdf 模板生成一个PDF,并且应将用户提供的字段添加到模板中

Now my question what's the best and easiest way to do this? I've look at the library laravel-dompdfbut it didn't look like it's working with templates.

现在我的问题是什么是最好和最简单的方法来做到这一点?我查看了laravel-dompdf库,但它看起来不像是在使用模板。

Did anybody had to do this? If yes, how did you do it? I'm working with laravel 5.0.

有人必须这样做吗?如果是,你是如何做到的?我正在使用 laravel 5.0

回答by S. Howe

I use the "vsmoraes/laravel-pdf": "^1.0" package and it does well for us.

我使用 "vsmoraes/laravel-pdf": "^1.0" 包,它对我们来说很好。

use Vsmoraes\Pdf\Pdf;

private $pdf;

public function __construct(Pdf $pdf)
{
    $this->middleware('auth');
    $this->pdf = $pdf;
}
public function pdfMaker($data)
{
$html = view('pdf.shippinglabel')->with('purchase', $purchase)->with('products', $products)->render();

    $label = $this->pdf->load($html)->output();

    file_put_contents(storage_path()."/ftp/po/label_".$id.".pdf",$label);
}

And then I have the formatted pdf with embedded CSS and not including external CSS file linking.

然后我有带嵌入式 CSS 的格式化 pdf,不包括外部 CSS 文件链接。

here's the github url: https://github.com/vsmoraes/pdf-laravel5

这是 github 网址:https: //github.com/vsmoraes/pdf-laravel5

回答by Robert-Jan

With the Laravel package from Barry (https://github.com/barryvdh/laravel-dompdf) you can load views. So in that way it is possible to make a template using the Blade views.

使用 Barry ( https://github.com/barryvdh/laravel-dompdf)的 Laravel 包,您可以加载视图。因此,通过这种方式,可以使用 Blade 视图制作模板。

回答by Moppo

Dompdf doesn't work with pdf templates, but it works with HTML templates.

Dompdf 不适用于 pdf 模板,但它适用于 HTML 模板。

So, you can do this way:

所以,你可以这样做:

  • Prepare a HTML template and place some placeholders( i.e: {field1}, {field2}, ecc ) where you want to print the values you'll get from the form
  • When the form is submitted, load the html template into a string, replace the placeholders in the string with the actual values you got from the form

  • Pass the string (that will contain valid html) to dompdf and it will generate a PDF file

  • 准备一个 HTML 模板并放置一些占位符(即:{field1}, {field2}, ecc )在您要打印将从表单中获得的值的位置
  • 提交表单后,将html模板加载到字符串中,将字符串中的占位符替换为您从表单中获得的实际值

  • 将字符串(将包含有效的 html)传递给 dompdf,它将生成一个 PDF 文件

回答by nielsv

I've used the packages fpdfand fpdito work with pdf templates.

我使用的封装FPDFFPDI工作与PDF格式的模板。