Cyo*_*oce 5 html javascript css pdf jquery
我做了一个语法荧光笔,我想要一个保存为 PDF 的选项。我看过这个 SO question,但下载它不会保留 CSS 样式,这破坏了下载突出显示文件的意义。有没有办法pre在保持 CSS 的同时将我的元素保存为 PDF?
HTML:
<pre id='output'> (highlighted portion) </pre>
<button id='save'>Save as PDF</button>
Run Code Online (Sandbox Code Playgroud)
JS:
$('#save').click(function(){
//this is what I need help with
});
Run Code Online (Sandbox Code Playgroud)
您可能已经注意到,如果这很重要,我正在使用 jQuery。
尝试打开 new window,将pre html, style,(如果有)pre元素添加到 new ,在 new 上window document.body调用.focus(), ;选择系统打印对话框,选择“打印到文件”.print()window
$("#save").click(function() {
var text = $("#output")[0].outerHTML;
// `styles`: `style` element;
// or `String` "<style>.light{color:#0af;}</style>" ;
// alternatively , add `style` attribute to `pre` element directly,
// e.g., `<pre style="color:#0af;">`
var styles = $("style")[0].outerHTML;
var popup = window.open("", "popup");
// append `text`:`pre` element `styles`:`style` element
// at `popup` `document.body`
popup.document.body.innerHTML = text + styles;
popup.focus();
popup.print();
});
Run Code Online (Sandbox Code Playgroud)
jsfiddle http://jsfiddle.net/tn04Ldka/2/