我收到错误:未定义jsPDF,我现在正在使用以下代码:
import { Component, OnInit, Inject } from '@angular/core';
import 'jspdf';
declare let jsPDF;
@Component({
....
providers: [
{ provide: 'Window', useValue: window }
]
})
export class GenratePdfComponent implements OnInit {
constructor(
@Inject('Window') private window: Window,
) { }
download() {
var doc = jsPDF();
doc.text(20, 20, 'Hello world!');
doc.text(20, 30, 'This is client-side Javascript, pumping out a PDF.');
doc.save('Test.pdf');
}
}
我安装了jsPDF的npm,但不知道如何导入jspdf并使用angular-cli运行:1.0.0-beta.17
答案 0 :(得分:68)
我已经完成了这项工作,经过大量的R& D之后,他们只需按照以下步骤操作: 安装:
npm install jspdf --save
typings install dt~jspdf --global --save
npm install @types/jspdf --save
在angular-cli.json中添加以下内容:
"scripts": [ "../node_modules/jspdf/dist/jspdf.min.js" ]
HTML:
<button (click)="download()">download </button>
组件ts:
import { Component, OnInit, Inject } from '@angular/core';
import * as jsPDF from 'jspdf'
@Component({
...
providers: [
{ provide: 'Window', useValue: window }
]
})
export class GenratePdfComponent implements OnInit {
constructor(
@Inject('Window') private window: Window,
) { }
download() {
var doc = new jsPDF();
doc.text(20, 20, 'Hello world!');
doc.text(20, 30, 'This is client-side Javascript, pumping out a PDF.');
doc.addPage();
doc.text(20, 20, 'Do you like that?');
// Save the PDF
doc.save('Test.pdf');
}
}
答案 1 :(得分:6)
我在 @gsmalhotra
的帮助下,基于this answer创建了一个jsPdf演示首先安装
npm install jspdf --save
我使用了一系列图片,首先将图片转换为base64 (jsPDF不允许图片网址)
getBase64Image(img) {
var canvas = document.createElement("canvas");
console.log("image");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL("image/jpeg");
return dataURL;
}
然后在循环中,我调用了上面的函数返回base64图像,然后使用doc.addImage()
将图像添加到PDF。
for(var i=0;i<this.images.length;i++){
let imageData= this.getBase64Image(document.getElementById('img'+i));
doc.addImage(imageData, "JPG", 10, (i+1)*10, 180, 150);
doc.addPage();
}
HTML代码
<div class="col-md-4" *ngFor="let img of images;let i=index">
<img id="img{{i}}" class="img-fluid" crossOrigin="Anonymous" [src]="img.url">
</div>
答案 2 :(得分:3)
旧问题,但这是我在我的角度应用程序中启动并运行的替代解决方案。我最终修改了this jsPDF solution使其无法使用ionic / cordova CLI进行工作。
npm install jspdf --save
npm install @types/jspdf --save
npm install html2canvas --save
npm install @types/html2canvas --save
向包含要生成PDF内容的div中添加一个ID
<div id="html2Pdf">your content here</div>
导入库
import * as jsPDF from 'jspdf';
import * as html2canvas from 'html2canvas';
添加生成PDF的方法
generatePdf() {
const div = document.getElementById("html2Pdf");
const options = {background: "white", height: div.clientHeight, width: div.clientWidth};
html2canvas(div, options).then((canvas) => {
//Initialize JSPDF
let doc = new jsPDF("p", "mm", "a4");
//Converting canvas to Image
let imgData = canvas.toDataURL("image/PNG");
//Add image Canvas to PDF
doc.addImage(imgData, 'PNG', 20, 20);
let pdfOutput = doc.output();
// using ArrayBuffer will allow you to put image inside PDF
let buffer = new ArrayBuffer(pdfOutput.length);
let array = new Uint8Array(buffer);
for (let i = 0; i < pdfOutput.length; i++) {
array[i] = pdfOutput.charCodeAt(i);
}
//Name of pdf
const fileName = "example.pdf";
// Make file
doc.save(fileName);
});
}
我发现此解决方案对于我的Web应用程序非常有效,并且非常有益,因为我可以控制何时生成PDF(异步接收数据后)。同样,我不需要在全球范围内安装任何库。
答案 3 :(得分:2)
以下是在 Angular 11 中下载 div
的方式:
npm install jspdf --save
html
中添加模板引用:<div #myTemp>
// other code here
<button (click)="print()"></button>
</div>
xyz.component.ts
中导入:import { jsPDF } from "jspdf";
import html2canvas from 'html2canvas';
export class XYZComponent {
@ViewChild('myTemp') myTempRef: ElementRef;
print(){
const DATA = this.myTempRef.nativeElement;
html2canvas(DATA).then(canvas => {
let fileWidth = 200;
let fileHeight = canvas.height * fileWidth / canvas.width;
const FILEURI = canvas.toDataURL('image/png')
let PDF = new jsPDF('p', 'mm', 'a4');
let position = 0;
PDF.addImage(FILEURI, 'PNG', 5, 5, fileWidth, fileHeight)
PDF.save('angular-demo.pdf');
});
}
}
答案 4 :(得分:0)
1.安装包
npm install jspdf
2.像跟随一样导入
import { jsPDF } from 'jspdf'
// important! using this ES6 syntax you don't need to import the library via the script
section in angular.json
3.使用它
// for somereadon the latest version (currenly 2.3) words with properties
of type string only, even if you have dates or numbers, convert them to string first
let data = [
{
id: "123032510",
firstname: "Benzara Tahar",
lastname: "Riad",
},
{
loggedInUserId: "1233205",
firstname: "Matouk",
lastname: "Aymen",
}
];
let header = this.createHeaders(['ID', 'Firstname', 'Lastname']);
let doc = new jsPDF()
doc.table(1, 1, data, header, {})
doc.save()