Javascript HTML2Canvas:在克隆的iframe中找不到元素

Javascript HTML2Canvas:在克隆的iframe中找不到元素,javascript,angular,html2canvas,Javascript,Angular,Html2canvas,在我的程序中,我试图导出一些页面的PDF,但当HTML2Canvas尝试处理我拥有的元素时,我遇到了以下错误: 在克隆的iframe中找不到元素 只有这个。这是我使用的代码: public exportToPdf( selectedFilter: SelectedFilter, currentTime: string, idOfExportDiv: string[] ): void { this.addClassToMainDiv(); const

在我的程序中,我试图导出一些页面的PDF,但当HTML2Canvas尝试处理我拥有的元素时,我遇到了以下错误:

在克隆的iframe中找不到元素

只有这个。这是我使用的代码:

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);
  }
因为我使用Angular并且需要一些动态设置的ID,所以在HTML中,1、2和3 div的ID设置如下:

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

我的程序中没有iFrame、跟踪器或其他第三方注入器,如谷歌广告, 以前有没有其他人遇到过这个问题,并且知道如何解决它

编辑:也许这有帮助。这是可能触发错误的文件,因为我在其中发现了这个错误:


我最终发现了这个问题

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
<div data-html2canvas-ignore="true">
   <div>
      <div id="export-main-0></div>
   </div>
</div>
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 => {
     ...
})


如果要克隆节点并添加元素或样式

尝试以下方式进行克隆操作:

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
<div data-html2canvas-ignore="true">
   <div>
      <div id="export-main-0></div>
   </div>
</div>
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 => {
     ...
})
html2canvas(elementById[index]{
onclone:函数(documentClone){
var headnode=document.createElement(“h1”);
var textnode=document.createTextNode('Hello World');
headnode.appendChild(textnode);
documentClone.querySelector(“#list”).prepend(头节点)
(documentClone.querySelector('body')).style.padding='30px';
}
})。然后(画布=>{
...
})

谢谢。成功了!!!