简体   繁体   English

HTML2Canvas:无法在克隆的 iframe 中找到元素

[英]HTML2Canvas: Unable to find element in cloned iframe

In my program, I am trying to do a PDF export of some pages, but I encountered the following error when HTML2Canvas tries to process the elements that I have:在我的程序中,我试图对某些页面进行 PDF 导出,但是当 HTML2Canvas 尝试处理我拥有的元素时遇到以下错误:

Unable to find element in cloned iframe无法在克隆的 iframe 中找到元素

And only this.只有这个。 This is the code I use:这是我使用的代码:

public exportToPdf(
    selectedFilter: SelectedFilter,
    currentTime: string,
    idOfExportDiv: string[]
  ): void {
    this.addClassToMainDiv();

    const elementsToScreenshot: HTMLElement[] = [];

    for (let index = 0; index < idOfExportDiv.length; index++) {
      elementsToScreenshot.push(document.getElementById(idOfExportDiv[index]));
    }

    const fileName = 'export-' + currentTime + '.pdf';
    this.download(elementsToScreenshot, fileName, selectedFilter, currentTime);
  }

And the download function:以及下载function:

  download(elementById: HTMLElement[], name, selectedFilter: SelectedFilter, currentTime: string) {
    const html2canvasPromises: Promise<unknown>[] = [];

    for (let index = 0; index < elementById.length; index++) {
      html2canvasPromises.push(
        html2canvas(elementById[index], {
          useCORS: true
        }).then(canvas => {
          const image = canvas.toDataURL();
          const imagePromise = this.addImage(image);
          return imagePromise;
        })
      );
    }

    Promise.all(html2canvasPromises)
      .then(content => {
        this.removeClassFromMainDiv();
        this.downloadPdf(selectedFilter, currentTime, name, content);
        this.pdfCreated.next(true);
        this.loadingSpinnerService.close();
      })
      .catch(error => {
        console.error('Image conversion error', 'Failed to screenshot and download provided elements', {
          error: error
        });
        this.removeClassFromMainDiv();
        this.pdfCreated.next(true);
        this.loadingSpinnerService.close();
      });
  }

The "elementsToScreenshot" is an array of HTMLElements that is always filled with the correct found elements inside the HTML rendering, which look like this: “elementsToScreenshot”是一个 HTMLElements 数组,它总是用 HTML 渲染中找到的正确元素填充,如下所示:

0: div#main-screen-export-div.m-l-20.m-top-15.wrapper-comp
1: div#export-main-0.d-flex.w-100.ng-star-inserted
2: div#export-main-1.d-flex.w-100.ng-star-inserted
3: div#export-main-2.d-flex.w-100.ng-star-inserted

Since I use Angular and I need some dynamically set ids, the ids of the 1,2 and 3 divs are set like this in HTML:由于我使用 Angular 并且我需要一些动态设置的 id,因此在 HTML 中,1,2 和 3 div 的 id 设置如下:

<div [id]="'export-main-' + index"></div>

I have no iframes in my program, trackers, or other 3rd party injectors like google ads, Did anyone else encounter this problem before and knows how to fix it?我的程序、跟踪器或其他 3rd 方注入器(如谷歌广告)中没有 iframe,有没有其他人遇到过这个问题并且知道如何解决它?

EDIT: Maybe this proves helpful.编辑:也许这被证明是有帮助的。 This is the file where the error probably triggers, since I found this exact error in there:这是可能触发错误的文件,因为我在那里发现了这个确切的错误:

https://github.com/niklasvh/html2canvas/blob/master/src/index.ts https://github.com/niklasvh/html2canvas/blob/master/src/index.ts

I found the issue in the end我最后发现了问题

The nesting of the HTML looked like this: HTML 的嵌套如下所示:

<div data-html2canvas-ignore="true">
   <div>
      <div id="export-main-0></div>
   </div>
</div>

The issue was that one of the parent divs had that data-html2canvas-ignore tag applied on it, which made the child div being undetectable by the html2canvas.问题是其中一个父 div 上应用了 data-html2canvas-ignore 标记,这使得子 div 无法被 html2canvas 检测到。 Simply removing the ignore rule from the parent div made everything work again.只需从父 div 中删除忽略规则,一切都会重新开始。

If you want to clone the node and add elements or styles,如果要克隆节点并添加元素或styles,

Try onclone like this:像这样尝试克隆

html2canvas(elementById[index], {
    onclone: function (documentClone) {

        var headnode = document.createElement("h1");
        var textnode = document.createTextNode('Hello World');  
        headnode.appendChild(textnode);

        documentClone.querySelector('#list').prepend(headnode)
        (<HTMLElement>documentClone.querySelector('body')).style.padding = '30px';
      }
  }).then(canvas => {
     ...
})

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

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