how to convert dynamic php file to pdf? - php

I try to convert dynamic php database file to pdf.
I try with DOMPDF, but I have a problem with defining the string. I'll explain:
Here is a 'hello world' script for dompdf:
require_once("dompdf_config.inc.php");
$html =
'<html><body>'.
'<p>Hello World!</p>'.
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("hello_world.pdf");
The thing is that instead of a simple
'<html><body>'.
'<p>Hello World!</p>'.
'</body></html>'
I have a long php file full of functions and sql queries. because of it I have inside it many ",',; e.t.c signs. I also have a javascript dynamic chart (jqplot) in this page.
So instead of pdf file I get errors errors errors...
Does anyone has a solution for this?
I will really appreciate any answer, and will be really really thankful for a solution...

First you'll need to generate HTML from your PHP, then pass it to DOMPDF:
<?php
require_once("dompdf_config.inc.php");
ob_start();
require_once("path/to/input/file.php");
$dompdf = new DOMPDF();
$dompdf->load_html(ob_get_clean());
$dompdf->render();
$dompdf->stream("file.pdf");
?>
You can also do a regular HTTP request:
<?php
require_once("dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html_file('http://example.com/file.php');
$dompdf->render();
$dompdf->stream("file.pdf");
?>
If you need JavaScript support, try wkhtmltopdf, it's based on Webkit and does it's work perfectly.

You can use ob_start and ob_get_contents to run PHP code and capture the output as a string.
For the JavaScript chart, though, you're out of luck. DOMPDF is pretty smart, but it's not that smart. You'll need to either use a non-JavaScript chart solution, do without the charts, or use a web browser to generate the PDF.

I am not sure why you need to generate HTML to build a PDF in the first place but as others have suggested, build out your PHP script and then use something like FPDF or TCPDF.
They both build PDFs just fine and can take HTML input.

Try This ...
<?php
ob_start();
require_once("dompdf_config.inc.php");
$file = file_get_contents('http://example.com/file.php');
$dompdf = new DOMPDF();
$dompdf->load_html($file);
$dompdf->render();
$dompdf->stream("filename.pdf");
?>

Related

how to generate pdf with dompdf or any other libraries, where html and php are writen together?

Because assigning a value to the "$html" with whole code which is written with php and html is tedious. Is there any way? or can we make whole page as pdf directly?
It really depends on your use case. For example, if the page you want to render to PDF can be accessed via a web server then you can just load that page in your Dompdf script:
// include dompdf then ...
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->load_html_file("http://example.com/document.php");
$dompdf->render();
$dompdf->stream();
If the content and Dompdf logic needs to all be on the same page then you can use output buffering to capture the HTML and feed it to 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();

HOW TO: convert HTML to PDF with PHP/DOMpdf?

I need your help to fix this:
My current code is this:
<?php
require_once '../dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$html = 'Insert full HTML content';
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream("codexworld",array("Attachment"=>0));
?>
This is the error a get:
Why if I have exactly the same code like the basic example, am I getting this error? What am I missing? I don't find in my folder the Autoloader.php, where do I have to get that file?
Well apparently it's an issue after domPdf has moved to Github. It seems that php-font-lib library doesn't exist. So one solution is to manually download it:
Go to https://github.com/PhenX/php-font-lib and download the library.
In the zip file, take the contents of the src/FontLib/ folder and paste that into the folder lib/php-font-lib.
or you can check this answer here

smarty template and dompdf

I am using smarty template to generate a page on-the-fly that consists of user-submitted form data and uploaded images. I can get to display all the contents as expected. I want the same to be output as PDF. The template is named index.tpl, and is located inside the template folder. I can't figure out how to put these two together. Any help would be appreciated, thanks. I tried the following, but doesn't work. There is no output.
require_once("dompdf/dompdf_config.inc.php");
$html = $smarty->fetch('index.tpl');
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("pdf_file.pdf");
When I checked the error_log, I noticed "PHP Fatal error: Class 'DOMPDF' not found" line. HOWEVER, when I created a simple file as shown below, I get it working perfectly (PDF is generated).
require_once("dompdf/dompdf_config.inc.php");
$html =
'<html><body>'.
'<p>Hello World!</p>'.
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("pdf_file.pdf");
What is happening here? Why the difference?
I think your answer is here http://www.smarty.net/docsv2/en/api.fetch.tpl
// capture the output
$output = $smarty->fetch('index.tpl');
$tmpfile = tempnam("/tmp", "dompdf_");
file_put_contents($tmpfile, $output);
...

Output Buffer + Pdf - PHP

I have a page that uses the glob function and file_get_contents to have a few html files and store them in the buffer.
So I want to convert this buffer ob_get_contents() to an pdf file.
What is the best way to do that? how?
Thanks in advance.
For creating PDF files from HTML and CSS, check out DOMpdf.
While this solution doesn't support the full range of HTML and CSS and its rendering can be a pain sometimes, it has one advantage: it does not require any special binaries to be installed (like wkhtmltopdf). It should run on your average shared PHP hosting.
Usage example:
<?php
require_once("dompdf_config.inc.php");
$html =
'<html><body>'.
'<p>Put your html here, or generate it with your favourite '.
'templating system.</p>'.
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>
why using the outputbuffer for this? you have it in variables using file_get_contents and can simply create your pdf with the data from the variables. when using ob_get_contents all it does is return the outputbuffer and what you normally do with the result is saving into a variable...
btw. you do want to convert html into pdf? If yes have a look at wkhtmltopdf
If ob_get_contents contains html files they are so many solutions out there that can achieve what you want. I think you should look at the following
PrinceXML
FPDF
TCPDF
HTML to PDF converter (PHP5)
wkhtmltopdf
Example using Simple HTML 2 PDF using PHP
$html = ob_get_contents();
ob_end_clean();
$pdf = new HTML2FPDF();
$pdf->SetTopMargin(1);
$pdf->AddPage();
$pdf->WriteHTML($html);
$pdf->Output('test.pdf','D');

PHP - Read dynamically generated (and echoed) HTML into a string?

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

Categories