Created
April 19, 2023 10:51
-
-
Save WilliamStam/79142ed546959d344b283349b5514416 to your computer and use it in GitHub Desktop.
print concated pdfs
This file contains hidden or 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
<button | |
type="button" | |
onclick="printit(['file1.pdf','file2.pdf'])" | |
> | |
Print pdf | |
</button > | |
<div id='pdf-viewer'></div > | |
<script | |
src="https://cdnjs.cloudflare.com/ajax/libs/print-js/1.6.0/print.js" | |
integrity="sha512-/fgTphwXa3lqAhN+I8gG8AvuaTErm1YxpUjbdCvwfTMyv8UZnFyId7ft5736xQ6CyQN4Nzr21lBuWWA9RTCXCw==" | |
crossorigin="anonymous" | |
referrerpolicy="no-referrer" | |
></script > | |
<script | |
src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.5.141/pdf.min.js" | |
integrity="sha512-BagCUdQjQ2Ncd42n5GGuXQn1qwkHL2jCSkxN5+ot9076d5wAI8bcciSooQaI3OG3YLj6L97dKAFaRvhSXVO0/Q==" | |
crossorigin="anonymous" | |
referrerpolicy="no-referrer" | |
></script > | |
<script | |
src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.5.141/pdf.worker.entry.min.js" | |
integrity="sha512-NJEHr6hlBM4MkVxJu+7FBk+pn7r+KD8rh+50DPglV/8T8I9ETqHJH0bO7NRPHaPszzYTxBWQztDfL6iJV6CQTw==" | |
crossorigin="anonymous" | |
referrerpolicy="no-referrer" | |
></script > | |
<script > | |
// credit to https://medium.com/@vicky.sharma8127344/print-single-or-multiple-files-with-print-js-and-pdf-js-ab9c8347a309 | |
const pages = [] | |
var scale = 1; | |
const printit = (pdfs) => { | |
getMultiplePngImagesWithImgTag(pdfs).then((multiplePngImagesWithImgTag) => { | |
// Now we have all converted base64 PNGs in image tag and we are ready to render them as html content to print as one single file with Print.js | |
printJS({ | |
printable: multiplePngImagesWithImgTag, | |
type: 'image', | |
base64: true, | |
}); | |
}); | |
} | |
const getMultiplePngImagesWithImgTag = (pdfs) => { | |
return new Promise((resolve, reject) => { | |
var docPromiseList = []; | |
pdfs.forEach((doc, index) => { | |
docPromiseList.push(convertBase64PdfToBase64Png(doc)); | |
}); | |
Promise.all(docPromiseList) | |
.then((res) => { | |
if (res && res.length) { | |
let doc = []; | |
// These two loops are getting base64 PNG where | |
// first loop is for each document and second loop is for each page of document | |
for (let i = 0; i < res.length; i++) { | |
for (let j = 0; j < res[i].length; j++) { | |
// get all docs in base64PNG in order to print with Print.js with docType as rawHTML | |
// doc = doc.concat( | |
// `<div style="text-align:center"><img src="${res[i][j]}"/ style="object-fit:cover;"></div><br/>` | |
// ); | |
doc.push(res[i][j]) | |
} | |
// doc = doc.concat(`<div style="page-break-before: always;"></div>`); | |
} | |
resolve(doc); | |
} | |
}) | |
.catch((err) => { | |
reject(err); | |
}); | |
}); | |
}; | |
const convertBase64PdfToBase64Png = (pdf_file) => { | |
return new Promise((resolve, reject) => { | |
pdfjsLib | |
.getDocument(pdf_file) | |
.promise.then((pdf) => { | |
let totalPages = pdf.numPages; | |
let totalPagePromiseList = []; | |
for (let currentPage = 1; currentPage <= totalPages; currentPage++) { | |
totalPagePromiseList.push( | |
convertEachBase64PdfPageToBase64PngPage(pdf, currentPage) | |
); | |
} | |
Promise.all(totalPagePromiseList) | |
.then((newRes) => { | |
resolve(newRes); | |
}) | |
.catch((newError) => { | |
reject(newError); | |
}); | |
}) | |
.catch((error) => { | |
// PDF loading error | |
reject(error); | |
}); | |
}); | |
}; | |
const convertEachBase64PdfPageToBase64PngPage = (pdf, currentPage) => { | |
return new Promise((resolve, reject) => { | |
pdf.getPage(currentPage).then((page) => { | |
var scale = 1.5; | |
var viewport = page.getViewport({scale: scale}); | |
// Prepare canvas using PDF page dimensions | |
var canvas = document.createElement('canvas'); | |
var context = canvas.getContext('2d'); | |
canvas.height = viewport.height; | |
canvas.width = viewport.width; | |
// Render PDF page into canvas context | |
var renderContext = { | |
canvasContext: context, | |
viewport: viewport, | |
}; | |
page | |
.render(renderContext) | |
.promise.then(() => { | |
resolve(canvas.toDataURL()); // Returns the content of the current canvas as an image | |
}) | |
.catch((error) => { | |
reject(error); | |
}); | |
}); | |
}); | |
}; | |
</script > |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment