Laravel打包html到pdf

时间:2016-01-16 16:44:49

标签: php html laravel pdf type-conversion

我使用以下包将html转换为pdf:

https://github.com/vsmoraes/pdf-laravel5

但是当我使用它时,它并没有全神贯注。这是我原来的html pdf:

enter image description here

当我转换它时,它看起来像这样:

enter image description here

我在这里做错了吗?

编辑:

public function __construct(Pdf $pdf)
{
    $this->pdf = $pdf;
}
public function download()
{
    $html = view('administration.invoice')->render();
    return $this->pdf->load($html)->download();
}

1 个答案:

答案 0 :(得分:2)

这个laravel包似乎正在使用DOMPDF,这很棒。

考虑到我无法看到您的代码,我将分享对我有用的PHP代码:

// This should be your normal HTML page
$url = 'http://www.website.com/invoice';
$html = file_get_contents($url);

// First. Convert all relative image paths to file system paths.
// Test that the generated path is where your images files are located
$html = str_replace("/images", public_path()."/images", $html);

// Generate PDF file
$dompdf = new DOMPDF();
$dompdf->set_paper("A4", "portrait");
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream('your_invoice_file.pdf');

如果您还想将Laravel View直接渲染为PDF,请用这一行替换前两行代码

// Just render the view normally
$html = View::make('path/view');

根据您添加的代码

,替代选项
$html = view('administration.invoice')->render();
$html = str_replace("/images", public_path()."/images", $html);
return $this->pdf->load($html)->download();

请记住将images文件夹设置为您拥有的文件夹。