Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example using PDF.js
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