typescript - HTML2Canvas 在 Ionic4 角度项目中生成空白图像。控制台中没有错误。相同的代码在纯 html/javascript 中生成正确的图像

标签 typescript html2canvas ionic4

我在 Ionic4 角度测试项目的 home.page.ts 中安装并导入了 html2canvas。我有一个普通的 100px X 100px、黄色背景 div,带有一行文本。我将此 div 传递给 html2canvas 以下载为 png 格式。 html2canvas 在控制台中列出所有步骤(没有错误),并生成与给定 div 大小相同的 png,但 div 是空的。它没有 div 或文本的颜色。

更改 div 的大小会正确生效,这意味着生成的图像与 div 的大小相同。此测试项目演示了该问题 - https://stackblitz.com/edit/html2cavasionic4test .

这是我的 home.page.ts

import { Component } from '@angular/core';
import * as html2canvas from 'html2canvas';


@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
  selected = 1;
  list: any = [{ id: 0, text: '0' }, { id: 1, text: 1 }];
  changed(e) {
    console.log(this.selected);
  }
  onPrintClicked(){
    let div = document.getElementById("h2cTest");
    const options = {allowTaint:true, background: "yellow", height: div.clientHeight, width: div.clientWidth };
    html2canvas.default(div, options)
    .then((canvas)=>{
      var myImage = canvas.toDataURL("image/png");
      console.log("image done");
      this.downloadURI("data:" + myImage, "yourImage.png");
    })
    .catch(()=>{})
  }
  downloadURI(uri, name) {
    var link = document.createElement("a");
    link.download = name;
    link.href = uri;
    link.click();
    //after creating link you should delete dynamic link
    //clearDynamicLink(link); 
  }

}

在我的本地计算机上,我创建了一个纯 html/javascript 页面,其功能与上面的 stackblitz 项目相同,并在页面上包含了脚本 html2canvs.min.js。这个简单的页面按预期工作并生成正确的图像。

html2Canvas 预计会生成 div 的图像,但它正在生成空白图像。是不是设置有问题?

更新

应用程序标记是问题所在。

最佳答案

https://www.npmjs.com/package/dom-to-image -> 比 html2canvas 效果更好 https://www.npmjs.com/package/jspdf ->如果您需要保存为pdf

页面.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 (在此处查看更多信息image-in-ionic4-angular)

注意:当前版本的 jsPDF 出现错误。 这是npm install <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dab0a9aabebc9aebf4eff4e9" rel="noreferrer noopener nofollow">[email protected]</a> --save工作正常

您可以在此处查看该网站的示例: Site

以及生成的 pdf

pdf

关于typescript - HTML2Canvas 在 Ionic4 角度项目中生成空白图像。控制台中没有错误。相同的代码在纯 html/javascript 中生成正确的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54650369/

相关文章:

javascript - 如何解决我的 TypeScript/ESLint/Webpack 转译问题

angular - 在路由中将对象作为参数传递返回 [Object object]

jsPDF:使用新的 .html() 方法时未加载 html2canvas

jquery - 防止某些元素渲染 Canvas

javascript - Angular 7 单例服务 : No provider for Service

angular - Enter键以Ionic 4 Angular 8形式移动到下一个元素

typescript 定义只能包含特定字符串的数组

javascript - 使用 html2canvas 时,传单 map 多边形未显示在 Canvas 上

ionic-framework - 相机的FILE_URI路径不适用于IONIC 4

typescript /Hapi : Property 'example' does not exist on type 'PluginProperties'