简体   繁体   中英

External css not working while using dompdf in codeigniter

$this->dompdf->setBasePath(realpath(FCPATH . '/assets/plugins/bootstrap/css/bootstrap.css'));

I am unable to call external css while using DOMPdf. I am using DOMPdf library to convert html page into pdf. The file is converting successfully but I'm unable to call external css or an image. How can I fix this issue?

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, eg $_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

You can use the isRemoteEnabled = false property when you create you DomPdf object

$dompdf = new \Dompdf\Dompdf(['isRemoteEnabled' => true]);

You can read in DomPdf github the Options.php file about this option. If isRemoteEnabled is set to true, DOMPDF will access remote sites for images and CSS files as required.

Another option is to include your css styles in your view with an tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM