Last active
February 26, 2022 19:44
-
-
Save michaeljscript/5ad80b221ad108fc0faa0e3c51d1f706 to your computer and use it in GitHub Desktop.
Images to PDF
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
document.write('<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js" onload="callback()"></script>'); | |
function getImageFromUrl(url) { | |
return new Promise((resolve, reject) => { | |
const img = new Image(); | |
img.onError = (err) => reject(err); | |
img.onload = () => resolve(img); | |
img.src = url; | |
}); | |
} | |
function createPDF(images) { | |
const doc = new jspdf.jsPDF(); | |
images.forEach((img, index) => { | |
index !== 0 && doc.addPage(); | |
doc.addImage(img, 'JPEG', 0, 0, 210, 210); | |
}); | |
return doc.output('bloburl'); | |
} | |
async function callback() { | |
const imageUrl = 'https://media.istockphoto.com/photos/software-developer-programming-code-on-computer-picture-id494290794?k=20&m=494290794&s=612x612&w=0&h=eQ6r8iFfV704JkWPl9ZCngi-TN-vZ5pOtZBpfFcL6P8='; | |
const blobUrl = createPDF([await getImageFromUrl(imageUrl)]); | |
window.open(blobUrl); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment