doc.fromHTML 不是 angular 6 的函数 [英] doc.fromHTML is not a function with angular 6

查看:22
本文介绍了doc.fromHTML 不是 angular 6 的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将模板 html 导出为 pdf,我正在尝试使用带有 angular 的 jsPDF lib,但出现此错误 doc.fromHTML is not a function.

I wanna export a template html to pdf and I'm trying use jsPDF lib with angular, but o got this error doc.fromHTML is not a function.

import { Component, ViewChild, ElementRef } from '@angular/core';
import * as jsPDF from 'jspdf';
// declare var jsPDF: any;
@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'exports-pdf';
@ViewChild('exports') exports: ElementRef;

public print(): void {

const doc = new jsPDF('p', 'pt', 'letter');

const content = this.exports.nativeElement;

const margins = {
  top: 80,
  bottom: 60,
  left: 40,
  width: 522
};
console.log(doc);
setTimeout(() => {
  doc.fromHTML(content.innerHTML, margins.left, margins.top, {}, function() {
    doc.output('export.pdf');
  }, margins);
  }, 100);
 }
}

我也尝试使用 import * as jsPDF from 'jspdf';或声明 var jsPDF: any;

当我使用 declare var jsPDF: any 时,我在控制台拥有所有 jsPDF 属性,但是当我单击按钮时没有任何反应.当我使用 import * as jsPDF from 'jspdf' 时,我有一个没有任何属性的对象,如下图:

when I use declare var jsPDF: any, I have at the console all jsPDF properties, but nothing happen when I click on button. When I use import * as jsPDF from 'jspdf' i have an object without any properties like this image:

在 angular.json 中,我在脚本中放入了导入

In angular.json i put in scripts the imports

<代码>脚本":["./node_modules/jspdf/dist/jspdf.min.js",./node_modules/jspdf/dist/jspdf.debug.js"]

然后错误继续.

我发现其他人也有同样的错误,但没有解决方案.我不知道这是 lib 错误还是 angular 效果不佳.

I found other dudes with the same error but without solution. I don't know if it is a lib bug or if with angular it not work very well.

我将代码上传到 GitHub.

推荐答案

从您的代码中删除我评论的内容.要导出为 pdf,您需要使用 save 函数 doc.save("export.pdf");

Remove what I commented from your code. To export as pdf you need to use save function doc.save("export.pdf");

app.component.ts

import { Component, ViewChild, ElementRef } from '@angular/core';
// import { log } from 'util'; ** REMOVE **
// import * as jsPDF from 'jspdf'; ** REMOVE **
declare var jsPDF: any;
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'exports-pdf';
  @ViewChild('exports') exports: ElementRef;

  public print(): void {

    const doc = new jsPDF('p', 'pt', 'letter');

    const content = this.exports.nativeElement;

    const margins = {
      top: 80,
      bottom: 60,
      left: 40,
      width: 522
    };
    console.log(doc);
 //   setTimeout(() => { ** REMOVE **
      doc.fromHTML(content.innerHTML, margins.left, margins.top, {}, function () {
      //  doc.output('export.pdf'); ** REMOVE **
        doc.save("export.pdf");
      }, margins);
 //   }, 100); ** REMOVE **
  }
}

angular.json

"scripts": [
   "./node_modules/jspdf/dist/jspdf.min.js",
 //  "./node_modules/jspdf/dist/jspdf.debug.js" ** REMOVE **
 ]

index.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>ExportsPdf</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
<!-- ** REMOVE ** <script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
  <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.debug.js"></script> -->
</head>
<body>
  <app-root></app-root>
</body>
</html>

这篇关于doc.fromHTML 不是 angular 6 的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆