html2pdf.js: @media print (or @media not print) in Css ignored
When using @media print
and @media not print
in the CSS in order to get rid of color, images … the generated pdf will ignore these.
On https://jsfiddle.net/wb2tgm6f/l the generated pdf still includes the
as visible text
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 5
- Comments: 15 (1 by maintainers)
Okay, forgot it… I had to run the removal of class after html2pdf function had completely run.
It’s not perfect (there’s a little subtle change in layout, of course) but whatever… 😉
This isn’t printing, so the print media query won’t get triggered. The workaround I’ve used is through the css preprocessor I’m using, I duplicate the print styles on both
@media print
andbody.print
. Then I add theprint
class to the body right before triggering html2pdf, and then removing it right after.Hello!
I’ve investigated and I think this is doable. It would require altering the stylesheets in html2canvas’s cloned document, which we can access with html2canvas’s
onclone
callback. In there, we could put something like:Needs testing in all relevant browsers. More info here - I stripped down the example code there to just what we would need.
Not sure if there’s any possible optimisation - two thoughts:
media=
value that doesn’t contain the desired medium (e.g. print). I don’t know enough about it to say whether that’s safe.@media print
inside of amedia="print"
stylesheet?). But again, I don’t know all the details.I’ll put this on the to-do list. I’m planning another change that will require tapping into html2canvas’s
onclone
, so I’ll attach this to that project.