https://www.npmjs.com/package/dom-to-image https://www.npmjs.com/package/dom-to-image-> 比 html2canvas 效果更好https://www.npmjs.com/package/jspdf https://www.npmjs.com/package/jspdf->如果您需要保存为pdf
page.ts
import * as jsPDF from 'jspdf';
//or import jsPDF from 'jspdf';
import domtoimage from 'dom-to-image';
captureScreen() {
const div = document.getElementById('pdfContent');
const divHeight = div.clientHeight;
const divWidth = div.clientWidth;
const options = { background: 'white', width: divWidth, height: divHeight };
domtoimage.toPng(div, options).then((imgData) => {
const doc = new jsPDF('p', 'mm', [divWidth, divHeight]);
const imgProps = doc.getImageProperties(imgData);
const pdfWidth = doc.internal.pageSize.getWidth();
const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;
doc.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
doc.save('pdfDocument.pdf');
});
}
页面.html
<ion-button (click)="captureScreen('pdfContent')">
<ion-icon name="print" size='small' slot="icon-only"></ion-icon>
</ion-button>
<div id="pdfContent">
<h1>Testing this to pdf</h1>
</div>
曾参与 ionic 5
(在这里查看更多离子四角图像 https://stackoverflow.com/questions/54650369/html2canvas-generating-blank-image-in-ionic4-angular-proj-no-error-in-console/63670250#63670250)
注意:当前版本的 jsPDF 出现错误。
这是npm install [email protected] /cdn-cgi/l/email-protection --save
工作正常
You can see the example here of the site:
![Site](https://i.stack.imgur.com/yXeof.png)
以及生成的 pdf