You need to login to send post. No account yet? Create one:
Create account

Problem of Dompdf with Drupal CSS compression... solved

No replies
admin

When you are trying use nice (and for sure with best results using CSS) PDF creator dompdf together with Drupal Print module, you'll (after creating PDF) get PHP error message if the CSS compression is enabled.

Sad... Because no web designer wants to give up CSS compression and PDF generation are good looking on drupal website too.

The problem is, that system CSS contain tags which dompdf don't like. Only way is... to switch the system CSS off. With disabled Drupal's CSS compression you will not notice the problem, because author of Print module itself - this is in print_pdf/print_pdf.pages.inc:12:

   // dompdf seems to have problems with something in system.css so let's not use it
   $html = preg_replace('!<link.*?modules/system/system.css.*?/>!', '', $html);

It's working very well when links in HTML looks like this:

<link type="text/css" rel="stylesheet" media="all" href="/modules/system/system.css?x" />

But compressed CSS links looks in Drupal HTML code that:

<link type="text/css" rel="stylesheet" media="all" href="/sites/somesite.co.uk/files/css/css_03e1d09231186369f817e2de7ed4d8a0.css" />

The simpliest solution is... Unlink all CSS and add only one wanted. In this example I suppose module Print isn't in sites/all/modules folder but in folder of site itself, so we can add in print_pdf/print_pdf.pages.inc under '$html = preg...' two new lines:

   $html = preg_replace('!<link.*?/files/css/css.*?/>!', '', $html);
   $html = preg_replace('!<link.*?/sites/all/modules.*?/>!', '', $html);

Half of work is done. Dompdf is quiet and generate nice PDF. Without styling yet.

Now we need add new print.css file. The easiest way is using Settings -> Options in Print module itself, where is field for new CSS, injected when module is working. So: create new CSS, write path there and voilá, dompdf is working :-)