Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
26
rated 0 times [  30] [ 4]  / answers: 1 / hits: 6268  / 4 Years ago, tue, december 1, 2020, 12:00:00

I am trying to generate the PDF file from HTML content.
contentDataURL is having the empty image data. I have tried changing the HTML content also but the same empty image is getting generated.
What's wrong with my canvas.toDataURL() implementation?
PDF file generation is working fine.


my code app link
https://stackblitz.com/edit/angular-ivy-1aug8i


<div class="test" id="test">
<button (click)="sendToPdf()">
testpdf</button> </div>
<div class="abc" id="testdivid">
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
</div>

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');
});
}

More From » angular

 Answers
2

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');
});
}

[#2195] Thursday, November 26, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tajo

Total Points: 415
Total Questions: 124
Total Answers: 103

Location: North Korea
Member since Tue, Jun 16, 2020
4 Years ago
tajo questions
Wed, Apr 6, 22, 00:00, 2 Years ago
Wed, Jan 26, 22, 00:00, 2 Years ago
Fri, Jul 17, 20, 00:00, 4 Years ago
;