i have a .php page with connection to a MySql BD to read some information and then I want to convert it to PDF to print the page. My question is, may I use DomPdf ? Or it only work for only html code? Thank you!
Asked
Active
Viewed 277 times
0
-
Your php code generates html, right ? Anyway, I don't especially know DomPDF but most libraries allow to render whatever you like, such as the content of a variable. – Jules R Nov 29 '18 at 10:25
-
Yes it also renders html (with `echo` ) but I also have some `if` and `while` code and some `query` too – Carlos Santiago Nov 29 '18 at 10:32
-
But it hasn't to be DomPDF, u know any other lib that allow to generate pdf from an .php page? – Carlos Santiago Nov 29 '18 at 10:33
-
I've personally used [PhantomJS](http://phantomjs.org/screen-capture.html) for generating PDFs from urls before, so it might work for you. – Ethan Nov 29 '18 at 10:36
-
use chrome headless - to convert your page to pdf. – lazzy_ms Nov 29 '18 at 10:49
-
@David PhantomJS works for html and php code together? – Carlos Santiago Nov 29 '18 at 10:50
-
@lazzy_ms that way the URL and the Page number stays on PDF, and I dont want that.. – Carlos Santiago Nov 29 '18 at 11:53
-
@CarlosSantiago wkhtmltopdf is also a good option. – lazzy_ms Nov 29 '18 at 11:58
1 Answers
0
You can use dynamic data like this. change fields in $html dynamically.
$html = "<table border='1' width='100%' style='border-collapse: collapse;'>
<tr>
<th>Username</th><th>Email</th>
</tr>
<tr>
<td>yssyogesh</td>
<td>yogesh@makitweb.com</td>
</tr>
<tr>
<td>sonarika</td>
<td>sonarika@gmail.com</td>
</tr>
<tr>
<td>vishal</td>
<td>vishal@gmail.com</td>
</tr>
</table>";
// include autoloader
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
$output = $dompdf->output();
file_put_contents("file.pdf", $output);

Ajit Maurya
- 56
- 1
- 9
-
That's actually what i've code haha, but I need to put some php code on the $html var and it give always an error :/ – Carlos Santiago Nov 29 '18 at 10:49
-
What is the error ? can you please share, might be i can help you in this regards. – Ajit Maurya Nov 29 '18 at 10:50
-
well, here is a solution for your error [click here](https://stackoverflow.com/questions/37521775/dompdf-error-no-block-level-parent-found-not-good) – Ajit Maurya Nov 29 '18 at 10:59
-
It may be that, but I don't have the file that they talk about :/ – Carlos Santiago Nov 29 '18 at 11:06