Search code examples
phphtmlcsscodeigniterdompdf

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?


Solution

  • 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