Skip to content
How to export PDF to image file

How to export PDF to image file

The render package rasterizes a page. ImageDevice renders one page at a time, and RenderToPath writes the result straight to a file.

import "github.com/unidoc/unipdf/v5/render"

device := render.NewImageDevice()
device.OutputWidth = 2048

if err := device.RenderToPath(page, "page-1.png"); err != nil {
    return err
}

The output format comes from the file extension. Only .png, .jpg and .jpeg are recognized; anything else returns an error rather than falling back to a default. JPEG is written at quality 100. Render returns an image.Image instead if you want to resize, crop or encode it yourself.

OutputWidth is the width of the output in pixels, and the height follows from the page. Leave it at zero and the page renders one pixel per point, which is 72 DPI and usually too coarse to read. For a Letter page, 1700 gives roughly 200 DPI.

A page with a crop box renders cropped to it, and OutputWidth is measured against the crop box rather than the media box. Page rotation is applied. Annotations are flattened into the image by default; RenderWithOpts(page, true) skips that and renders page content only.

Rendering is per page, so converting a document means looping over GetPage. The PDF to image guide has the full program. Pages containing JPEG2000 images are the one case that needs extra setup, since UniPDF has no JPX decoder of its own; custom encoding shows how to register one.

Last updated on