Skip to content

Instantly share code, notes, and snippets.

@garyhtou
Last active December 7, 2024 11:18
Show Gist options
  • Save garyhtou/6e8e7d3697a55d516edf260ac1c2d270 to your computer and use it in GitHub Desktop.
Save garyhtou/6e8e7d3697a55d516edf260ac1c2d270 to your computer and use it in GitHub Desktop.
Download pages as PNG from Apryse's pdftron WebViewer
/*
1. Click "Digital Textbook"
2. Open in new tab
3. Select "Page by page"
5. Select "Single page"
6. Select "Fit to page"
7. Go to page 1
8. Open Devtools
9. Use Devtools' Select tool to click on the page.
10. Switch to Devtool's console tab and run the script
11. Using MacOS's Finder, select all downloaded PNGs. To get the pages in the
right order, you may need to: Sort files by Date Added asc. Select Page 1
image, shift+click and select last page image file.
12. Right click and use MacOS's "Create PDF" to combine all the PNGs into a
single PDF.
13. Delete all the PNGs
You may need to allow website to download multiple files
*/
function downloadURI(uri, name) {
var link = document.createElement("a");
link.download = name;
link.href = uri;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
delete link;
}
function savePage(pageNumber) {
const canvas = document.querySelector(`#pageContainer${pageNumber} > canvas.hacc`)
const dataUrl = canvas.toDataURL("image/png")
downloadURI(dataUrl, `page${pageNumber}`)
}
async function nextPage() {
const button = document.querySelector("button[aria-label='Next page']")
if (button.disabled) {
// Can't go to next page (at end?)
return false
}
button.click()
await sleep(200)
return true
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function run() {
var page = 1
do {
savePage(page++)
} while (await nextPage());
}
await run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment