PHP-将动态生成的(和回显的)HTML读入字符串? [英] PHP - Read dynamically generated (and echoed) HTML into a string?
问题描述
我有一个文件,可以从数据库中提取一些信息并创建一些相对简单的动态HTML。
I have a file that pulls some information from the database and creates some relatively simple dynamic HTML.
然后DOMPDF可以使用该文件将其转换为PDF文件。 DOMDPF使用GET变量来实现基本实现。
The file can then be used by DOMPDF to turn it into a PDF document. DOMDPF uses GET variables to achieve the basic implementation.
ob_start();
include_once("report.php?id=1249642977");
require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html(ob_get_contents());
$dompdf->render();
$dompdf->stream("sample.pdf", array('Attachment'=>'0'));
ob_end_clean();
我想我可能可以使用一些类似的方法来实现目标,但是并不奇怪
I thought I might be able to use something ilke that to achieve the aim but unsurprisingly it didn't work.
那么,如果您要直接将文件加载到DOMPDF类中使用的字符串中,那么如何读取输出到浏览器的HTML?
So how can I read the HTML that gets output to the browser if you were to load the file directly, into a string for use with the DOMPDF class?
推荐答案
两个问题。
首先,您不能将类似于使用include_once的PHP页面-查询字符串将被忽略。您必须以其他方式将id赋予report.php。
First, you can't call a PHP page like that using include_once - the query string will be ignored. You have to give the id to report.php some other way.
第二,您正确地缓冲了输出。但是,您要将当前的输出缓冲区传递给DOMPDF,告诉它生成PDF到输出缓冲区,并丢弃该输出缓冲区。您可能想要这样的东西:
Second, you're correctly buffering the output. However, you're passing the current output buffer to DOMPDF, telling it to generate a PDF to the output buffer, and the discarding the output buffer. You probably want something like:
$dompdf = new DOMPDF();
$dompdf->load_html(ob_get_clean());
$dompdf->render();
$dompdf->stream("sample.pdf", array('Attachment'=>'0'));
这将获得当前的输出缓冲区,将其丢弃并禁用输出缓冲。然后$ dompdf-> stream应该可以工作。
That'll get the current output buffer, discard it, and disable output buffering. The $dompdf->stream should then work.
这篇关于PHP-将动态生成的(和回显的)HTML读入字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!