A tiny wrapper around pdf.js to convert PDFs to dataURLs.
npm i -S pdf-to-dataURL
pdfToDataURL is an async function (due to the async nature of pdf.js), that
accepts a Blob of the PDF (e.g. from an <input type='file' />
element), the
width of the viewport the dataURL should fit in (for proper sizing), and a
callback that will be called after each page of the PDF is converted with a
page object.
The page object has four properties:
width
:number
the width of the page's dataURLheight
:number
the height of the page's dataURLsrc
:dataURL
the dataURL of the pageindex
:number
the index of the page -- as pdf.js is async, the callback may be called not in the correct page order. Use this property if the order of the pages matters to you to properly place into an array. Useful with rendering libraries like React, where you can easily control the ordering of elements in the DOM.
import pdfToDataURL from 'pdf-to-dataURL'
const viewportWidth = 800
let initialNumChildren = document.body.children.length
const cb = function (page) {
let img = document.createElement('img')
img.width = page.width
img.height = page.height
img.src = page.src // the dataURL is here
document.body.appendChild(img)
}
pdfToDataURL(pdfBlob, viewportWidth, cb)