Generate a PDF page thumbnail using JavaScript

To create a PDF page thumbnail in multiple image formats.

JavaScript

1async function main() {
2 const doc = await PDFNet.PDFDoc.createFromURL(filename);
3
4 // set the output resolution
5 const pdfdraw = await PDFNet.PDFDraw.create(92);
6
7 // Rasterize the first page in the document and save the result as PNG.
8 const pg = await doc.getPage(1);
9 const buf = await pdfdraw.exportStream(pg, 'PNG');
10
11 //optionally save the blob to a file or upload to a server
12 const blob = new Blob([buf], { type: 'application/pdf' });
13}
14PDFNet.runWithCleanup(main);

Convert PDF to image (JPG, PNG, BMP, TIFF)
Full code sample illustrates how to use the built-in rasterizer in order to render PDF images on the fly and how to save resulting images in PNG and JPEG format.

More image format options:

  • "RAW" : RAW format.There are four possibilities:
    • e_rgba - if transparent and color page;
    • e_gray_alpha - if transparent and gray page;
    • e_rgb - if opaque and color page;
    • e_gray - if opaque and gray page. NOTE that if page is set to be transparent (SetPageTransparent), the output color channels are already multiplied by the alpha channel.
  • "BMP" : Bitmap image format (BMP)
  • "JPEG" : Joint Photographic Experts Group (JPEG) image format
  • "PNG" : 24-bit W3C Portable Network Graphics (PNG) image format
  • "PNG8" : 8-bit, palettized PNG format. The exported file size should be smaller than the one generated using "PNG", possibly at the expense of some image quality.
  • "TIFF" : Tag Image File Format (TIFF) image format.
  • "TIFF8" : Tag Image File Format (TIFF) image format (with 8-bit palete).

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales