My understanding is that the print.css file should style all the PDF as well as printer friendly versions of output from the module by default. But like others, my PDFs are not getting styled. I saw a few other tickets about this issue, but none of the proposed solutions seemed to work with TCPDF. Does anyone know of a solution that does?

Thanks!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

paulen’s picture

Can't get it working in dompdf either for that matter.

jcnventura’s picture

Title: Print view not using css with TCPDF » Inline the CSS when sending the HTML to TCPDF
Status: Active » Postponed

It works fine in dompdf. However, TCPDF seems to only load CSS when loading the HTML text from a file..

From the documentation, it seems to be able to handle CSS if it is inlined.. I've a (very dirty) hack to inline the CSS (which works well if there's no @include directives), in the print_mail module that could probably be used here..

jcnventura’s picture

Version: 6.x-1.12 » 7.x-2.x-dev
Status: Postponed » Fixed

Committed to 7.x-2.x-dev a mechanism to inline the CSS when creating a PDF.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

george.plescan’s picture

Status: Closed (fixed) » Active

Could you please explain how to make the CSS work. I have the latest version, but the CSS it not being used.

I have put sites/all/modules/print/css/print.css in the Custom stylesheet settings and still nothing.

Thank you.

jcnventura’s picture

Status: Active » Postponed (maintainer needs more info)

I was never able to make the CSS work in TCPDF, and I can't understand why. Even though it's passed inline, and so something like example 61 (http://www.tcpdf.org/examples/example_061.phps) should have worked. Every other lib seems to be able to include CSS.

It seems to be some bug with TCPDF, which should be reported to the maintainer.

jcnventura’s picture

Status: Postponed (maintainer needs more info) » Closed (fixed)

No further info in two weeks. Closing the issue again.

cornel.eftimie’s picture

Hi all, maybe this would help:

  1. create the print_pdf.css file in your_theme/css folder
  2. copy the theme_print_pdf_tcpdf_content() function from print/print_pdf/print_pdf.pages.inc to your theme's template.php file
  3. replace this
    @$pdf->writeHTML($matches[1]);
    

    with this

    @$pdf->writeHTML('
      <head>
    	<link rel="stylesheet" type="text/css" href="' . drupal_get_path('theme', 'your_theme') . '/css/print_pdf.css">
      </head>' . $matches[1]);
    

I have used it only for hiding some elements and it works fine.
I suppose you could add some other css code before /head

Good luck!

msypes’s picture

I'm looking for solutions to this as well.
Comment #8 didn't change anything for me. I have no indication that the copied function overrides the module's version, i.e., I don't think it's being called at all.

NB: Interesting finding, probably documented elsewhere, but I'll post it here in case it helps as well. The overriding function you create must be in your main theme. Initially I was trying this with an administrative view, so put the css and code into that theme's template.php file. Only when I moved them to the main theme did everything work as expected.

dbassendine’s picture

I found the styling would not apply to TCPDF either, and I did some testing using the print_pdf.tpl.php template to see where styles would or would not apply:

  • Theme function - external style sheet (ie. #8 above) - works
  • Template HEAD - external style sheet (ie. [link rel="stylesheet"]) - not effective
  • Template HEAD - internal style sheet (ie. [style]...[/style]) - not effective
  • Template markup - inline style (ie, [tag style="..."]) - works

I'm not clear why an external stylesheet included at this stage works, but it doesn't when referenced in the template HEAD. This definitely needs to be fixed in the default theme function, as TCPDF styling is just not working right now. Perhaps we use the #8 snippet and include:

  • If "use theme stylesheets" is switched on, all theme stylesheets (ie. those from $print['css'])
  • Theme print.css stylesheet
  • Configurable stylesheet path/filename set in a new settings page setting

Would this be the right approach, do you think?

Thanks, David

hockey2112’s picture

When I follow the steps in #8, I receive this error:

Fatal error: Cannot redeclare theme_print_pdf_tcpdf_content() (previously declared in /home/mysite/public_html/sites/all/themes/fusion/fusion_core/template.php:511) in /home/mysite/public_html/sites/all/modules/print/print_pdf/print_pdf.pages.inc on line 576

I wonder if I did something wrong when copying the code over to the theme's template.php file. This is what I added, at the end of that file:

/*ADDED for Print module (PDF version CSS)*/
function theme_print_pdf_tcpdf_content($vars) {
  $pdf = $vars['pdf'];
  // set content font
  $pdf->setFont($vars['font'][0], $vars['font'][1], $vars['font'][2]);

  preg_match('!<body.*?>(.*)</body>!sim', $vars['html'], $matches);
  $pattern = '!(?:<div class="print-(?:logo|site_name|breadcrumb|footer)">.*?</div>|<hr class="print-hr" />)!si';
  $matches[1] = preg_replace($pattern, '', $matches[1]);

  // Make CCK fields look better
  $matches[1] = preg_replace('!(<div class="field.*?>)\s*!sm', '$1', $matches[1]);
  $matches[1] = preg_replace('!(<div class="field.*?>.*?</div>)\s*!sm', '$1', $matches[1]);
  $matches[1] = preg_replace('!<div( class="field-label.*?>.*?)</div>!sm', '<strong$1</strong>', $matches[1]);

  // Since TCPDF's writeHTML is so bad with <p>, do everything possible to make it look nice
  $matches[1] = preg_replace('!<(?:p(|\s+.*?)/?|/p)>!i', '<br$1 />', $matches[1]);
  $matches[1] = str_replace(array('<div', 'div>'), array('<span', 'span><br />'), $matches[1]);
  do {
    $prev = $matches[1];
    $matches[1] = preg_replace('!(</span>)<br />(\s*?</span><br />)!s', '$1$2', $matches[1]);
  } while ($prev != $matches[1]);

@$pdf->writeHTML('
  <head>
    <link rel="stylesheet" type="text/css" href="' . drupal_get_path('fusion', 'fusion_starter') . '/css/print_pdf.css">
  </head>' . $matches[1]);

  return $pdf;
}

Does any of that look "off"?

mxlav’s picture

need to change your're function name from theme_print_pdf_tcpdf_content to fusion_starter_print_pdf_tcpdf_content?

Note I also tried #8 and it picked up the new css..altought it is ignoring a lot of stuff like padding, alignments ets...

gaurav.goyal’s picture

I am using the below code, but still css is not applied on the downloaded pdf.

$pdf = $vars['pdf'];
  // set content font
  $pdf->setFont($vars['font'][0], $vars['font'][1], $vars['font'][2]);

  preg_match('!<body.*

(.*)!sim', $vars['html'], $matches);
$pattern = '!(?:

|

)!si';
$matches[1] = preg_replace($pattern, '', $matches[1]);

// Make CCK fields look better
$matches[1] = preg_replace('!(

)\s*!sm', '$1', $matches[1]);
$matches[1] = preg_replace('!(
.*?

)\s*!sm', '$1', $matches[1]);
$matches[1] = preg_replace('!

.*?)

!sm', '', $matches[1]);

// Since TCPDF's writeHTML is so bad with

, do everything possible to make it look nice
$matches[1] = preg_replace('!<(?:p(|\s+.*?)/?|/p)>!i', '
', $matches[1]);
$matches[1] = str_replace(array('

'), array('
'), $matches[1]);
do {
$prev = $matches[1];
$matches[1] = preg_replace('!()
(\s*?
)!s', '$1$2', $matches[1]);
} while ($prev != $matches[1]);

@$pdf->writeHTML('

' . $matches[1]);

return $pdf;
?>

gaurav.goyal’s picture

Please Ignore the above code.
I am using the below code, but still css is not applied on the downloaded pdf.

$pdf = $vars['pdf'];
// set content font
$pdf->setFont($vars['font'][0], $vars['font'][1], $vars['font'][2]);

preg_match('!(.*)!sim', $vars['html'], $matches);
$pattern = '!(?:

|

)!si';
$matches[1] = preg_replace($pattern, '', $matches[1]);

// Make CCK fields look better
$matches[1] = preg_replace('!(

)\s*!sm', '$1', $matches[1]);
$matches[1] = preg_replace('!(
.*?

)\s*!sm', '$1', $matches[1]);
$matches[1] = preg_replace('!

.*?)

!sm', '', $matches[1]);

// Since TCPDF's writeHTML is so bad with

, do everything possible to make it look nice
$matches[1] = preg_replace('!<(?:p(|\s+.*?)/?|/p)>!i', '
', $matches[1]);
$matches[1] = str_replace(array('

'), array('
'), $matches[1]);
do {
$prev = $matches[1];
$matches[1] = preg_replace('!()
(\s*?
)!s', '$1$2', $matches[1]);
} while ($prev != $matches[1]);

@$pdf->writeHTML('

' . $matches[1]);

return $pdf;

NIKS_Artreaktor’s picture

gaurav.goyal - read carefully #8

you didn't change function - step 3.

@$pdf->writeHTML('
  <head>
    <link rel="stylesheet" type="text/css" href="' . drupal_get_path('theme', 'your_theme') . '/css/print_pdf.css">
  </head>' . $matches[1]);

and fix carefully "your_theme" in drupal_get_path('theme', 'your_theme')
----------------------------------------------------------------------------------

And want to say to everyone who tried fix in #8 and didn't get result - first time i didn't get result
i did everything right, but i don't know why it gave not effect.

After i install
mPDF library handler
and library

and tried to change to use TCPDF again - i so that it changed...

so be attentive - to don't get my mistake - check changes carefully.

thanks for fix.

P.S.
mPDF library - it seams to me better
- utf-8 (unicode) - Okey
- images - it seems to me more original ( in TCPDF - it was like bigger because of wrong sizes and quality of image was worse).

j.johnson.bbt’s picture

Not getting this to work. Tried this at the bottom of my theme template.php file but no success. What's wrong?

function Elimai_print_pdf_tcpdf_content($vars) {
  $pdf = $vars['pdf'];
  // set content font
  $pdf->setFont($vars['font'][0], $vars['font'][1], $vars['font'][2]);

  preg_match('!<body.*?>(.*)</body>!sim', $vars['html'], $matches);
  $pattern = '!(?:<div class="print-(?:logo|site_name|breadcrumb|footer)">.*?</div>|<hr class="print-hr" />)!si';
  $matches[1] = preg_replace($pattern, '', $matches[1]);

  // Make CCK fields look better
  $matches[1] = preg_replace('!(<div class="field.*?>)\s*!sm', '$1', $matches[1]);
  $matches[1] = preg_replace('!(<div class="field.*?>.*?</div>)\s*!sm', '$1', $matches[1]);
  $matches[1] = preg_replace('!<div( class="field-label.*?>.*?)</div>!sm', '<strong$1</strong>', $matches[1]);

  // Since TCPDF's writeHTML is so bad with <p>, do everything possible to make it look nice
  $matches[1] = preg_replace('!<(?:p(|\s+.*?)/?|/p)>!i', '<br$1 />', $matches[1]);
  $matches[1] = str_replace(array('<div', 'div>'), array('<span', 'span><br />'), $matches[1]);
  do {
    $prev = $matches[1];
    $matches[1] = preg_replace('!(</span>)<br />(\s*?</span><br />)!s', '$1$2', $matches[1]);
  } while ($prev != $matches[1]);

 @$pdf->writeHTML('
  <head>
    <link rel="stylesheet" type="text/css" href="' . drupal_get_path('Elimai', 'Elimai') . '/css/print_pdf.css">
  </head>' . $matches[1]);

  return $pdf;
}
cobenash’s picture

Issue summary: View changes
FileSize
2.55 KB

This Patch provide custom pdf css settings.

Go to /admin/config/user-interface/print/pdf/tcpdf

Input the custom pdf css path and then tcpdf will get the css file.