What I found worked was adding:
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
to the index.html file (it could presumably be elsewhere).
I then used:
const elementToPrint = document.getElementById('foo'); //The html element to become a pdf
const pdf = new jsPDF('p', 'pt', 'a4');
pdf.addHTML(elementToPrint, () => {
doc.save('web.pdf');
});
Which no longer uses html2canvas in the code.
You can then remove the following import:
import * as html2canvas from 'html2canvas';
Answer from Greg on Stack OverflowWhat I found worked was adding:
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
to the index.html file (it could presumably be elsewhere).
I then used:
const elementToPrint = document.getElementById('foo'); //The html element to become a pdf
const pdf = new jsPDF('p', 'pt', 'a4');
pdf.addHTML(elementToPrint, () => {
doc.save('web.pdf');
});
Which no longer uses html2canvas in the code.
You can then remove the following import:
import * as html2canvas from 'html2canvas';
In case someone prefer not to use cdn scripts & would like to use a more (angular) way, this worked for me in Angular 6:
Using this way will give you better support & autocomplete in the editor & will help you avoid depending on cdn scripts (if you wanna avoid them, like me)
Based on the excellent answer here & since it was hard for me to find that answer, I am re-sharing what was stated in it & helped me use jsPDF in Angular 6 (all credit goes to the original author of this answer)
You should run these cmds:
npm install jspdf --save
typings install dt~jspdf --global --save
npm install @types/jspdf --save
Add following in angular-cli.json:
"scripts": [ "../node_modules/jspdf/dist/jspdf.min.js" ]
html:
<button (click)="download()">download </button>
component 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');
}
}
How to convert HTML content to PDF in Angular
how to set top and bottom margin in .HTML using jspdf
anyone experience with jsPDF? My image is cut off
Can I access the HTML of a component that is simply created in a TS file and not included in the actual app?
Videos
after reinstalling html2canvas worked fine
import html2canvas from 'html2canvas';
import jspdf from 'jspdf';
sendToPdf(){
let data = document.getElementById("test");
// let data = document.getElementById("maindiv");
console.log(data);
html2canvas(data).then(canvas => {
const contentDataURL = canvas.toDataURL('image/jpeg', 1.0)
console.log(contentDataURL);
let pdf = new jspdf('l', 'cm', 'a4'); //Generates PDF in landscape mode
// let pdf = new jspdf('p', 'cm', 'a4'); //Generates PDF in portrait mode
pdf.addImage(contentDataURL, 'PNG', 0, 0, 29.7, 21.0);
pdf.save('Filename.pdf');
});
}
To use your code in Angular13+ you must add 2 lines of code, otherwise there will be an Type 'null' is not assignable to type 'HTMLElement' Error:
You may use:
sendToPdf() {
let data = document.getElementById('test');
// let data = document.getElementById("maindiv");
console.log(data);
if (data != null) {
html2canvas(data).then((canvas) => {
const contentDataURL = canvas.toDataURL('image/jpeg', 1.0);
console.log(contentDataURL);
let pdf = new jsPDF('l', 'cm', 'a4'); //Generates PDF in landscape mode
// let pdf = new jspdf('p', 'cm', 'a4'); //Generates PDF in portrait mode
pdf.addImage(contentDataURL, 'PNG', 0, 0, 29.7, 21.0);
pdf.save('Filename.pdf');
});
}
}