首页 > 解决方案 > html2canvas:错误未捕获(承诺):[object undefined] angular 6

问题描述

我正在使用 html2canvas 将 html 转换为图像,然后导出为 pdf 文件。这是我使用这个包的步骤:

  1. 纱线添加 html2canvas

  2. 添加代码以将 html 转换为图像。

    async export(comp, doc, save) {
      // create pdf file
      let height = REPORT_PAGE.top;
      const header = comp.reportHeader.nativeElement;
      const headerCanvas = await html2canvas(header, {svgRendering: true});
      height = REPORT_PAGE.top;
      let imgHeightHtml = REPORT_PAGE.header * REPORT_PAGE.width / headerCanvas.width;
      let contentDataURL = headerCanvas.toDataURL('image/png');
      doc.addImage(contentDataURL, 'PNG', REPORT_PAGE.left, height, REPORT_PAGE.width, imgHeightHtml);
      if (save) {
        doc.save('myreport.pdf');
      } else {
        doc.addPage();
      }
    }
    

其中 comp 是角度分量,doc 是 jsPDF,save 是布尔值。调用 html2canvas 时会抛出以下错误。

Uncaught (in promise): [object Undefined]
at resolvePromise (zone.js:814)
at resolvePromise (zone.js:771)
at zone.js:873
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:4053)
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)

html2canvas 是一个非常有用的包,但是这个错误真的很糟糕。请帮忙。

标签: htmlcanvasjspdfhtml2canvasxhtml2pdf

解决方案


我有同样的问题,这解决了它:

html2canvas(element, {
    ignoreElements: (node) => {
        return node.nodeName === 'IFRAME';
    }
}).then((canvas) => {
    ...
});

来源:https ://github.com/niklasvh/html2canvas/issues/1593#issuecomment-489059452


推荐阅读