Created
May 1, 2018 18:10
-
-
Save rossta/ba13b32e413b305f163139398ac2ba72 to your computer and use it in GitHub Desktop.
Example using PDF.js
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
import range from 'lodash/range' | |
import('pdfjs/dist').then(pdfjs => { | |
pdfjs | |
.getDocument('wibble.pdf') | |
.then(pdf => { | |
const pagePromises = range(1, pdf.numPages).map(number => pdf.getPage(number)) | |
return Promise.all(pagePromises) | |
}) | |
.then( | |
pages => { | |
const scale = 1.5 | |
const canvases = pages.map(page => { | |
const viewport = page.getViewport(scale) | |
// Prepare canvas using PDF page dimensions | |
const canvas = document.createElement('canvas') | |
canvas.height = viewport.height | |
canvas.width = viewport.width | |
// Render PDF page into canvas context | |
const canvasContext = canvas.getContext('2d') | |
const renderContext = { canvasContext, viewport } | |
page.render(renderContext).then(() => console.log('Page rendered')) | |
return canvas | |
}) | |
canvases.forEach(canvas => document.body.appendChild(canvas)) | |
}, | |
error => console.log('Error', error), | |
) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment