是否可以使用 jQuery 生成 PDF?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4490028/
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
Is it possible to generate PDF using jQuery?
提问by Mohan Ram
I am using Ajax functionality to retrieve my content and I need to export PDF on success of jQuery.ajax()
. How can I do that?
我正在使用 Ajax 功能来检索我的内容,并且我需要在jQuery.ajax()
. 我怎样才能做到这一点?
采纳答案by Nick Craver
jQuery cannot (because JavaScript cannot) create a PDF from data, no...it can getone from your server (like any other request), but it cannot generate one. JavaScript simply doesn't have a mechanism (though there are some HTML5 options being implemented now) to create/save a file that works cross-browser, especially a binary file.
jQuery 不能(因为 JavaScript 不能)从数据创建 PDF,不……它可以从您的服务器获取一个(像任何其他请求一样),但它不能生成一个。JavaScript 根本没有机制(尽管现在实现了一些 HTML5 选项)来创建/保存跨浏览器工作的文件,尤其是二进制文件。
回答by Dave Ward
If at all possible, the server-side is a better choice for generating PDFs. It's probably going to be faster for most users and returning a file via standard HTTP request is much more robust than the current client-side options.
如果可能的话,服务器端是生成 PDF 的更好选择。对于大多数用户来说,它可能会更快,并且通过标准 HTTP 请求返回文件比当前的客户端选项要健壮得多。
That said, this library will generate a PDF on the client-side: http://snapshotmedia.co.uk/blog/jspdf
也就是说,这个库将在客户端生成一个 PDF:http: //snapshotmedia.co.uk/blog/jspdf
In browsers that support data URIs, it can return the PDF directly. In other browsers, you can couple it with a Flash component called Downloadify to accomplish the same.
在支持数据 URI 的浏览器中,它可以直接返回 PDF。在其他浏览器中,您可以将其与名为 Downloadify 的 Flash 组件结合使用来完成相同的操作。
回答by James Tomasino
If you are retrieving PDF data from your server in your AJAX success and need to output it to your user as a download, it can be accomplished with a tiny bit of base64 encoding. If I understand correctly, you most likely have a scenario where your server is possibly returning a PDF or some other data type on success (like XML). In this case, you have two steps to handle the request:
如果您在 AJAX 成功中从服务器检索 PDF 数据并需要将其作为下载输出给您的用户,则可以通过一点点 base64 编码来完成。如果我理解正确,您很可能会遇到这样的情况:您的服务器可能会在成功时返回 PDF 或其他一些数据类型(如 XML)。在这种情况下,您有两个步骤来处理请求:
1) Determine the content type via its header. Here is an example of splitting your handler based on the response:
1)通过其标题确定内容类型。以下是根据响应拆分处理程序的示例:
$.ajax({
type: "POST", url: "/test", data: someData, success: function(response, status, xhr){
var ct = xhr.getResponseHeader("content-type") || "";
if (ct.indexOf(‘xml') > -1) {
// handle xml here
}
if (ct.indexOf(‘pdf') > -1) {
// handle pdf here
}
}
});
2) Once you have your PDF content, you can redirect the browser to show the pdf by using a base64 data trick. First, encode the data content in base64. There are a number of librariesto help you do this in Javascript. Then, return your content via document.location.href:
2) 获得 PDF 内容后,您可以使用 base64 数据技巧重定向浏览器以显示 pdf。首先,在base64中对数据内容进行编码。有许多库可以帮助您在 Javascript 中执行此操作。然后,通过 document.location.href 返回您的内容:
document.location.href = 'data:application/pdf;base64,' + base64PDFData;
That should get what you need. You could theoretically forward any content-type to the browser in this method.
那应该得到你所需要的。理论上,您可以使用此方法将任何内容类型转发到浏览器。
EDIT:
编辑:
I should mention that the data uri will unfortunately not work in IE due to security restrictions.
我应该提到的是,由于安全限制,数据 uri 将无法在 IE 中工作。
回答by akaco
回答by Himanshu Singh
Add this to your Script:
将此添加到您的脚本中:
<script src="https://docraptor.com/docraptor-1.0.0.js"></script>
DocRaptor.createAndDownloadDoc("YOUR_API_KEY_HERE", {
test: false, // test documents are free, but watermarked
type: "pdf",
name: planStatus+"_" +assessmentYear+"_"+employeeId+ ".pdf",
document_content: document.querySelector('#MyDoc').innerHTML, // use this page's HTML
// document_content: "<h1>Hello world!</h1>", // or supply HTML directly
// document_url: "http://example.com/your-page", // or use a URL
// javascript: true, // enable JavaScript processing
// prince_options: {
// media: "screen", // use screen styles instead of print styles
// }
})
回答by tig
If the HTML you are dealing with is a table, you can use the jquery.dataTables plug in with TableTools to generate PDF. It uses Flash behind the scenes and is severely limited on formatting, but it does generate PDF.
如果您处理的 HTML 是表格,您可以使用带有 TableTools 的 jquery.dataTables 插件生成 PDF。它在幕后使用 Flash,并且在格式方面受到严重限制,但它确实生成了 PDF。