离子4 html2canvas

问题描述 投票:1回答:2

我正在尝试捕获HTML以在Ionic 4应用程序中成像。我尝试使用html2canvas,然而,它不起作用并在控制台中显示此输出:enter image description here

这是我的代码:

var element = document.getElementById('capture');
  html2canvas(element).then(canvas => {
    var imgData = canvas.toDataURL("image/png");
    this.socialSharing.share(null, null, imgData);
});
javascript ionic-framework html2canvas ionic4
2个回答
1
投票

我也在试用这个组件,但它不起作用。我找到的解决方案是使用dom-to-image库。我附上了我所做的一个例子。您只需要放置要转换的文件扩展名即可。

https://www.npmjs.com/package/jspdf

https://www.npmjs.com/package/dom-to-image

import * as jsPDF from 'jspdf';
import domtoimage from 'dom-to-image';

exportPdf(){
    const div = document.getElementById('pdf');
    const options = { background: 'white', height: 845, width: 595 };
    domtoimage.toPng(div, options).then((dataUrl) => {
        //Initialize JSPDF
        const doc = new jsPDF('p', 'mm', 'a4');
        //Add image Url to PDF
        doc.addImage(dataUrl, 'PNG', 0, 0, 210, 340);
        doc.save('pdfDocument.pdf');
    }
}

0
投票

我有同样的问题。我设法通过将我的代码放在<ion-content>标签之外来“修复”它。我的猜测是html2canvas不渲染阴影DOM元素(这是<ion-content>的情况,从Ionic 4开始)。

例如,这会输出一个空画布:

<ion-content>
  <div id="capture">
    <h1>Test</h1>
  </div>
</ion-content>

但以下工作:

<div id="capture">
  <h1>Test</h1>
</div>

这不是一个真正的解决方案,但它足以让我使用......

© www.soinside.com 2019 - 2024. All rights reserved.