php到pdf使用dompdf

问题描述 投票:1回答:4

我有以下代码开始。

<?
require('html2fpdf.php');
$pdf=new HTML2FPDF();
$pdf->AddPage();
$fp = fopen("en/print-job.php?ID=12","r");
$strContent = fread($fp, filesize("en/print-job.php?ID=12"));
fclose($fp);
$pdf->WriteHTML($strContent);
$pdf->Output("home.pdf");
echo "PDF file is generated successfully!";
?>

我有一个名为print-pdf.php的页面,它建立在bootstrap和输出上,如下所示:

https://www.lotomanager.in/en/print-job.php?ID=12

那么如何将此页面转换为pdf?

我从上面的代码得到以下结果:

警告:fopen(en / print-job.php?ID = 12):无法打开流:第5行/home/loto/public_html/pdf.php中没有此类文件或目录

警告:在第6行的/home/loto/public_html/pdf.php中,en / print-job.php的文件大小():stat失败?ID = 12

警告:fread()期望参数1是资源,第6行的/home/loto/public_html/pdf.php中给出了boolean

警告:fclose()期望参数1是资源,第7行/home/loto/public_html/pdf.php中给出的布尔值成功生成PDF文件!

php pdf html2pdf
4个回答
1
投票

试试这个

<?php
require_once 'dompdf/autoload.inc.php';
?>
<?php

// reference the Dompdf namespace

use Dompdf\Dompdf;

// instantiate and use the dompdf class
$html = file_get_contents('http://www.lotomanager.in/en/print-job.php?ID=12');
$dompdf = new Dompdf();
$dompdf->loadHtml($html);

 // (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream();
?>

0
投票

不明白你的意思。如果您打算在文档中显示ID:

$dompdf->loadHtml('hello world ' . $_GET['ID'] );

0
投票

DOMPDF有一个你可以使用的load_html_file方法。为了安全起见,我还将$_GET作为整数投射。

<?php
require_once 'dompdf/autoload.inc.php';
?>
<?php

// reference the Dompdf namespace

use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->load_html_file('https://www.lotomanager.in/en/print-job.php?ID='.(int)$_GET['ID']);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream();
?>

0
投票

你可以通过重构print-job.php来实现这一点,你可以通过某种方式生成HTML,它可以将HTML打印到用户或将其提供给PDF库。

最简单的方法可能是

ob_start();
include('print-job.php');
$html = ob_end_clean();

但这可能会导致两个文件中的不同全局变量或某些内容出现问题,并且您必须小心将来对pront-job.php进行更改。所以说更好的清理那里的代码不要直接打印。

更好的方法是根本不使用dompdf + html,而是使用适当的布局创建PDF。这是更多的工作,但应该给一个更清洁的结果。

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