Last active
December 26, 2022 14:23
-
-
Save dbuezas/1d52bbd4830126e50be684abed9cb740 to your computer and use it in GitHub Desktop.
loads all jpegs and makes a pdf with them
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
load = async (url) => { | |
var head= document.getElementsByTagName('head')[0]; | |
var script= document.createElement('script'); | |
script.type= 'text/javascript'; | |
script.src= url; | |
return new Promise(resolve => { | |
script.addEventListener('load', resolve); | |
head.appendChild(script); | |
}); | |
} | |
getDataUri = async (url) => { | |
var image = new Image(); | |
image.crossOrigin = "Anonymous"; | |
return new Promise(resolve => { | |
image.onload = function () { | |
var canvas = document.createElement('canvas'); | |
canvas.width = this.naturalWidth; // or 'width' if you want a special/scaled size | |
canvas.height = this.naturalHeight; // or 'height' if you want a special/scaled size | |
canvas.getContext('2d').drawImage(this, 0, 0); | |
// ... or get as Data URI | |
resolve(canvas.toDataURL('image/png')); | |
}; | |
image.src = url; | |
}); | |
} | |
begin = async () => { | |
console.log('Loading libraries') | |
await Promise.all([ | |
load('https://github.com/devongovett/blob-stream/releases/download/v0.1.3/blob-stream.js'), | |
load('https://github.com/devongovett/pdfkit/releases/download/v0.8.0/pdfkit.js'), | |
]); | |
console.log('Initing PDF') | |
const doc = new PDFDocument({ | |
size: 'a4', | |
autoFirstPage: false, | |
}); | |
const stream = doc.pipe(blobStream()); | |
stream.on('finish', () => { | |
url = stream.toBlobURL('application/pdf'); | |
window.open(url, "zhe book"); | |
console.log('Popup should have opened now.'); | |
}); | |
const promises = []; | |
const addImage = async (imgEl) => { | |
const i = promises.length; | |
await Promise.all(promises); | |
if (imgEl && imgEl.src) { | |
console.log('adding page', i); | |
const uri = await getDataUri(imgEl.src); | |
doc.addPage().image(uri, 0, 0, { | |
fit: [600, 780], | |
align: 'center', | |
valign: 'center' | |
}); | |
} else { | |
doc.addPage(); | |
console.log('page empty', i) | |
} | |
}; | |
let pageEls = document.querySelectorAll('.outer_page'); | |
for (pageEl of pageEls) { | |
const i = promises.length; | |
console.log('loading page', i); | |
pageEl.scrollIntoView(); | |
let imgEl; | |
const startT = Date.now(); | |
while (!imgEl && (Date.now() - startT < 5000)) { | |
await new Promise(r => setTimeout(r, 10)); | |
imgEl = pageEl.querySelector('.absimg'); | |
} | |
promises.push(addImage(imgEl)); | |
} | |
console.log('waiting for page', promises.length); | |
await Promise.all(promises); | |
console.log('Making PDF'); | |
doc.end(); | |
} | |
begin(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment