I'm writing a function to create and display a pdf file using Dompdf in a Laravel 6 project using PHP 8. I tried with many tutorials and even the official documentation from GitHub for the normal Dompdf version and it's wrapper for Laravel. Here is my pdf generator function code:
private function generate_pdf(){
$html = '
<html>
<body>
<h1>Hello Dompdf</h1>
<h2>MDF</h2>
</body>
</html>
';
$pdf = PDF::loadHTML($html);
return $pdf->stream('welcome.pdf');
}
It works fine but here is the result of the function:
As you can see the elements are overlapping itself on top of each others. I looked for answers but I can't find anything related. Does somebody knows why is this happening?
If somebody have this problem again, I found a solution and it is the worst it can be:
private function generate_pdf(){
$html = '
<html>
<body>
<h1>Hello Dompdf</h1><br/><br/>
<h2>MDF</h2>
</body>
</html>
';
$pdf = PDF::loadHTML($html);
return $pdf->stream('welcome.pdf');
}
Just add
elements to every element in order to give the correct space. It is a nasty solution but it works.
use div tags & align your html components use div to divide html to sections will not have such issues then
Divs didn't work at all, it just made the block-type container and then draw it on top any other element (same case that I showed off). Alignment worked fine but it din't was too helpful to my task. The <br/> tag worked good... I wonder if that problem is happening only in my computer because I installed something wrong or there is a missing dependency, or there is someone else with the same problem 🤔.