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?
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.
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.
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..
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>
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 :)
BlackAura has the right idea. You're appending the generated PDF to the output buffer, and then discarding everything.