admin管理员组文章数量:1417412
I want to generate PDF from HTML. The libraries I tried are print.js
, jsPDF
With print.js
I have HTML and trying to make it to PDF and it succeed, but the PDF is only the HTML, without the CSS. But that does not suit me. Sample example
<table id="printJS-form">
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro ercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>
<button type="button" onclick="printJS('printJS-form', 'html')">
Print Form
</button>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
The closest thing to success was with jsPDF
, where I create image this time with all css and then generate from that image PDF. But the problem here is that generated PDF is just image and I lose all meta data from the PDF. Sample example
<body>
<div id="printJS-form">
<h3 id='head-color'>Test heading</h3>
<button id='print-btn'>Start print process</button>
</div>
</body>
$('#print-btn').click(()=>{
var pdf = new jsPDF();
pdf.addHTML(document.body,function() {
pdf.save('web.pdf');
});
})
#head-color{
color: blue;
}
Another thing I tried and really hope to make it here, because it seems cleaner option for me, is with "onbeforeprint" event. Here right before window.print()
to be called, "onbeforeprint" event is activated. And if in "onbeforeprint", PDF is already generated I want somehow to access it. But I can`t figure out how and whether it is at all possible. Sample example
<html>
<body onbeforeprint="myFunction()">
<h1>Try to print this document</h1>
<p><b>Tip:</b> Keyboard shortcuts, such as Ctrl+P sets the page to print.</p>
<p><b>Note:</b> The onbeforeprint event is not supported in Safari and Opera.</p>
<button type="button" onclick="printWindow()">
Print Form
</button>
<script>
function myFunction() {
// get blob pdf
alert("You are about to print this document!");
}
function printWindow() {
window.print();
}
</script>
</body>
</html>
And finally I'm asking
Is there a way, with print.js or jsPDF, to get not just the html, but the css too?
Can I get binary PDF from "onbeforeprint" event or any other javascript event?
I want to generate PDF from HTML. The libraries I tried are print.js
, jsPDF
With print.js
I have HTML and trying to make it to PDF and it succeed, but the PDF is only the HTML, without the CSS. But that does not suit me. Sample example
<table id="printJS-form">
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro ercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>
<button type="button" onclick="printJS('printJS-form', 'html')">
Print Form
</button>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
The closest thing to success was with jsPDF
, where I create image this time with all css and then generate from that image PDF. But the problem here is that generated PDF is just image and I lose all meta data from the PDF. Sample example
<body>
<div id="printJS-form">
<h3 id='head-color'>Test heading</h3>
<button id='print-btn'>Start print process</button>
</div>
</body>
$('#print-btn').click(()=>{
var pdf = new jsPDF();
pdf.addHTML(document.body,function() {
pdf.save('web.pdf');
});
})
#head-color{
color: blue;
}
Another thing I tried and really hope to make it here, because it seems cleaner option for me, is with "onbeforeprint" event. Here right before window.print()
to be called, "onbeforeprint" event is activated. And if in "onbeforeprint", PDF is already generated I want somehow to access it. But I can`t figure out how and whether it is at all possible. Sample example
<html>
<body onbeforeprint="myFunction()">
<h1>Try to print this document</h1>
<p><b>Tip:</b> Keyboard shortcuts, such as Ctrl+P sets the page to print.</p>
<p><b>Note:</b> The onbeforeprint event is not supported in Safari and Opera.</p>
<button type="button" onclick="printWindow()">
Print Form
</button>
<script>
function myFunction() {
// get blob pdf
alert("You are about to print this document!");
}
function printWindow() {
window.print();
}
</script>
</body>
</html>
And finally I'm asking
Is there a way, with print.js or jsPDF, to get not just the html, but the css too?
Can I get binary PDF from "onbeforeprint" event or any other javascript event?
Share Improve this question edited Mar 15, 2022 at 13:57 stanimirsp asked Jul 4, 2019 at 6:09 stanimirspstanimirsp 3,1462 gold badges29 silver badges42 bronze badges1 Answer
Reset to default 4I think you could use Puppeteer to generate a pdf. I've not tried it but here are some links that demonstrate this approach:
- advanced-pdf-generation-for-node-js-using-puppeteer
- pdf-from-html-node-js-puppeteer
- high-quality-pdf-generation-using-puppeteer
本文标签: How to generate PDF from HTML with JavaScriptStack Overflow
版权声明:本文标题:How to generate PDF from HTML with JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745261989a2650396.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论