javascript 使用 JSPDF 在客户端生成 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11967802/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:54:54  来源:igfitidea点击:

Generate PDF on client side using JSPDF

javajavascriptgwtpdf-generationjspdf

提问by RAS

In my application I need to download pdf by parsing HTML on client side itself. For generating pdf on client side I am using jsPdf. Following is my code.

在我的应用程序中,我需要通过在客户端本身解析 HTML 来下载 pdf。为了在客户端生成 pdf,我使用 jsPdf。以下是我的代码。

PdfGenerator.java

pdf生成器.java

public static native String createPDF() /*-{
        $wnd.createPDF(); 
}-*/;

entrypoint.html

入口点.html

 function createPDF(){
         try {
          var doc = new jsPDF();
           doc.text(20, 20, 'This PDF has a title, subject, author, keywords and a crea');
           doc.output('datauri');
           var out = doc.output();
           var url = 'data:application/pdf;base64,' + Base64.encode(out);
           document.location.href = url;
        } catch (e) {
            return e.message;
        }
        return "";
};

I have added all the js in my project and defined script as well. But whenever I call this method then it is giving output "sprintf is not defined.". Please let me know if I am missing out something.

我已经在我的项目中添加了所有 js 并定义了脚本。但是每当我调用此方法时,它都会给出输出“sprintf 未定义”。. 如果我遗漏了什么,请告诉我。

采纳答案by PVR

I guess you forget to add the scripts for sprintf.jsand base64 js. As jsPdf.jsinternally uses both of these js.

我猜您忘记为sprintf.js和添加脚本base64 js。由于在jsPdf.js内部使用这两个 js。

entrypoint.html

入口点.html

<script type="text/javascript" src="base64.js"></script>
<script type="text/javascript" src="sprintf.js"></script>
<script type="text/javascript" src="jspdf.js"></script>

please refer this link http://forums.webhosting.uk.com/web-designing-development/6718-jspdf-generating-your-pdf-web-page-documents-using-javascript.html

请参考此链接http://forums.webhosting.uk.com/web-designing-development/6718-jspdf-generating-your-pdf-web-page-documents-using-javascript.html

回答by trudesign

In the latest builds of jsPDF, you don't need base64 or sprintf, just jspdf.min.js found in the 'dist' folder, includes all the plugins (except downloadify/swfobject).

在最新版本的 jsPDF 中,您不需要 base64 或 sprintf,只需要在“dist”文件夹中找到的 jspdf.min.js,包括所有插件(除了 downloadify/swfobject)。

Just updating an old ticket if someone runs across it when trying to figure out jsPDF due to it's not so good documentation.

如果有人在试图找出 jsPDF 时遇到它,则只需更新一张旧票,因为它不是很好的文档。

回答by Moshensky Daniil

You don't need to complicate your code by using window.location. JsPDF has method .save()to handle it.

您无需使用window.location. JsPDF 有.save()处理它的方法。

function createPDF(){
         try {
           var doc = new jsPDF();
           doc.text(20, 20, 'This PDF has a title, subject, author, keywords and a crea');
           doc.save('file_name.pdf');
        } catch (e) {
            return e.message;
        }
        return "";
};

回答by jcaruso

Depending on what browser you are supporting (all browser versions, and IE 10+), you don't even need to include Base64.encode(). Just call btoa()instead.

根据您支持的浏览器(所有浏览器版本和 IE 10+),您甚至不需要包含Base64.encode(). 只是打电话btoa()