gpt4 book ai didi

Laravel - 将使用 TailwindCSS 设置样式的 View 输出为 PDF

转载 作者:行者123 更新时间:2023-12-05 08:12:29 37 4
gpt4 key购买 nike

我正在尝试从 View 生成 pdf,但样式就是出不来。我试过使用 3 个不同的库,但结果差别不大。我错过了什么吗?

查看

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Test View</title>
<link rel="stylesheet" type="text/css" media="screen" href="{{ asset('css/app.css') }}">
<script src="{{ asset('js/app.js') }}" defer></script>
</head>
<body class="font-sans antialiased">

<div class="grid grid-cols-3">
<div class="bg-red-200 col-span-2 h-screen"></div>
<div class="bg-blue-200 h-screen">
<div class="grid grid-cols-8">
<div class="col-span-7">
<div class="rounded-t-full w-full h-screen flex items-center justify-center bg-green-600">
<div class="h-60 w-60 rounded-full bg-blue-500">TEST</div>
</div>
</div>
</div>
</div>
</div>

</body>
</html>

外观

enter image description here


dompdf导出方法

protected function dompdfImplementation()
{
$dompdf = new Dompdf;
$dompdf->getOptions()->setChroot(public_path());
$dompdf->loadHtml(view('view')->render());

$dompdf->stream('view.pdf', ['Attachment' => false]);
}

dompdf导出结果

enter image description here


mpdf导出方法

protected function mpdfImplementation()
{
$mpdf = new Mpdf;
$mpdf->WriteHTML(view('view')->render());

$mpdf->output();
}

mpdf导出结果

enter image description here


tcpdf导出方法

protected function tcpdfImplementation()
{
$tcpdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
$tcpdf->AddPage();
$tcpdf->writeHTML(view('view')->render());

$tcpdf->Output('view.pdf', 'I');
}

tcpdf导出结果

enter image description here


如果没有 css 内联器,是否无法将 View 导出为 pdf?

我是否最好手动截取整页屏幕截图,将其粘贴到文本文档中并将其另存为 pdf 文件?

最佳答案

这个包就像一个魅力(与 spatie 一样): https://github.com/spatie/browsershot

根据文档:

The package can convert a webpage to an image or pdf. The conversionis done behind the scenes by Puppeteer which controls a headlessversion of Google Chrome.

关于Laravel - 将使用 TailwindCSS 设置样式的 View 输出为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67730273/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com