微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Laravel - 将带有 TailwindCSS 样式的视图输出为 PDF

如何解决Laravel - 将带有 TailwindCSS 样式的视图输出为 PDF

我正在尝试从视图生成 pdf,但样式不会出来。我试过使用 3 个不同的库,但结果没有太大不同。我错过了什么吗?

view

<!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>

appearance

enter image description here


dompdf export method

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

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

dompdf export result

enter image description here


mpdf export method

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

    $mpdf->output();
}

mpdf export result

enter image description here


tcpdf export method

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 export result

enter image description here


如果没有 css 内联,就不能将视图导出为 pdf 吗?

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

解决方法

如果您需要支持现代 CSS 功能,例如 flexbox,我想您的 Tailwind 示例使用了它,那么最好使用无头 chrome(或稍旧的 wkhtmltopdf)来生成 PDF。

使用 chrome-php/chrome 的示例:


<?php

use HeadlessChromium\BrowserFactory;
use HeadlessChromium\Page;

require_once __DIR__ . '/vendor/autoload.php';

$browserFactory = new BrowserFactory();

$browser = $browserFactory->createBrowser([
    'noSandbox' => true,'customFlags' => [
        '--proxy-server="direct://"','--proxy-bypass-list=*','--font-render-hinting=none',],]);

$page = $browser->createPage();

$tempname = '//<path to your HTML over HTTP>';

$page->navigate('file://' . $tempname)
    ->waitForNavigation(Page::NETWORK_IDLE);

$outputPath = __DIR__ . '/output.pdf';

$page->pdf([
    'printBackground' => true,'preferCSSPageSize' => true,'marginTop' => 0.4,'marginBottom' => 0.2,'marginLeft' => 0.2,'marginRight' => 0.2,])->saveToFile($outputPath);

使用 PHP PDF 库来利用它们相对于浏览器方法的一些优势 - 颜色处理、预印、条形码支持、页眉和页脚、页码、TOC 等,但鉴于它们对 HTML 和 CSS 的支持,期望量身定制很可能需要模板和样式表。

当然,当您无法通过托管服务提供商运行 PHP 以外的其他软件时,上一段也适用。

,

之后

  • 尝试使用 barryvdh/laravel-dompdf 而不是 dompdf/dompdf
  • 在我的样式表上使用 media="print"
  • 使用 TailwindCSS 的 print:*
  • 试图让 chrome-php 工作无济于事
  • 正在尝试 barryvdh/excel 的 pdf 导出。

没有任何效果,所以我找到的解决方案很糟糕。

  1. 在 Firefox 中打开视图。
  2. 截取整个页面(不仅仅是可见内容)。
  3. 使用我找到的第一个在线 PNG 到 PDF 转换器。

我希望有一种不那么痛苦的方法来做到这一点,但它看起来不像。这个糟糕的解决方案确实有效。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。