2

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

user3566643
  • 535
  • 1
  • 4
  • 8

1 Answers1

1

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.

Kevin Brown
  • 8,805
  • 2
  • 20
  • 38
  • Thank you kevin. By Description, this library looks quite promising. I will give it a try. :) – user3566643 Jul 22 '14 at 15:47
  • Kevin - I am not sure how to use it? Is it possible to use this libraru in my web app. Note that, my web app cannot have access to internet except the server on which it is being hosted. So, I need to include any external library in my app physically instead of just making a call to it. please let me know if that is possible – user3566643 Jul 22 '14 at 15:50
  • It changes daily as we work on it. The JS code in that sample, walks the styles for a div and all children and creates a new div with all resolved styles. If that is all you want, you can extract/analyze and modify (that is only the early example, a new version is coming). The only outside connection that needs is for formatting to PDF as it uses an externally hosted REST based XSL FO engine we developed. If you have your own PDF print solution then you do not need that. – Kevin Brown Jul 23 '14 at 02:51
  • Note, if you use Chrome browser, we now replace the original div with the PDF and remove the processed div. You will need to debug to get access to the "intermediate" (modified) HTML div with all styles resolved. – Kevin Brown Jul 23 '14 at 02:54
  • FYI, the JS code to walk the DOM and inject all relevant CSS both internal and external is posted on Github now @ https://github.com/Xportability/css-to-pdf – Kevin Brown Jul 30 '14 at 21:06