-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working fine, need to do some alterations
- Loading branch information
Showing
1 changed file
with
69 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,82 @@ | ||
(function () { | ||
var doc = new jspdf.jsPDF("p", "pt", "a4"); | ||
var sourceElement = document.querySelector(".pdf_print_container"); | ||
doc.html(sourceElement, { | ||
callback: function (pdf) { | ||
console.log(pdf); | ||
// pdf.save("sample-file.pdf"); | ||
var iframe = document.createElement("iframe"); | ||
iframe.setAttribute( | ||
"style", | ||
` | ||
position:absolute; | ||
function renderPrintPage(sourceElement, endCallback) { | ||
function getConvertedPdfFromHtml(doc, callback) { | ||
const opt = { | ||
margin: [5, 0, 8, 0], | ||
enableLinks: false, | ||
pagebreak: { | ||
avoid: ["tr", ".submodule-block", ".preface-page", ".first-page"], | ||
mode: ["css", "legacy"], | ||
}, | ||
image: { type: "jpeg", quality: 1 }, | ||
html2canvas: { | ||
allowTaint: true, | ||
dpi: 300, | ||
letterRendering: true, | ||
logging: false, | ||
scale: 2, | ||
scrollX: 0, | ||
scrollY: 0, | ||
}, | ||
jsPDF: { doc }, | ||
}; | ||
|
||
return html2pdf() | ||
.from(sourceElement) | ||
.set(opt) | ||
.toPdf() | ||
.get("pdf") | ||
.then((pdf) => { | ||
const totalPages = pdf.internal.getNumberOfPages(); | ||
|
||
for (let i = 1; i < totalPages + 1; i++) { | ||
pdf.setPage(i); | ||
pdf.setFontSize(8); | ||
pdf.text( | ||
`Page ${i} of ${totalPages}`, | ||
pdf.internal.pageSize.getWidth() - 10, | ||
pdf.internal.pageSize.getHeight() - 5 | ||
); | ||
} | ||
|
||
callback(pdf); | ||
}); | ||
} | ||
|
||
function addPdfToIframe(doc) { | ||
var iframe = document.createElement("iframe"); | ||
iframe.setAttribute( | ||
"style", | ||
` | ||
position:fixed; | ||
right:0; | ||
top:0; | ||
bottom:0; | ||
size: A4; | ||
left: 0; | ||
z-index: 2000; | ||
width: 100%; | ||
height: 100%; | ||
padding:20px; | ||
z-index: 2; | ||
` | ||
); | ||
var blobPDF = new Blob([pdf.output("blob")], { type: "application/pdf" }); | ||
var blobUrl = URL.createObjectURL(blobPDF); | ||
iframe.src = blobUrl; | ||
); | ||
|
||
document.body.appendChild(iframe); | ||
console.log(doc); | ||
var blobPDF = new Blob([doc.output("blob")], { type: "application/pdf" }); | ||
var blobUrl = URL.createObjectURL(blobPDF); | ||
iframe.src = blobUrl; | ||
|
||
const opt = { | ||
margin: [5, 0, 8, 0], | ||
filename: `sample.pdf`, | ||
enableLinks: false, | ||
pagebreak: { | ||
avoid: ["tr", ".submodule-block"], | ||
mode: ["css", "legacy"], | ||
}, | ||
image: { type: "jpeg", quality: 1 }, | ||
html2canvas: { | ||
allowTaint: true, | ||
dpi: 300, | ||
letterRendering: true, | ||
logging: false, | ||
scale: 2, | ||
scrollX: 0, | ||
scrollY: 0, | ||
}, | ||
jsPDF: { doc }, | ||
}; | ||
document.body.appendChild(iframe); | ||
|
||
var htmlPDF = html2pdf() | ||
.from(sourceElement) | ||
.set(opt) | ||
.toPdf() | ||
.get("pdf") | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
.then((pdf) => { | ||
const totalPages = pdf.internal.getNumberOfPages(); | ||
iframe.onload = function () { | ||
endCallback(iframe); | ||
}; | ||
} | ||
|
||
for (let i = 1; i < totalPages + 1; i++) { | ||
pdf.setPage(i); | ||
pdf.setFontSize(8); | ||
pdf.text( | ||
`Page ${i} of ${totalPages}`, | ||
pdf.internal.pageSize.getWidth() - 10, | ||
pdf.internal.pageSize.getHeight() - 5 | ||
); | ||
} | ||
}) | ||
.save(); | ||
var doc = new jspdf.jsPDF("p", "pt", "a4"); | ||
|
||
console.log(htmlPDF); | ||
}, | ||
html2canvas: { | ||
scale: 0.5, | ||
pageSplit: true, | ||
}, | ||
getConvertedPdfFromHtml(doc, (pdf) => { | ||
addPdfToIframe(pdf); | ||
}); | ||
} | ||
|
||
// const options = { | ||
// margin: 0, | ||
// file_name: file_name, | ||
// image: { type: "jpeg", quality: 0.98 }, | ||
// html2canvas: { scale: 2 }, | ||
// jsPDF: { unit: "mm", format: "a4" }, | ||
// pagebreak: { before: ".submodule-block" }, | ||
// }; | ||
|
||
// html2pdf().set(options).from(sourceElement).save(); | ||
})(); | ||
renderPrintPage(document.querySelector(".pdf_print_container"), () => { | ||
// stop loader here | ||
}); |