Dompdf Doesn't Work with External CSS File

DOMPDF doesn't work with external css file

This has in fact nothing to do with Zend Framework, but you need to supply DomPDF the right path to load the "external" files from.

$dompdf = new DOMPDF();
$dompdf->setBasePath(realpath(APPLICATION_PATH . '/path/to/css/'));
$dompdf->loadHtml($html);
$dompdf->render();

See also the manual of DomPDF for this feature.

External css not working while using dompdf in codeigniter

DomPDF requires the use of absolute paths for anything you are including.

Thus, you need to use the $_SERVER['DOCUMENT_ROOT'] variable and trail it with the rest of the server path that leads to the file, e.g. $_SERVER['DOCUMENT_ROOT'].'assets/plugins/bootstrap/css/bootstrap.css';

Word of warning:
DomPDF's performance decreases radically when embedding large CSS files such as bootstrap (You're looking at a couple of seconds to generate a simple 2-3 page PDF file even if you don't use a lot of formatting). My suggestion would be to inline the specific styles you need in the view rather than linking the full bootstrap library to allow faster dompdf rendering.
I faced this very issue some time ago and posted a question (and answer) here, after I did a lot of research, hoping it might help someone else in the future. I recommend reading Slow PDF generation with PHP+DomPDF

DOMPDF doesn't work with external css file

This has in fact nothing to do with Zend Framework, but you need to supply DomPDF the right path to load the "external" files from.

$dompdf = new DOMPDF();
$dompdf->setBasePath(realpath(APPLICATION_PATH . '/path/to/css/'));
$dompdf->loadHtml($html);
$dompdf->render();

See also the manual of DomPDF for this feature.

Add CSS to DomPDF

the css file must be referenced in the HTML you give to DomPDF.

If you don't want to change your twig template, you can use a workaround like this :

$dompdf = new Dompdf();
$html = $this->renderView('dashboard/user_table.html.twig', [
'users' => $users
]);
$html .= '<link type="text/css" href="/absolute/path/to/pdf.css" rel="stylesheet" />';
$dompdf->loadHtml($html);

Note that adding a link tag to the body is not valid according to HTML specifications. With the current Dompdf version, it works but it may not work in future versions.

CSS not working with DOMPDF

dompdf v0.5.x does not support floats. v0.6.0 does, though it is still an experimental feature that has to be enabled in the configuration. After testing your document I can say that dompdf doesn't handle it very well. If you want to stick with dompdf you might consider tables since your document is somewhat tabular in nature.

Dompdf 0.8.3 not fetching remote CSS in PHP 7.2 and CodeIgniter 3

The problem was due to a port redirection issue in the server. Solved :) Thank you all!



Related Topics



Leave a reply



Submit