Convert PDF to Image (JPEG, PNG, etc)

Hi,

I want to convert PDF to Image. I am trying to do it in plain javascript without using any server. is it possible?

I tried using PDFNet.PDFDraw.exportBuffer, but it says PDFNet.PDFDraw.exportBuffer is not a function.

Any Help would be appreciated.

Regards,

Hi,

Yes, you can definitely do this without using a server. It’ll probably be easier using the Document APIs instead of PDFNet APIs, and faster too because you don’t need to load the “full API”.

If you’re already using a viewer you can use code like this https://www.pdftron.com/documentation/web/guides/create/thumbnail/#create-a-high-resolution-page-image-with-annotations

Note that from a browser canvas object you can use the toDataURL or toBlob functions to get the image data
https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL
https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob

If you want to do it and you don’t have a WebViewer instance already you can first create a Document object in memory using CoreControls.createDocument https://www.pdftron.com/documentation/web/guides/lower-level-doc-create-pdf/

That doc object is the exact same as what you get from docViewer.getDocument() so then you can call doc.loadCanvasAsync to get the rendered page.

Let me know if you have any other questions about this.

Matt Parizeau
Software Developer
PDFTron Systems Inc.

Thanks. It was easy!