jsPDF alternative for printing parts of a website

时间:2019-01-09 21:42:34

标签: javascript pdf jspdf

I tried converting a single part of a website into a pdf using jsPDF. The part contains images, background-images and text all nested into divs and lists. I figured it is a lot of work to make jsPDF convert a section dynamically.

Is there any good alternative to use where i can just throw a div in and print it as chrome does it with a whole site ?

1 个答案:

答案 0 :(得分:0)

如果使用jsPDF html PlugIn,则无需进行很多工作。亲自查看sample。我使用了html2canvas.1.0.0-alpha.12.jsjspdf.debug.js。这是工作代码:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js" integrity="sha384-NaWTHo/8YCBYJ59830LTz/P4aQZK1sS0SneOgAvhsIl3zBu8r9RevNg5lHCHAuQ/"
crossorigin="anonymous"></script>
<script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
<script>
    function download() {
        let pdf = new jsPDF('p', 'pt', 'a3');
        pdf.html(document.getElementById('idName'), {
            callback: function (pdf) {
                pdf.save('test.pdf');
            }
        });
    }
</script>