Moj*_*imi 15 javascript css wkhtmltopdf python-pdfkit
我正在尝试使用pdfKit
从html文件创建pdf文件来在python中设置服务.
所以基本上我会将我的元素作为字符串发送并期望服务器返回它的pdf版本,但为了准确表示我还需要发送元素的css文件.
我怎样才能做到这一点?仅使用元素及其所有子元素的相关样式属性和选择器生成JSON /对象.尊重层次结构,没有重复.有类似的问题,但它们已经过时,往往不考虑儿童元素.
我想也许有一种方法可以从这个元素创建一个新的DOM,然后获得根css?
这是我想出的东西,基本上传递您想要提取其子项及其子项样式的元素,它会将样式表作为字符串返回给您。在运行代码片段之前打开您的控制台,您将看到console.log
.
因为我想支持提取每个元素,即使是那些没有选择器的元素,我不得不将每个元素 id 替换为专门为它们生成的唯一 uuid,以方便输出的样式。这种方法的问题在于,如果您使用 id 进行样式设置或用户交互,您将在调用extractCSS
.
但是,oldId
一旦您的pdfKit
过程完成生成,使用I'm 传递来更改回来是非常简单的。只需调用swapBackIds
传递elements
函数返回的值即可。如果在我的代码段中取消对调用的注释,您可以看到行为的差异:#root
粉红色背景将消失,因为样式针对的是 element id
。
总而言之,您需要:
extractCSS
使用您要提取的元素调用res.stylesheet
swapBackIds
与res.elements
// Generate an unique id for your element
// From https://stackoverflow.com/a/2117523/2054072
function uuidv4 () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
// Flatten an array
// https://stackoverflow.com/a/15030117/2054072
function flatten(arr) {
return arr.reduce(function (flat, toFlatten) {
return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
}, []);
}
function recursiveExtract (element) {
var id = uuidv4()
var oldId = element.id
var computed = window.getComputedStyle(element)
var style = computed.cssText
// Now that we get the style, we can swap the id
element.setAttribute('id', id)
// The children are not a real array but a NodeList, we need to convert them
// so we can map over them easily
var children = Array.prototype.slice.call(element.children)
return [{ id: id, style: style, oldId: oldId }].concat(children.map(recursiveExtract))
}
function extractCSS (element) {
if (!element) { return { elements: [], stylesheet: '' } }
var raw = recursiveExtract(element)
var flat = flatten(raw)
return {
elements: flat,
stylesheet: flat.reduce(function (acc, cur) {
var style = '#' + cur.id + ' {\n' + cur.style + '\n}\n\n'
return acc + style
}, '')
}
}
var pdfElement = document.querySelector('#root')
var res = extractCSS(pdfElement)
console.log(res.stylesheet)
function swapBackIds (elements) {
elements.forEach(function (e) {
var element = document.getElementById(e.id)
element.setAttribute('id', e.oldId)
})
}
swapBackIds(res.elements)
Run Code Online (Sandbox Code Playgroud)
#root {
background-color: pink;
}
.style-from-class {
background-color: red;
width: 200px;
height: 200px;
}
.style-from-id {
background-color: green;
width: 100px;
height: 100px;
}
Run Code Online (Sandbox Code Playgroud)
<div id="root">
<span>normal</span>
<span style="background: blue">inline</span>
<div class="style-from-class">
style-class
</div>
<div class="style-from-id">
style-id
<div style="font-size: 10px">a very nested</div>
<div style="font-size: 12px; color: white">and another</div>
</div>
</div>
<div id="ignored-sibling">
</div>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1606 次 |
最近记录: |