如何使用dompdf或任何其他库生成pdf,其中html和php一起编写?

如何使用dompdf或任何其他库生成pdf,其中html和php一起编写?,php,dompdf,Php,Dompdf,因为用php和html编写的完整代码为“$html”赋值是很繁琐的。有办法吗?或者我们可以直接将整个页面制作成pdf格式吗?这取决于您的使用情况。例如,如果要呈现为PDF的页面可以通过web服务器访问,则可以在Dompdf脚本中加载该页面: // include dompdf then ... use Dompdf\Dompdf; $dompdf = new Dompdf(); $dompdf->load_html_file("http://example.com/document.php

因为用php和html编写的完整代码为“$html”赋值是很繁琐的。有办法吗?或者我们可以直接将整个页面制作成pdf格式吗?

这取决于您的使用情况。例如,如果要呈现为PDF的页面可以通过web服务器访问,则可以在Dompdf脚本中加载该页面:

// include dompdf then ...
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->load_html_file("http://example.com/document.php");
$dompdf->render();
$dompdf->stream();
如果内容和Dompdf逻辑需要全部位于同一页面上,则可以使用输出缓冲捕获HTML并将其提供给Dompdf:

ob_start();
// HTML + PHP to create output
$html = ob_get_clean();
ob_end_clean();

// include dompdf then ...
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream();

您可以将整个页面打印为pdf格式,在谷歌上查看“wkhtmltopdf”,您可以使用html2pdf库进行尝试-