Convert PDF to image in JavaScript

Convert PDF to PNG, JPG, BMP, TIFF using WebViewer SDK in the browser without server-side dependencies. (You can also check out our online PDF to Image demo now.)

Using WebViewer

You can rasterize PDF pages to an image and then save the resulting buffer to your database, file storage, or simply download it. The sample below demonstrates how to generate images with FullAPI enabled, you can also generate images using a lean build of WebViewer which should result in better performance.

JavaScript (v8.0+)

1Webviewer({
2 fullAPI: true,
3}, viewerRef).then((instance) => {
4 const { Core } = instance;
5 const { documentViewer, PDFNet } = Core;
6
7 await PDFNet.initialize();
8
9 documentViewer.addEventListener('documentLoaded', async () => {
10 const doc = await documentViewer.getDocument().getPDFDoc();
11 const pdfdraw = await PDFNet.PDFDraw.create(92);
12 const itr = await doc.getPageIterator(1);
13 const currPage = await itr.current();
14
15 const pngBuffer = await pdfdraw.exportBuffer(currPage, 'PNG');
16 const tifBuffer = await pdfdraw.exportBuffer(currPage, 'TIFF');
17 });
18});

Without WebViewer UI

This code sample demonstrates how to convert PDF to image without initializing WebViewer UI. The sample below demonstrates how to generate images with FullAPI enabled, you can also generate images using a lean build of WebViewer which should result in better performance.

JavaScript (v8.0+)

1<html>
2 <body>
3 <script src="../lib/core/webviewer-core.min.js"></script>
4 <script>
5 (async function() {
6 Core.setWorkerPath('../lib/core');
7 Core.enableFullPDF();
8 const licenseKey = 'Insert commercial license key here after purchase';
9 await PDFNet.initialize();
10
11 const doc = await PDFNet.PDFDoc.createFromURL('url_to_pdf');
12 const pdfdraw = await PDFNet.PDFDraw.create(92);
13 const itr = await doc.getPageIterator(1);
14 const currPage = await itr.current();
15 const pngBuffer = await pdfdraw.exportBuffer(currPage, 'PNG');
16 const tifBuffer = await pdfdraw.exportBuffer(currPage, 'TIFF');
17 })()
18 </script>
19 </body>
20</html>

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales