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
Define a create_page_stream callback that accepts a page number and returns a writable binary stream (open(path, "wb"), or an io.BytesIO).
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; returns a writable streamdefcreate_page_stream(page_number):# Open a file for writing (an io.BytesIO works too)returnopen(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 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.