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 an open writable file stream; the library renders each requested page and writes the result into that stream.

Steps to generate page previews

  1. Define a create_page_stream callback that accepts a page number and returns an open binary file object (e.g. open(path, "wb")).
  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; must return a writable FILE stream
    def create_page_stream(page_number):
        # Open a file for writing — do NOT return BytesIO here
        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 (46 KB)
page-2.png (46 KB)

Download full output

Note

The callback must return a file/path stream, not BytesIO. A BytesIO object returned from the create_page_stream callback will come back empty — the .NET bridge does not write back into callback-provided in-memory streams. Always use open(path, "wb") or another operating-system file stream.

Linux / macOS: page rasterization requires libgdiplus (GDI+). Install it with apt-get install libgdiplus (Debian/Ubuntu) or brew install mono-libgdiplus (macOS/Homebrew) before calling generate_preview().

Explanation

  • Stream Callback: create_page_stream(page_number) is invoked once for every page in the requested list. It must open and return a writable binary file handle. The library writes the rendered image data into that handle.
  • 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