我使用以下包将 html 转换为 pdf:
https://github.com/vsmoraes/pdf-laravel5
但是当我使用它时,它看起来根本不存在。这是我原来的 html pdf:
当我转换它时,它看起来像这样:
我在这里做错了什么吗?
编辑:
public function __construct(Pdf $pdf)
{
$this->pdf = $pdf;
}
public function download()
{
$html = view('administration.invoice')->render();
return $this->pdf->load($html)->download();
}
最佳答案
这个 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();
记住将图像文件夹设置为您拥有的文件夹。
关于php - Laravel 打包 html 到 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34829446/