PHP - 将动态生成(和回显)的 HTML 读取到字符串中?

发布于 2024-07-30 05:57:24 字数 515 浏览 1 评论 0原文

我有一个文件,它从数据库中提取一些信息并创建一些相对简单的动态 HTML。

然后 DOMPDF 可以使用该文件将其转换为 PDF 文档。 DOMDPF 使用 GET 变量来实现基本实现。

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();

我想我也许可以使用类似的东西来实现目标,但毫不奇怪,它不起作用。

那么,如果您要直接将文件加载到与 DOMPDF 类一起使用的字符串中,我如何读取输出到浏览器的 HTML?

I have a file that pulls some information from the database and creates some relatively simple dynamic HTML.

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

晨曦÷微暖 2024-08-06 05:57:25

BlackAura 的想法是正确的。 您将生成的 PDF 附加到输出缓冲区,然后丢弃所有内容。

BlackAura has the right idea. You're appending the generated PDF to the output buffer, and then discarding everything.

笨笨の傻瓜 2024-08-06 05:57:24

只需使用 PHP 的 file_get_contents 即可:

$page_html = file_get_contents("page.php?id=number");

然后将 $page_html 传递给 dompdf。

希望这可以帮助 :)

Simply use PHP's file_get_contents as such:

$page_html = file_get_contents("page.php?id=number");

then pass $page_html to dompdf.

Hope this helps :)

在巴黎塔顶看东京樱花 2024-08-06 05:57:24

两个问题。

首先,您不能像使用 include_once 那样调用 PHP 页面 - 查询字符串将被忽略。 您必须以其他方式将 id 提供给report.php。

其次,您正确缓冲了输出。 但是,您将当前输出缓冲区传递给 DOMPDF,告诉它生成 PDF 到输出缓冲区,然后丢弃输出缓冲区。 您可能想要类似的东西:

$dompdf = new DOMPDF();
$dompdf->load_html(ob_get_clean());
$dompdf->render();
$dompdf->stream("sample.pdf", array('Attachment'=>'0'));

这将获取当前的输出缓冲区,丢弃它,并禁用输出缓冲。 然后 $dompdf->stream 应该可以工作。

Two problems.

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.

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'));

That'll get the current output buffer, discard it, and disable output buffering. The $dompdf->stream should then work.

晚风撩人 2024-08-06 05:57:24

BlackAura 走在正确的轨道上。 正如其他人所建议的那样,File_get_contents 不会传递 GET 变量(或 POST)。 但您可以使用 cURL 来做到这一点。 下面的代码应该可以工作:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://YOURFULLURL.COM/report.php?id=1249642977');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$my_html = curl_exec($ch);
curl_close($ch);

$dompdf = new DOMPDF();
$dompdf->load_html($my_html);
$dompdf->render();
$dompdf->stream("sample.pdf", array('Attachment'=>'0'));

希望有帮助。

-凯文

BlackAura is on the right track. File_get_contents, as others have suggested will not pass along the GET vars (or POST). But you can use cURL to do that. The code below should work:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://YOURFULLURL.COM/report.php?id=1249642977');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$my_html = curl_exec($ch);
curl_close($ch);

$dompdf = new DOMPDF();
$dompdf->load_html($my_html);
$dompdf->render();
$dompdf->stream("sample.pdf", array('Attachment'=>'0'));

Hope that helps.

-Kevin

长发绾君心 2024-08-06 05:57:24

使用简单的 HTTP 请求来获取 HTML 文件并将其加载到 $dompdf 对象中怎么样:

   <?php
    //...
    $dompdf->load_html(file_get_contents("http://yoursite.net/report.php?id=1249642977"));
   //...
   ?>

我不明白为什么这里需要输出缓冲..

What about using a simple HTTP request to get the HTML file and than loading it in the $dompdf object:

   <?php
    //...
    $dompdf->load_html(file_get_contents("http://yoursite.net/report.php?id=1249642977"));
   //...
   ?>

I don't see why you need output buffering here..

荒人说梦 2024-08-06 05:57:24

我最终决定更改生成 HTML 的 php 页面,使其成为返回 HTML 字符串的函数。 这更多的是一种解决方法而不是解决方案。

未来访问此页面的 Google 员工应该知道,DOMPDF 允许您使用以下方式在页面上运行内联 PHP:

<script type="text/php">
   //some PHP here
</script>

I eventually decided to alter the php page that produces the HTML so that it becomes a function returning an HTML string. This is more of a workaround than a solution.

Future Googlers to this page should be aware that DOMPDF allows you to run inline PHP on a page using:

<script type="text/php">
   //some PHP here
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文