GsM*_*tra 24 jspdf angular2-cli angular
我收到一个错误:没有定义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');
}
}
Run Code Online (Sandbox Code Playgroud)
我已经安装了jsPDF的npm,但不知道如何导入jspdf并使用angular-cli运行:1.0.0-beta.17
GsM*_*tra 74
我已经完成了这项工作,经过大量的研发后,他们只需按照以下步骤操作: 安装:
npm install jspdf --save
typings install dt~jspdf --global --save
npm install @types/jspdf --save
Run Code Online (Sandbox Code Playgroud)
在angular-cli.json中添加以下内容:
"scripts": [ "../node_modules/jspdf/dist/jspdf.min.js" ]
Run Code Online (Sandbox Code Playgroud)
HTML:
<button (click)="download()">download </button>
Run Code Online (Sandbox Code Playgroud)
组件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');
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
我创建基于一个jsPdf演示这个答案与帮助@gsmalhotra
首先安装
npm install jspdf --save
Run Code Online (Sandbox Code Playgroud)
我使用了图像数组,首先将图像转换为base64 (jsPDF不允许使用图像URL)
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;
}
Run Code Online (Sandbox Code Playgroud)
然后在循环中,我调用了上面的函数,返回base64图像,然后使用将图像添加到PDF doc.addImage().
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();
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
旧问题,但这是我在我的角度应用程序中启动并运行的替代解决方案。我最终修改了这个jsPDF解决方案,使其无需使用ionic / cordova CLI即可工作。
npm install jspdf --save
npm install @types/jspdf --save
npm install html2canvas --save
npm install @types/html2canvas --save
Run Code Online (Sandbox Code Playgroud)
向包含要生成PDF内容的div中添加一个id
<div id="html2Pdf">your content here</div>
Run Code Online (Sandbox Code Playgroud)
导入库
import * as jsPDF from 'jspdf';
import * as html2canvas from 'html2canvas';
Run Code Online (Sandbox Code Playgroud)
添加生成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);
});
}
Run Code Online (Sandbox Code Playgroud)
我发现此解决方案对于我的Web应用程序非常有效,并且非常有益,因为我可以控制何时生成PDF(异步接收数据后)。同样,我不需要全局安装任何库。
| 归档时间: |
|
| 查看次数: |
39504 次 |
| 最近记录: |