dompdf 相关问题

用PHP编写的HTML到PDF转换器

Dompdf 显示尼泊尔语字符的问题

我正在使用 Dompdf 为我的尼泊尔内容创建 pdf。我在我的控制器中编写了以下代码: 公共函数generatePDF(请求$请求){ $data = $this->news->where('slug', $r...

回答 2 投票 0

如何使用 dompdf 获取多个不同大小的 pdf

我有一个 php 文件。我想用dompdf将php页面中的html代码转换为pdf。 我的PHP页面是这样的; 帕格... 我有一个 php 文件。我想用dompdf将php页面中的html代码转换为pdf。 我的PHP页面是这样的; <!--First Page--> <div class="container"> Page 1 </div> <div class="page-break></div> <!--Second Page--> <div class="container"> Page 2 </div> <div class="page-break></div> <!--Third Page--> <div class="container"> Page 3 </div> <div class="page-break></div> .... <!--Sixth Page--> <div class="container"> Page 6 </div> 我想将此 php 页面转换为 pdf,如下所示。 页面尺寸横向, 页面尺寸横向, 页面尺寸横向, 页面尺寸纵向, 页面尺寸纵向, 页面尺寸纵向 你能帮忙吗? 我可以使用 dompdf 将 php 页面中的 html 代码转换为 pdf 文件。但是,我无法使我的 php 页面中的 html 页面水平和垂直。 我的PHP页面是这样的; -html- -div-容器 第1页 -div-分页符 -div-容器 第2页 -div-分页符 -div-容器 第3页 -div-分页符 。 . -div-容器 第 6 页 我想将此 php 页面转换为 pdf,如下所示。 1.页面尺寸横向,2.页面尺寸横向,3.页面尺寸横向,4.页面尺寸纵向,5.页面尺寸纵向,6.页面尺寸纵向

回答 1 投票 0

使用 dom pdf 打开时生成的 pdf 已损坏

我正在从数据模态传递后变量。我使用的查询已经过检查并且是正确的。我对如何定义变量 $html 感到困惑 这是我的代码 我正在从数据模态传递后变量。我使用的查询已经过检查并且是正确的。我对如何定义变量感到困惑$html 这是我的代码 <?php require_once __DIR__ . "/../functions.php"; require_once __DIR__ . "/../dompdf/autoload.inc.php"; use Dompdf\Dompdf; if (isset($_POST['exprt'])) { $html = '<!DOCTYPE html> <html> <center> <div class="icon-centered"> <img style="object-fit: contain; margin-left: 40px;" src="' . $init->base_url('assets/img/profil_rs/' . $klinik->foto_rs) . '" alt="Logo" width="13%"> </div> <h1>Sensus Harian Rawat Inap <br /> RSPAD Gatot Soebroto</h1> </center> <!-- Table --> <div class="table-responsive datatable-custom"> <table border="1"> <thead>'; foreach ($arrDisplay as $date => $pasien): $html .= ' <tr> <td colspan="12" align="center"> ' . print($date) . ' </td> </tr> <tr> <th>No</th> <th>Nomor Registrasi</th> <th>Nomor Rekam Medis</th> <th>Nama Pasien</th> <th>Umur</th> <th>Poliklinik</th> <th>Dokter</th> <th>Ruang Perawatan</th> <th>Tanggal Masuk</th> <th>Tanggal Keluar</th> <th>Lama Dirawat</th> <th>Cara Keluar</th> </tr> </thead> <tbody>'; $no = 1; foreach ($pasien as $s): $datemasuk = new DateTime($s->tglmasuk); $datekeluar = new DateTime($s->tglkeluar); $age = $init->findage($s->tglahirP); $lamarawat = $init->lamarawat($s->tglmasuk, $s->tglkeluar); $html .= ' <tr role="row" class="even"> <td> ' . $no . ' </td> <td> ' . $s->no_register . ' </td> <td> ' . $s->norm . ' </td> <td> ' . $s->nmpasien . ' </td> <td> ' . $age . ' </td> <td> ' . $s->nmpoli . ' </td> <td> ' . $s->nmdokter . ' </td> <td> ' . $s->nmruang . ' </td> <td> ' . $datemasuk->format('d/m/Y H:i:s') . ' </td> <td> ' . $datekeluar->format('d/m/Y H:i:s') . ' </td> <td> ' . "$lamarawat hari" . ' </td> <td> ' . $s->nmcrkeluar . ' </td> </tr>'; $no++; endforeach; $html .= '</tbody>'; endforeach; $html .= '</table> </div> <!-- End Table --> </body> </html>'; $dompdf = new Dompdf(); $dompdf->loadHtml($html); $savein = 'download/Document/'; $dompdf->setPaper('A4', 'Landscape'); $dompdf->render(); // Download pdf $dompdf->stream($filename, ["Attachment" => 0]); } ?> 我尝试过ob_start(),但没用 我的代码有问题吗? 你的语法有很多错误,你的开始标签在循环之外,但它们的结束标签在循环内。 不漂亮,但试试这个: <?php require_once __DIR__ . "/../functions.php"; require_once __DIR__ . "/../dompdf/autoload.inc.php"; use Dompdf\Dompdf; if (isset($_POST['exprt'])) { $html = ' <!DOCTYPE html> <html> <center> <div class="icon-centered"> <img style="object-fit: contain; margin-left: 40px;" src="' . $init->base_url('assets/img/profil_rs/' . $klinik->foto_rs) . '" alt="Logo" width="13%"> </div> <h1>Sensus Harian Rawat Inap <br /> RSPAD Gatot Soebroto</h1> </center> <!-- Table --> <div class="table-responsive datatable-custom"> <table border="1">'; if (!empty($arrDisplay) && is_array($arrDisplay)) foreach ($arrDisplay as $date => $pasien): $html .= ' <thead> <tr> <td colspan="12" align="center"> ' . print($date) . ' </td> </tr> <tr> <th>No</th> <th>Nomor Registrasi</th> <th>Nomor Rekam Medis</th> <th>Nama Pasien</th> <th>Umur</th> <th>Poliklinik</th> <th>Dokter</th> <th>Ruang Perawatan</th> <th>Tanggal Masuk</th> <th>Tanggal Keluar</th> <th>Lama Dirawat</th> <th>Cara Keluar</th> </tr> </thead> <tbody> '; $no = 1; if (!empty($pasien) && is_array($pasien)) foreach ($pasien as $s): $datemasuk = new DateTime($s->tglmasuk); $datekeluar = new DateTime($s->tglkeluar); $age = $init->findage($s->tglahirP); $lamarawat = $init->lamarawat($s->tglmasuk, $s->tglkeluar); $html .= ' <tr role="row" class="even"> <td> ' . $no . ' </td> <td> ' . $s->no_register . ' </td> <td> ' . $s->norm . ' </td> <td> ' . $s->nmpasien . ' </td> <td> ' . $age . ' </td> <td> ' . $s->nmpoli . ' </td> <td> ' . $s->nmdokter . ' </td> <td> ' . $s->nmruang . ' </td> <td> ' . $datemasuk->format('d/m/Y H:i:s') . ' </td> <td> ' . $datekeluar->format('d/m/Y H:i:s') . ' </td> <td> ' . "$lamarawat hari" . ' </td> <td> ' . $s->nmcrkeluar . ' </td> </tr> '; $no++; endforeach; $html .= '</tbody>'; endforeach; $html .= '</table> </div> <!-- End Table --> </body> </html> '; $dompdf = new Dompdf(); $dompdf->loadHtml($html); $savein = 'download/Document/'; $dompdf->setPaper('A4', 'Landscape'); $dompdf->render(); // Download pdf $dompdf->stream($filename, ["Attachment" => 0]); } ?>

回答 1 投票 0

未找到 DomPDF 的类

当我在控制器中使用为 Facades::class 创建的别名时,遇到“使用未知类”错误。 $pdf = PDF ::loadView('测试', $data); 这行表示 PDF 是未知类别...

回答 1 投票 0

如何使用Dompdf获取多个页面和不同尺寸(纵向,横向)

我在单个 html 文件中有六个 html 页面,我想使用 dompdf 转换为 pdf,如下所示; 页面尺寸横向, 页面尺寸横向, 页面尺寸横向, 页面尺寸纵向, 页面大小端口...

回答 1 投票 0

如何使用Dompdf获取多个页面

我在 Magento 2 中使用 dompdf 下载发票的多个 PDF,但只得到一页。有人可以帮帮我吗? $domPdf = new Dompdf(); $domPdf->loadHtml($html); $domPdf->

回答 2 投票 0

使用DomPDF在生成的PDF上获取PNG

我在使用 DOMPDF 在生成的 PDF 上显示图片时遇到问题。我的DomPDF的版本是v2.0.3。我正在将 XAMPP 用于本地主机。 这是输出 在此输入图像描述 ...

回答 1 投票 0

Laravel Vapor + Docker,DomPDF 不渲染自定义字体,在本地工作,而不是在登台上

我在使用 DomPDF 和 Laravel Vapor 获取自定义字体时遇到一些问题。 (本地字体有效) 这些是我的 CSS 字体定义(字体存储在 public/fonts 内) @font-face { ...

回答 1 投票 0

DomPDF:图像不可读或为空

出于某种原因,DomPDF 不会渲染正在解析的 html 中包含的图像: 但是,当图像作为 html 返回时,图像会呈现在页面上: 我已经研究过这些问题并且有

回答 12 投票 0

当我将 docx 转换为 pdf 时,它失去了风格

当我将 docx 文件转换为 pdf 文件时,它已转换,但没有样式和阿拉伯字母,它变得像“???” 这是代码: 公共函数attr(请求$请求) { /* 设置 PDF En...

回答 1 投票 0

Laravel - 将使用 TailwindCSS 样式的视图输出为 PDF

我正在尝试从视图生成pdf,但样式就是不出来。我尝试过使用 3 个不同的库,但结果并没有太大不同。我错过了什么吗? 看法 我正在尝试从视图生成 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 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 mpdf export method protected function mpdfImplementation() { $mpdf = new Mpdf; $mpdf->WriteHTML(view('view')->render()); $mpdf->output(); } mpdf export result 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 如果没有 css 内联,是否无法将视图导出为 pdf? 我是否最好手动截取整页屏幕截图,将其粘贴到文本文档中并将其另存为 pdf 文件? 这个包的作用就像一个魅力(就像通常的 spatie 一样): https://github.com/spatie/browsershot 根据医生的说法: 该软件包可以将网页转换为图像或 pdf。转换 是由 Puppeteer 在幕后完成的,它控制着一个无头的 谷歌浏览器版本。 如果您需要支持现代CSS功能,例如flexbox(我想您的Tailwind示例使用了flexbox),那么最好使用headless 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 库可以利用其相对于浏览器方法的一些优势 - 颜色处理、预打印、条形码支持、页眉和页脚、页码、目录等,但考虑到它们对 HTML 和 CSS 的支持,需要定制模板和很可能需要样式表。 当然,当您无法通过托管提供商运行除 PHP 之外的其他软件时,上一段也适用。 这个答案可能会解决问题: https://stackoverflow.com/a/36695827/5540654 基本上,您需要在文档的 base 中添加 head 标签。 <html> <head> ... <base href="http://www.example.com/"> <link href="/assets/css/style.css" rel="stylesheet"> ... </head> 使用 wkhtmltopdf 为我工作。可能是类似的问题。 之后 尝试barryvdh/laravel-dompdf而不是dompdf/dompdf 在我的样式表上使用media="print" 使用 TailwindCSS 的 print:* 类 试图让chrome-php工作却无济于事 尝试 barryvdh/excel 的 pdf 导出。 没有任何效果,所以我达到的解决方案是一个糟糕的解决方案。 在 Firefox 中打开视图。 截取整个页面(不仅仅是可见内容)。 使用我找到的第一个在线 PNG 到 PDF 转换器。 我希望有一种不那么痛苦的方法来做到这一点,但它看起来不像。这个糟糕的解决方案实际上有效。

回答 4 投票 0

如何下载 Laravel 中 ajax 调用请求的控制器生成的 PDF

我无法直接下载 PDF 文件。 本质上,用户在他们填写/选择的页面上有一些输入和选项,然后这些数据通过 A...

回答 1 投票 0

服务器上的 DOMpdf

我在本地主机上安装了 DOMpdf(WAMP 和 dompdf 从 git 下载)并且工作正常。安装是使用 Composer 完成的。当我安装在本地主机上时,有一个子目录

回答 1 投票 0

为什么日语字符串会在 PDF 中产生意外的换行符?

以下代码是pdf的左标题div。 虽然东京都江东区潮见2-9-15内田洋行幕潮见オフィス市区町村名市区町村名市区町村是一行字符串,但没想到在东京都江东区潮见2-9-之后就断成了两行当它是...

回答 1 投票 0

如何处理CSS中元素之前的分页符?

我想在pdf的每页上每隔第三个项目分页。为了打破页面,我使用了 page-break-before 属性。但问题是设计从第二页开始就被破坏了......

回答 1 投票 0

(已解决)PDF 在 adobe acrobat 中显示空白页

你好,我在codeigniter 3中使用dompdf将html转换为pdf,当我下载pdf并在pdf阅读器浏览器中打开时,会显示pdf。 pdf 在浏览器中 但是当我在 adobe acrobat 中打开 pdf 时,pdf 就...

回答 1 投票 0

PDF 在 adobe acrobat 中显示空白页

你好,我在codeigniter 3中使用dompdf将html转换为pdf,当我下载pdf并在pdf阅读器浏览器中打开时,会显示pdf。 pdf 在浏览器中 但是当我在 adobe acrobat 中打开 pdf 时,pdf 就...

回答 1 投票 0

使用 DOMPDF 获取 PDF 中的本地图像

我是 PHP 新手,正在尝试为我的作业构建一个小项目。 我正在尝试使用 PHP(DOMPDF 包)将一些 HTML 转换为 PDF。 HTML 确实包含一些标签来输出一些图像......

回答 1 投票 0

使用dompdf无法显示中文文本

你好我想使用dompdf将html转换为pdf,我的HTML有中文文本。在 HTML 视图中,中文文本显示得很好,但当我转换为 pdf 时,中文文本仅显示正方形。 这是我的代码 $

回答 1 投票 0

如何在带有 dompdf 的 PDF 中使用 bootstrap?

我正在尝试生成一个包含数据库信息的报告,并且我可以使用 HTML 和 boostrap 正确显示它,但是当我尝试渲染我的 html 文件时,引导程序停止工作。 我正在尝试生成一个包含来自数据库的信息的报告,并且我可以使用 HTML 和 boostrap 正确显示它,但是当我尝试渲染我的 html 文件时,引导程序停止工作。 <?php //this is my html: ob_start(); ?> <!-- All my file goes here, including the <link></link> to the boostrap --> <?php $html = ob_get_clean() ?> 然后,在另一个文件(我用来创建 PDF)中我这样做了: <?php require_once 'route/to/dompdf/autoload.inc.php'; use Dompdf\Dompdf; $dompdf = new Dompdf(); include "firstFileWithHtml.php"; $options = $dompdf->getOptions(); $options->set(array('isRemoteEnabled' => true)); $dompdf->setOptions($options); $dompdf->loadHtml($html); $dompdf->setPaper("letter"); $dompdf->render(); $dompdf->stream("file_pdf", array("Attachment" =>false)); ?> 它会生成 PDF 预览,只是没有 boostrap。 我缺少一个选项吗? 我尝试下载 bootstrap,尝试使用其他版本(4.3.1、5.0.2、5.3.0),我尝试使用 CDN,但没有任何效果。 我也尝试了这个答案中的解决方案,但是将CSS放在同一个文件中不起作用,并且方法set_base_path已被弃用。 我找到了解决方案,感谢@ceejayoz 的提示。 如果我这样放置 href: <link rel="stylesheet" href=""../user/bootstrap-5.0.2-dist/css/bootstrap.min.css"> 没成功。 但如果我用这个代替: <link rel="stylesheet" href="http://<?php echo $_SERVER['HTTP_HOST']; ?>/PROJECT/user/bootstrap-5.0.2-dist/css/bootstrap.min.css"> 它就像一个魅力。 我能够使 bootstrap 5 工作。我正在做一个 Laravel 项目,需要 dompdf。 首先,获取Bootstrap 5缩小版的内容 $bootstrap5 = file_get_contents(public_path() . '/css/bootstrap.min.css'); 然后将“$bootstrap5”传递到$data变量中 $data = [ 'professions' => $professions, 'logo' => $imageLogo, 'css' => $bootstrap5, ]; 然后用$data加载PDF $pdf = Pdf::setPaper('letter', 'portrait')->setOptions(['dpi' => 100, 'defaultFont' => 'Nunito, sans-serif', 'isHtml5ParserEnabled' => true, 'isRemoteEnabled' => true, 'isJavascriptEnabled' => true]) ->loadView('reports.staffing-report', $data); 在我的staffing-report.php中,我在内部添加了一个样式标签,并从$data中呼应了“css”: <style> {{ $css }} </style>

回答 2 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.