I am generating PDFs from HTML documents. Styles must be applied using the style="" attribute (inline) currently.
I would like to populate the style attribute with rules being applied to elements by an external stylesheet.
I have explored the possibility outlined here: CSS style to inline style via JavaScript - however, this applied an excessive amount of browser default styles as you can see by inspecting the DIV in my example below.
Is there a way to apply only the styles I have defined in my stylesheet?
function applyStyles(el) {
var styles = getComputedStyle(el);
for(let key in styles) {
let prop = key.replace(/\-([a-z])/g, v => v[1].toUpperCase());
el.style[prop] = styles[key];
}
}
applyStyles(document.getElementById('test-div'));
#test-div {
padding: 1rem;
border: 1px solid #CCC;
border-radius: 3px;
font-family: sans-serif;
box-shadow: 1px 1px 6px rgba(0, 0, 0, 0.15);
}
<div id="test-div">I am the test DIV</div>