简体   繁体   English

使用 jsPDF 和 html2canvas 导出 pdf

[英]Export pdf with jsPDF and html2canvas

I can print a PDF with jsPDF but cannot use a div as content.我可以使用 jsPDF 打印 PDF 但不能使用 div 作为内容。 For this I have now imported html2canvas, unfortunately I can't really figure out from the documentation how to use this.为此,我现在已经导入了 html2canvas,不幸的是,我无法从文档中真正弄清楚如何使用它。 I have found a few instructions on the internet, but they did not work properly for me.我在互联网上找到了一些说明,但它们对我不起作用。 The whole thing is in WordPress.整件事都在 WordPress 中。 I get various error messages, either that undefiend is not a function, or that it can't find something, or whatever.我收到各种错误消息,要么 undefiend 不是 function,要么它找不到东西,或者其他什么。

first approach:第一种方法:

function testPDF() {
    console.log("testPDF-function works")
    html2canvas(document.getElementById('a4')).then(function(canvas){
        document.body.appendChild(canvas)
    })
    var img = canvas.toDataURL('img/png')

    var doc = new jsPDF();
    doc.addImage(img, 'JPEG', 20,20)
    doc.save('test.pdf');
}

second approach: Github (here someone has merged the two libraries, but that doesn't work for me either.)第二种方法: Github (这里有人合并了两个库,但这对我也不起作用。)

let page = document.getElementById("a4");
function testPDF() {
    html2PDF(page, {
        jsPDF: {
            format: "a4",
        },
        imageType: "image/jpeg",
        output: "offerte.pdf",
    });
}

third approach第三种方法

function testPDF() {
    html2canvas(document.getElementById("a4")).then(function cancas(canvas) {
        // document.body.appendChild(canvas);
        var doc = new jsPDF();
        doc.addImage(document.body.appendChild(canvas));
        doc.save("offerte.pdf");
    });
}

My html looks like this:我的 html 看起来像这样:

<div id='a4' style='height: 297mm; width: 210mm; border: 1px black solid; margin-left:20%;'>
    <p>Content</p>
</div>
<button id='pdfExport' onclick='testPDF' >Export PDF</button>"

html2canvas html2canvas

jsPDF jsPDF

Try this below code.试试下面的代码。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.4.1/jspdf.debug.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>

<style type="text/css">
    #a4{width: 100%;background: #ffffff;color: #000000;}
</style>

<div id="a4" style="height: 297mm; width: 210mm; border: 1px black solid; margin-left:20%;">
   Content
</div>
<button id="pdfExport" onclick="javascript:demoFromHTML();">Export PDF</button>

<script>
    function demoFromHTML() {
        html2canvas(document.getElementById('a4'), {
            onrendered: function (canvasObj) {
                var pdf = new jsPDF('P', 'pt', 'a4'),
                    pdfConf = {
                        pagesplit: false,
                        backgroundColor: '#FFF'
                    };
                document.body.appendChild(canvasObj); //appendChild is required for html to add page in pdf
                pdf.addHTML(canvasObj, 0, 0, pdfConf, function () {
                    document.body.removeChild(canvasObj);
                    //pdf.addPage();
                    pdf.save('Test.pdf');
                });
            }
        });
    }
</script>

The above code fiddle.上面的代码小提琴。 jsfiddle jsfiddle

Also, refer to the below URLs.此外,请参阅以下 URL。

https://github.com/devrajatverma/jsPdfExample https://github.com/devrajatverma/jsPdfExample

How to properly use jsPDF library 如何正确使用jsPDF库

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

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