首页 > 解决方案 > Angular Html2Pdf - DOMException:无法在 'ShadowRoot' 上设置 'adoptedStyleSheets' 属性

问题描述

使用 Angular 12 实现 html2pdf,将部分 html 页面转换为 PDF 以供下载。

控制台错误

index-7a8b7a1c.js:150 Uncaught (in promise) DOMException: 无法在 'ShadowRoot' 上设置 'adoptedStyleSheets' 属性:不允许在多个文档中共享构造的样式表

HTML

<section id="toPdfContainer">
    <h5 class="card-title">Hello WOrld!</h5>
</section>
<button class="btn btn-primary w-100" (click)="toPdf()">TO PDF</button>

脚本

import * as html2pdf from 'html2pdf.js';

...

public toPdf() {
   const elementToPdf = document.getElementById('toPdfContainer');
   console.log('to PDF', elementToPdf);
   const opt = {
      margin: 1,
      filename: 'myfile.pdf',
      image: { type: 'jpeg', quality: 0.98 },
      html2canvas: { scale: 2 },
      jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
   };
   html2pdf().set(opt).from(elementToPdf).save();
}

谁能帮我这个?

标签: javascriptangulartypescripthtml2pdfangular12

解决方案


推荐阅读