简体   繁体   English

使用jspdf库将html文件转换为pdf

[英]converting html file into pdf using jspdf library

I am trying to convert the html file to pdf using jsPDF library. 我正在尝试使用jsPDF库将html文件转换为pdf。 But in the code I implemented, the whole image is not converting into pdf. 但是在我实现的代码中,整个图像并未转换为pdf。 Only half of it is visible. 它只有一半可见。 Could someone please tell me where am I wrong? 有人可以告诉我我哪里错了吗?

 var canvas = document.getElementById('myCanvas'); var context = canvas.getContext('2d'); context.beginPath(); context.moveTo(0,0); context.lineTo(200,100); context.closePath(); context.strokeStyle = '#0000ff'; context.stroke(); download.addEventListener("click", function() { // only jpeg is supported by jsPDF var imgData = canvas.toDataURL("image/jpeg",1.0); var pdf = new jsPDF(); pdf.addImage(imgData, 'JPEG', 0, 0); var download = document.getElementById('download'); pdf.save("download.pdf"); }); 
 <script src="https://parall.ax/parallax/js/jspdf.js"></script> <canvas id="myCanvas" width="200" height="100" style="border:solid #0000ff;"> </canvas> <button id="download">download</button> 

I recently did some research on converting html to pdfs. 我最近对将html转换为pdf进行了一些研究。 I looked at jsPDF, but it seemed better suited for embedding PDF documents into html pages. 我看了jsPDF,但它似乎更适合于将PDF文档嵌入到html页面中。

If you're looking to export a html page to PDF you may want to consider https://github.com/fraserxu/electron-pdf , which is what I ultimately decided to use. 如果要将HTML页面导出为PDF,则可能需要考虑https://github.com/fraserxu/electron-pdf ,这是我最终决定使用的。 Electron generates output consistent with the 'print to pdf' feature in Chrome. Electron生成的输出与Chrome中的“打印到pdf”功能一致。

Note: this is a server-side rendering solution. 注意:这是服务器端渲染解决方案。 If you have to do it client-side then it is not the right solution. 如果必须在客户端执行此操作,那么这不是正确的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM