jsPDF html method always fails

前端 未结 1 1062
迷失自我
迷失自我 2020-12-11 13:43

I am trying to generate a PDF from an html element (a div) with some content:

相关标签:
1条回答
  • 2020-12-11 13:47

    The problem is most likely the version of html2canvas you used. html2canvas 0.4.1 is not going to work. It should be html2canvas 1.0.0-alpha.11 or higher. However, current version, html2canvas 1.0.0-rc.3 doesn't work either, it also gives me a blank page. At least html2canvas 1.0.0-alpha.12 still works for me.

    <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="~/lib/html2canvas/html2canvas.min.js"></script>
    <!-- html2canvas 1.0.0-alpha.11 or html2canvas 1.0.0-alpha.12 is needed -->
    <script>
        function download() {
            let pdf = new jsPDF('p', 'pt', 'a4');
    
            pdf.html(document.getElementById('toPDF'), {
                callback: function () {
                    window.open(pdf.output('bloburl'));
                }
            });
        }
    </script>
    

    Update: the latest version that works for me is html2canvas v1.0.0-rc.1

    0 讨论(0)
提交回复
热议问题