如何正确使用jsPDF库

Dan*_*uva 75 html javascript jquery jspdf

我想将我的一些div转换为PDF,我尝试过jsPDF库但没有成功.似乎我无法理解我需要导入什么才能使库工作.我经历过这些例子,但我仍然无法理解.我尝试过以下方法:

<script type="text/javascript" src="js/jspdf.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

在jQuery之后:

$("#html2pdf").on('click', function(){
    var doc = new jsPDF();
    doc.fromHTML($('body').get(0), 15, 15, {
        'width': 170
    });
    console.log(doc);
});
Run Code Online (Sandbox Code Playgroud)

出于测试目的但我收到:

"Cannot read property '#smdadminbar' of undefined"
Run Code Online (Sandbox Code Playgroud)

#smdadminbar身体的第一个div 在哪里.

Wel*_*her 120

您可以使用html中的pdf,如下所示,

步骤1:将以下脚本添加到标头中

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

或在本地下载

第2步:添加HTML脚本以执行jsPDF代码

自定义此项以传递标识符或仅将#content更改为您需要的标识符.

 <script>
    function demoFromHTML() {
        var pdf = new jsPDF('p', 'pt', 'letter');
        // source can be HTML-formatted string, or a reference
        // to an actual DOM element from which the text will be scraped.
        source = $('#content')[0];

        // we support special element handlers. Register them with jQuery-style 
        // ID selector for either ID or node name. ("#iAmID", "div", "span" etc.)
        // There is no support for any other type of selectors 
        // (class, of compound) at this time.
        specialElementHandlers = {
            // element with id of "bypass" - jQuery style selector
            '#bypassme': function (element, renderer) {
                // true = "handled elsewhere, bypass text extraction"
                return true
            }
        };
        margins = {
            top: 80,
            bottom: 60,
            left: 40,
            width: 522
        };
        // all coords and widths are in jsPDF instance's declared units
        // 'inches' in this case
        pdf.fromHTML(
            source, // HTML string or DOM elem ref.
            margins.left, // x coord
            margins.top, { // y coord
                'width': margins.width, // max width of content on PDF
                'elementHandlers': specialElementHandlers
            },

            function (dispose) {
                // dispose: object with X, Y of the last line add to the PDF 
                //          this allow the insertion of new lines after html
                pdf.save('Test.pdf');
            }, margins
        );
    }
</script>
Run Code Online (Sandbox Code Playgroud)

第3步:添加您的身体内容

<a href="javascript:demoFromHTML()" class="button">Run Code</a>
<div id="content">
    <h1>  
        We support special element handlers. Register them with jQuery-style.
    </h1>
</div>
Run Code Online (Sandbox Code Playgroud)

请参阅原始教程

看到一个工作小提琴

  • 感谢您发布此内容,但我尝试了上面的确切代码(本地),并生成了一个空白的pdf文档.它与jquery 1.11.0完全不兼容所以我使用了与https://github.com/MrRio/jsPDF/blob/master/examples/js/jquery/jquery-1.7.1上使用的相同的jquery版本. min.js (4认同)
  • 我如何使它打印我的 CSS 样式? (3认同)
  • 供参考。此答案中提供的小提琴使用“.fromHTML”,该格式已于 2018 年 11 月弃用。 (2认同)

use*_*332 16

您只需要此链接jspdf.min.js

它拥有一切.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,这让我疯了!使用jspdf的调试版本,任何配置中的其他版本都没有.也许缩小是以某种方式破坏它?或者我做错了. (2认同)

Vit*_*vzh 9

根据最新版本(1.5.3),没有fromHTML()方法了。相反,您应该使用 jsPDF HTML 插件,请参阅:https ://rawgit.com/MrRio/jsPDF/master/docs/module-html.html#~html

您还需要添加 html2canvas 库以使其正常工作:https : //github.com/niklasvh/html2canvas

JS(来自 API 文档):

var doc = new jsPDF();   

doc.html(document.body, {
   callback: function (doc) {
     doc.save();
   }
});
Run Code Online (Sandbox Code Playgroud)

您也可以提供 HTML 字符串而不是对 DOM 元素的引用。


rob*_*pim 7

这最终是为我做了什么(并触发了处理):

function onClick() {
  var pdf = new jsPDF('p', 'pt', 'letter');
  pdf.canvas.height = 72 * 11;
  pdf.canvas.width = 72 * 8.5;

  pdf.fromHTML(document.body);

  pdf.save('test.pdf');
};

var element = document.getElementById("clickbind");
element.addEventListener("click", onClick);
Run Code Online (Sandbox Code Playgroud)
<h1>Dsdas</h1>

<a id="clickbind" href="#">Click</a>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.3/jspdf.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

而对于KnockoutJS倾向的那些,有点约束力:

ko.bindingHandlers.generatePDF = {
    init: function(element) {

        function onClick() {
            var pdf = new jsPDF('p', 'pt', 'letter');
            pdf.canvas.height = 72 * 11;
            pdf.canvas.width = 72 * 8.5;

            pdf.fromHTML(document.body);

            pdf.save('test.pdf');                    
        };

        element.addEventListener("click", onClick);
    }
};
Run Code Online (Sandbox Code Playgroud)