Load Document from Stream

GroupDocs.Merger for Python via .NET accepts any readable binary stream as the source document. This lets you load documents from memory, network responses, cloud storage SDKs, or any other source that exposes a file-like object — without first writing the content to disk.

The Merger class provides the following stream-based constructor overloads:

  • Merger(stream) — load from a binary readable stream with default settings.
  • Merger(stream, load_options) — load from a stream and supply additional loading options (e.g. a password).
  • Merger(stream, load_options, settings) — load from a stream with load options and custom MergerSettings.
Tip
GroupDocs.Merger accesses the stream only when an action (e.g. join(), save()) is performed on the Merger instance. Keep the stream open until the with block exits.

The following example demonstrates loading two PDF documents from binary file handles, merging them, and saving the result to disk:

from groupdocs.merger import Merger

def load_from_stream():
    # Open the base document as a binary stream
    with open("./input.pdf", "rb") as f:
        # Load the document directly from the stream
        with Merger(f) as merger:
            # Append a second document by file path
            merger.join("./input2.pdf")
            # Save the merged result to disk
            merger.save("./output.pdf")

if __name__ == "__main__":
    load_from_stream()

input.pdf is a sample file used in this example. Click here to download it.

input2.pdf is a sample file used in this example. Click here to download it.

Binary file (PDF, 252 KB)

Download full output

Explanation

  • Open Stream: open("./input.pdf", "rb") opens the base document as a binary readable file object. Any binary stream (e.g. io.BytesIO, an S3 response body, a requests response with raw) is accepted.
  • Load from Stream: Merger(f) initialises the instance using the supplied stream. The stream must remain open until all operations are complete.
  • Join Additional Document: merger.join("./input2.pdf") appends a second document specified by file path. You can also pass a stream here.
  • Save Result: merger.save("./output.pdf") writes the combined document to disk. You can alternatively pass a writable stream to save().
  • Resource Cleanup: The nested with blocks ensure the Merger instance is disposed first, then the file handle is closed.

Refer to the GroupDocs.Merger API Reference for full details on stream-based constructor overloads.

See also