If you are using jsPDF, you can think your multipage download PDF function like in below snippets -
downloadPDF() {
const htmlWidth = document.getElementById('idcard_main').offsetWidth;
const htmlHeight = document.getElementById('idcard_main').offsetHeight;
const topLeftMargin = 15;
let pdfWidth = htmlWidth + (topLeftMargin * 2);
let pdfHeight = (pdfWidth * 1.5) + (topLeftMargin * 2);
const canvasImageWidth = htmlWidth;
const canvasImageHeight = htmlHeight;
const totalPDFPages = Math.ceil(htmlHeight / pdfHeight) - 1;
const data = document.getElementById('idcard_main');
html2canvas(data, { allowTaint: true }).then(canvas => {
canvas.getContext('2d');
const imgData = canvas.toDataURL("image/jpeg", 1.0);
let pdf = new jsPDF('p', 'pt', [pdfWidth, pdfHeight]);
pdf.addImage(imgData, 'png', topLeftMargin, topLeftMargin, canvasImageWidth, canvasImageHeight);
for (let i = 1; i <= totalPDFPages; i++) {
pdf.addPage([pdfWidth, pdfHeight], 'p');
pdf.addImage(imgData, 'png', topLeftMargin, - (pdfHeight * i) + (topLeftMargin * 4), canvasImageWidth, canvasImageHeight);
}
pdf.save(`Document ${new Date().toLocaleString()}.pdf`);
});
}
Answer from Shalim Ahmed on Stack OverflowIf you are using jsPDF, you can think your multipage download PDF function like in below snippets -
downloadPDF() {
const htmlWidth = document.getElementById('idcard_main').offsetWidth;
const htmlHeight = document.getElementById('idcard_main').offsetHeight;
const topLeftMargin = 15;
let pdfWidth = htmlWidth + (topLeftMargin * 2);
let pdfHeight = (pdfWidth * 1.5) + (topLeftMargin * 2);
const canvasImageWidth = htmlWidth;
const canvasImageHeight = htmlHeight;
const totalPDFPages = Math.ceil(htmlHeight / pdfHeight) - 1;
const data = document.getElementById('idcard_main');
html2canvas(data, { allowTaint: true }).then(canvas => {
canvas.getContext('2d');
const imgData = canvas.toDataURL("image/jpeg", 1.0);
let pdf = new jsPDF('p', 'pt', [pdfWidth, pdfHeight]);
pdf.addImage(imgData, 'png', topLeftMargin, topLeftMargin, canvasImageWidth, canvasImageHeight);
for (let i = 1; i <= totalPDFPages; i++) {
pdf.addPage([pdfWidth, pdfHeight], 'p');
pdf.addImage(imgData, 'png', topLeftMargin, - (pdfHeight * i) + (topLeftMargin * 4), canvasImageWidth, canvasImageHeight);
}
pdf.save(`Document ${new Date().toLocaleString()}.pdf`);
});
}
I got the solution from a tutorial. Posting it here since it may help someone else.
Generate Multipage PDF using Single Canvas of HTML Document using jsPDF
How to Create Multipage PDF from HTML Using jsPDF and html2Canvas
Videos
Best possible solution I could come up with till now.
You would have to install the below packages from npm
html2canvas
jspdf
import * as jsPDF from 'jspdf';
import html2canvas from 'html2canvas';
htmltoPDF()
{
// parentdiv is the html element which has to be converted to PDF
html2canvas(document.querySelector("#parentdiv")).then(canvas => {
var pdf = new jsPDF('p', 'pt', [canvas.width, canvas.height]);
var imgData = canvas.toDataURL("image/jpeg", 1.0);
pdf.addImage(imgData,0,0,canvas.width, canvas.height);
pdf.save('converteddoc.pdf');
});
}
UPDATE:
Came up with another solution. I wasn't able to break it down into A4 size pages, but I was able to make a single pdf file.
Packages:
dom-to-image
jspdf
import domtoimage from 'dom-to-image';
import * as jsPDF from 'jspdf';
downloadPDF()
{
var node = document.getElementById('parentdiv');
var img;
var filename;
var newImage;
domtoimage.toPng(node, { bgcolor: '#fff' })
.then(function(dataUrl) {
img = new Image();
img.src = dataUrl;
newImage = img.src;
img.onload = function(){
var pdfWidth = img.width;
var pdfHeight = img.height;
// FileSaver.saveAs(dataUrl, 'my-pdfimage.png'); // Save as Image
var doc;
if(pdfWidth > pdfHeight)
{
doc = new jsPDF('l', 'px', [pdfWidth , pdfHeight]);
}
else
{
doc = new jsPDF('p', 'px', [pdfWidth , pdfHeight]);
}
var width = doc.internal.pageSize.getWidth();
var height = doc.internal.pageSize.getHeight();
doc.addImage(newImage, 'PNG', 10, 10, width, height);
filename = 'mypdf_' + '.pdf';
doc.save(filename);
};
})
.catch(function(error) {
// Error Handling
});
}
The following code was used and worked for my project
Step 1 : Run following commands to install npm packages
> npm install jspdf
> npm install html2canvas
Step 2: Import installed packages in app.components.ts. I haven't imported those packages in constructor()
> import * as jspdf from 'jspdf';
> import html2canvas from 'html2canvas';
Step 3: Give an id for the HTML div that has to be exported as PDF. Add a button that activates the function too.
<div id="MyDIv" style="margin-left: 45px;" class="main-container">
</div>
<div class="icon_image " title="Share As PDF" (click)="exportAsPDF('MyDIv');"><img src="assets/img/pdf.png"></div>
Step 4 : Write the code for generating PDF as follows
exportAsPDF(div_id)
{
let data = document.getElementById(div_id);
html2canvas(data).then(canvas => {
const contentDataURL = canvas.toDataURL('image/png')
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');
});
}
You can use jspdf.
working Demo
.html
<div id="pdfTable" #pdfTable>
<h1>{{name}}</h1>
<table>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>
</div>
<div> <button (click)="downloadAsPDF()">Export To PDF</button></div>
.ts
public downloadAsPDF() {
const doc = new jsPDF();
const specialElementHandlers = {
'#editor': function (element, renderer) {
return true;
}
};
const pdfTable = this.pdfTable.nativeElement;
doc.fromHTML(pdfTable.innerHTML, 15, 15, {
width: 190,
'elementHandlers': specialElementHandlers
});
doc.save('tableToPdf.pdf');
}
You need to display the contents to be printed within a DIV. After displaying the contents, use the following code which was used and worked for my project
Step 1 :
Run following commands to install npm packages
> npm install jspdf
> npm install html2canvas
Step 2:
Import installed packages in app.components.ts. I haven't imported those packages in constructor()
> import * as jspdf from 'jspdf';
> import html2canvas from 'html2canvas';
Step 3:
Give an id for the HTML div that has to be exported as PDF. Add a button that activates the function too.
<div id="MyDIv" style="margin-left: 45px;" class="main-container">
</div>
<div class="icon_image " title="Share As PDF" (click)="exportAsPDF('MyDIv');"><img src="assets/img/pdf.png"></div>
Step 4 :
call the code for generating PDF as follows
exportAsPDF(divId)
{
let data = document.getElementById('divId');
html2canvas(data).then(canvas => {
const contentDataURL = canvas.toDataURL('image/png') // 'image/jpeg' for lower quality output.
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');
});
}