如何使用 JavaScript 或 jQuery 在客户端将 html 页面导出为 pdf?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18459029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to export a html page to pdf in client side using JavaScript or jQuery?
提问by sid
And if it is not possible then what are the other alternatives?
如果这是不可能的,那么其他选择是什么?
I tried to convert a complete html page to pdf with dynamic values but I can't .
我试图将一个完整的 html 页面转换为具有动态值的 pdf,但我不能。
But I saw some API like jspdfbut it is not useful for me.
但是我看到了一些像jspdf这样的API,但它对我没有用。
Is it possible to save a HTML page as PDF file using JavaScript or jQuery?
是否可以使用 JavaScript 或 jQuery 将 HTML 页面保存为 PDF 文件?
In Detail:
详细:
I generated one HTML Page which contains a list grid which populated all the available reports dynamically. It has one button 'save as PDF'. If the user clicks this button then the HTML page will be converted to a PDF file.
我生成了一个 HTML 页面,其中包含一个动态填充所有可用报告的列表网格。它有一个按钮“另存为 PDF”。如果用户单击此按钮,则 HTML 页面将转换为 PDF 文件。
Is it possible using JavaScript or jquery?
是否可以使用 JavaScript 或 jquery?
回答by Daniel Ruf
jsPDF has a HTML renderer but it is in the early stages:
jsPDF 有一个 HTML 渲染器,但它处于早期阶段:
https://github.com/mrrio/jsPDF
https://github.com/mrrio/jsPDF
http://parall.ax/products/jspdf
http://parall.ax/products/jspdf
But the best solution is the one from mujaffars. User AJAX and a stable PDF library written in PHP like FPDF, PDFLib, TCPDF and others
但最好的解决方案是来自 mujaffars 的解决方案。用户 AJAX 和用 PHP 编写的稳定 PDF 库,如 FPDF、PDFLib、TCPDF 等
回答by Shubh
Well, if I have got your question properly then u need to Export a Table Data as PDF
?
好吧,如果我正确地回答了您的问题,那么您需要回答Export a Table Data as PDF
吗?
If Yes, then please see datatables.jswith simple example here.
如果是,请在此处查看带有简单示例的datatables.js。
Also, look into use BytescoutPDF.js (Bytescout PDF Generator for JavaScript) to draw PDF invoice and JSPDFyou can get the details here.
此外,研究使用 BytescoutPDF.js (Bytescout PDF Generator for JavaScript) 来绘制 PDF 发票和JSPDF,您可以在此处获取详细信息。
回答by mujaffars
You need to use any pdf generating library. You have not specified in which technology you are working (Php, .net or anything else)
您需要使用任何 pdf 生成库。您尚未指定您使用的技术(Php、.net 或其他任何技术)
In PHP you can use tcpdf library, Where you can pass html to generate pdf
在 PHP 中,您可以使用 tcpdf 库,您可以在其中传递 html 以生成 pdf
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->AddPage();
$pdf->writeHTML($pdfHtml, true, false, true, false, '');
Where $pdfHtml will be your page html
$pdfHtml 将是您的页面 html
In your case when clicking on 'save as pdf' button you can get html by using html() method of jquery
在您单击“另存为 pdf”按钮时,您可以使用 jquery 的 html() 方法获取 html
eg.
例如。
var pageHtml = $('html').html();
by passing pageHtml to ajax page, You can create pdf in ajax page
通过将 pageHtml 传递给 ajax 页面,您可以在 ajax 页面中创建 pdf
回答by Vaibs_Cool
It is possible indeed with pdf.js
plugin
pdf.js
is an HTML5 experiment that explores building a faithful and efficient PDF renderer without native code assistance.
pdf.js
插件
确实有可能pdf.js
是一个 HTML5 实验,它探索在没有本机代码帮助的情况下构建一个忠实而高效的 PDF 渲染器。