Problem/Motivation

The DomPDF library only supports CSS2 and some CSS3 properties as indicated on its project page. This is fine, but in its current implementation this module completely stops processing when an unrecognised CSS3 property is encountered with a bunch of errors like this:

Error generating document: Failed to generate PDF: 'box_sizing' is not a recognized CSS property., 'hyphens' is not a recognized CSS property., 'hyphens' is not a recognized CSS property., 'text_shadow' is not a recognized CSS property.

Steps to reproduce

I wish my PDF to look like the regular website, so I feed the same CSS as the regular website (to avoid having to maintain two different stylesheets).

I have created a subtheme of Bartik, and use its global-styling library as entity print CSS like so:

# mytheme.info.yml
...
base theme: bartik

libraries-extend:
  bartik/global-styling:
    - mytheme/custom-styling
    
entity_print:
  node:
    all: 'bartik/global-styling'

After this, PDF generation is broken and clicking the link redirects back to the entity and the above errors are shown in a warning message. When using the debug path, all is fine and looks great.

Proposed resolution

The DomPdf library uses two global variables for processing all errors encountered when rendering the PDF: $_dompdf_warnings and $_dompdf_show_warnings. All encountered issues are writting to the first, and the second variable indicates if they should be shown or not. This is FALSE by default. DomPDF happily parses what it can and returns a PDF file.

The DomPDF plugin that comes with this module does not take the global $_dompdf_show_warnings into account and only reads $_dompdf_warnings and is therefor inconsistent with the behavior of the library. There is no way to suppress these warnings and users never get to see the PDF that was created.

I believe that Drupal\entity_print\Plugin\EntityPrint\PrintEngine\DomPDF::getError() should check if $_dompdf_show_warnings is true, or maybe better, use a module configuration value to determine if error processing is desired. It could be as easy as this:

  /**
   * {@inheritdoc}
   */
  protected function getError() {
    global $_dompdf_warnings, $_dompdf_show_warnings;
    if (is_array($_dompdf_warnings) && $_dompdf_show_warnings) {
      return implode(', ', $_dompdf_warnings);
    }
    return FALSE;
  }

I could also argue that warnings should not be treated as errors, but I can understand that this module does not make this differentiation.

Remaining tasks

Decide on the solution.

User interface changes

Possibly a debug setting checkbox?

API changes

None.

Data model changes

None.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Neograph734 created an issue. See original summary.

neograph734’s picture

Issue summary: View changes
larowlan’s picture

Hey, I'm keeping the lights on here, and don't have much insight into how DomPDF works.

So once I get over the vomit in the back of my throat from the use of globals in 2022, I'm happy to accept patches that make this configurable or less brittle.

neograph734’s picture

Thanks, I'll see what I can do. I think it will be a checkbox in the dompdf configuration to toggle these warnings.

Ideally you would want these warnings to show on the debug page and not during rendering (maybe watchdog, but still some suppression option is needed to avoid flooding the logs). I need to dig into this module more to see what it can do.

neograph734’s picture

The current fork should at least stop the main issue from occuring.

I am still trying to figure out how to properly show the CSS warnings as debug messages, but it is diffcult to find a generic approach. Maybe that should be a follow-up issue..

neograph734’s picture

Status: Active » Needs review
larowlan’s picture

Status: Needs review » Needs work

Left some comments on the MR

apaderno made their first commit to this issue’s fork.

gwvoigt’s picture

StatusFileSize
new1.79 KB

In case you are using https://www.drupal.org/project/entity_print/issues/2823430#comment-13909604, this patch will fail. So here's one that will work with that other issue combined

avpaderno’s picture

Status: Needs work » Needs review
larowlan’s picture

Status: Needs review » Needs work

Still NW from previous review

mrshowerman made their first commit to this issue’s fork.

neograph734’s picture

Status: Needs work » Needs review

I think this resolves the earlier mentioned issues (I too think that the aliasing makes sense.). So setting Needs review.
Thanks mrshowerman!

  • larowlan committed 0f15fb0d on 8.x-2.x authored by Neograph734
    Issue #3323264 by Neograph734, larowlan, apaderno, gwvoigt, mrshowerman...
larowlan’s picture

Status: Needs review » Fixed

Thanks, cutting 8.x-2.13 with this in it

larowlan’s picture

Status: Fixed » Closed (fixed)

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