php - Laravel 打包 html 到 pdf

标签 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();
}

最佳答案

这个 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/

相关文章:

php - mock 似乎无法正常工作

Laravel - 动态添加字段到模型并阻止其保存

php - 如何在 prestashop 程序化产品导入期间添加图像?

php - 将Form标签添加到innerHTML创建的html元素中

php - AJAX PHP 回调函数中的参数

html - 如何在数字输入中获得自动点?

php - PHP 中的动态图像生成

javascript - 两个不同的 Div 具有相同的 ID 和类名,其中一个是 Work

html - CSS - 固定宽度布局

php - 尝试安装 Laravel - 未找到 cURL