Generate Page Previews

GroupDocs.Merger for Python via .NET can rasterize document pages to image files — PNG, JPEG, or BMP — using the generate_preview() method. You supply a callback function that receives the 1-based page number and returns a writable stream, a file opened for writing or an io.BytesIO; the library renders each requested page, writes the result into that stream and closes it.

Steps to generate page previews

  1. Define a create_page_stream callback that accepts a page number and returns a writable binary stream (open(path, "wb"), or an io.BytesIO).
  2. Create a PreviewOptions instance, passing the callback, the desired PreviewMode, and a list of page numbers to render.
  3. Instantiate the Merger class with the source document path.
  4. Call merger.generate_preview() with the PreviewOptions object.
from groupdocs.merger import Merger
from groupdocs.merger.domain.options import PreviewOptions, PreviewMode

def generate_page_previews():
    # Callback: called once per requested page; returns a writable stream
    def create_page_stream(page_number):
        # Open a file for writing (an io.BytesIO works too)
        return open(f"./page-{page_number}.png", "wb")

    # Load the source document
    with Merger("./input.pdf") as merger:
        # Configure preview: PNG format, render pages 1 and 2
        preview_options = PreviewOptions(create_page_stream, PreviewMode.PNG, [1, 2])
        # Rasterize the requested pages and write them via the callback streams
        merger.generate_preview(preview_options)

if __name__ == "__main__":
    generate_page_previews()
input.pdf is a sample file used in this example. Click here to download it.
page-1.png (1897 KB)
page-2.png (209 KB)

Download full output

The callback can return a file or an io.BytesIO. The library closes each page’s stream once the page is written: a file is then complete on disk, and a BytesIO holds the image. To handle each page as soon as it is ready, pass a release callback too, PreviewOptions(create_page_stream, release_page_stream, PreviewMode.PNG, [1, 2]): it is called as release_page_stream(page_number, stream) with the object create_page_stream returned.

Explanation

  • Stream Callback: create_page_stream(page_number) is invoked once for every page in the requested list. It returns a writable binary stream, a file handle or an io.BytesIO. The library writes the rendered image data into that stream and closes it.
  • PreviewOptions: The constructor accepts three arguments: the stream callback, a PreviewMode enum value (PNG, JPEG, or BMP), and a list of 1-based page numbers to render. Omit the page list to render all pages.
  • Load Document: The Merger context manager opens the source document and releases all resources when the with block exits.
  • Generate: merger.generate_preview(preview_options) rasterizes each requested page and writes the image into the corresponding stream returned by the callback.

After running the example, you will find page-1.png and page-2.png in the working directory.

Refer to the GroupDocs.Merger API Reference for full details on PreviewOptions and PreviewMode.

See also