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
Define a create_page_stream callback that accepts a page number and returns an open binary file object (e.g. open(path, "wb")).
Create a PreviewOptions instance, passing the callback, the desired PreviewMode, and a list of page numbers to render.
Instantiate the Merger class with the source document path.
Call merger.generate_preview() with the PreviewOptions object.
fromgroupdocs.mergerimportMergerfromgroupdocs.merger.domain.optionsimportPreviewOptions,PreviewModedefgenerate_page_previews():# Callback: called once per requested page; must return a writable FILE streamdefcreate_page_stream(page_number):# Open a file for writing — do NOT return BytesIO herereturnopen(f"./page-{page_number}.png","wb")# Load the source documentwithMerger("./input.pdf")asmerger:# Configure preview: PNG format, render pages 1 and 2preview_options=PreviewOptions(create_page_stream,PreviewMode.PNG,[1,2])# Rasterize the requested pages and write them via the callback streamsmerger.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.
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.