pdf.save不是Angular 6中使用的jspdf中的函数错误

时间:2018-12-26 18:04:53

标签: angular jspdf

我正在角度6中使用jsPDF的最新版本

包装:-https://www.npmjs.com/package/jspdf

源代码

import * as jsPDF from 'jspdf'

 public captureScreen() 
    {
        var data = document.getElementById('print');
        html2canvas(data).then(canvas => 
            {
            // Few necessary setting options
            var imgWidth = 208;
            var pageHeight = 295;
            var imgHeight = canvas.height * imgWidth / canvas.width;
            var heightLeft = imgHeight;

            const contentDataURL = canvas.toDataURL('image/png')
            let pdf = new jsPDF('p', 'mm', 'a4'); // A4 size page of PDF
            var position = 0;
            pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight)
            pdf.save('MYPdf.pdf'); // Generated PDF 

        });
    }

错误

ERROR Error: Uncaught (in promise): TypeError: pdf.save is not a function
TypeError: pdf.save is not a function
at app.generateRecepit.Component.ts:59
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:388)
at Object.onInvoke (core.js:14060)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:387)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.run (zone.js:138)
at zone.js:872
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:14051)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
at resolvePromise (zone.js:814)
at zone.js:877
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:14051)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
at drainMicroTaskQueue (zone.js:595)
at push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask (zone.js:500)
at ZoneTask.invoke (zone.js:485)
at timer (zone.js:2054)

4 个答案:

答案 0 :(得分:0)

步骤1:-从下面的链接下载dist新文件夹

Link to download dist folder

步骤2:-检查node_modules中的jspdf文件夹

enter image description here

步骤3:-提取下载的dist.zip文件,并替换为node_modules jspdf文件夹中的文件。

最后一步以这种方式导入jsPDF。

import * as jsPDF from 'jspdf'

链接以获取更多详细信息:-view details

答案 1 :(得分:0)

使用1.4.2版本,该版本稳定且没有该问题。

答案 2 :(得分:0)

版本1.5.3已发布并解决了该问题。

答案 3 :(得分:0)

将jspdf当前版本更新为1.5.3版,新的更新解决了这些问题