Search code examples
javascriptpythoncsspdfpdf-conversion

Programmatically add all Css stlyings from external CSS stylesheets to the html's <head> tag


I am using a python library to convert HTML page into PDF.

It does it correctly, except it only handles inline styling. It does not reflect the styling applied to DOM elements using external style sheets.

So, as a solution I am thinking of adding those CSS styling from all the external CSS stylesheets into the head tag of the html file and then send it to get converted into pdf.

But, I am not sure how? Can anyone give me any ideas or atleast suggestion on how to go around fixing that? Or, if they know a better solution.

Much appreciate


Solution

  • Is the python running outside or client-side? You can examine the solution here @ http://www.xportability.com/XEPOnline/FOTestSuite.html. While this does a lot more, you can reach through that page to the included Javascript. Look for flattenstyle.js for inspiration.

    Because our handling is different, we actually copy a selected div element to another hidden div and "flatten" the style by extracting styles we want. What you could do is run such a javascript on page load and save out the div and not destroy it, then you have most all the print styling in the HTML.