GroupDocs.Viewer writes its output to files by default, but every output can go to memory instead. Instead of a file name, pass a function that returns a writable stream, usually an io.BytesIO. GroupDocs.Viewer calls it for each page (or once, for a PDF file) and writes the output into the stream it gets. Each stream is complete by the time view() returns. This suits a web service that sends pages straight to the client, and storage that is not a local disk.
the page function for each page, create_resource_stream(page_number, resource_name) for each font, style sheet or image, and create_resource_url(page_number, resource_name) for the address the page uses for it
One PDF file
PdfViewOptions(create_file_stream)
create_file_stream() once
A function returns a writable stream: an empty io.BytesIO, or a file opened for writing (open(path, "wb")), which GroupDocs.Viewer writes through its own handle and closes for you.
Render page images to memory
The following example renders every page of a document to a PNG image in memory:
importiofromgroupdocs.viewerimportViewerfromgroupdocs.viewer.optionsimportPngViewOptionsdefrender_png_pages_to_memory():pages={}defcreate_page_stream(page_number):# Called once per page: return the stream the page is written topages[page_number]=io.BytesIO()returnpages[page_number]withViewer("sample.docx")asviewer:viewer.view(PngViewOptions(create_page_stream))# Every buffer holds its page once view() returnsforpage_numberinsorted(pages):print("page %d: %d bytes"%(page_number,len(pages[page_number].getvalue())))if__name__=="__main__":render_png_pages_to_memory()
sample.docx is the sample file used in this example. Click here to download it.
With external resources, the fonts, style sheets and images of a page go to streams of their own, and a third function gives the address each page uses for a resource. Here the addresses are those of a web service that would serve the resources from the resources dictionary:
importiofromgroupdocs.viewerimportViewerfromgroupdocs.viewer.optionsimportHtmlViewOptionsdefrender_html_to_memory():pages={}resources={}defcreate_page_stream(page_number):pages[page_number]=io.BytesIO()returnpages[page_number]defcreate_resource_stream(page_number,resource_name):resources[(page_number,resource_name)]=io.BytesIO()returnresources[(page_number,resource_name)]defcreate_resource_url(page_number,resource_name):# The address the HTML page uses for this resourcereturn"/documents/sample/pages/%d/%s"%(page_number,resource_name)withViewer("sample.docx")asviewer:viewer.view(HtmlViewOptions.for_external_resources(create_page_stream,create_resource_stream,create_resource_url))print("%d pages, %d resources"%(len(pages),len(resources)))html=pages[1].getvalue().decode("utf-8")print("page 1 links its resources:","/documents/sample/pages/1/"inhtml)if__name__=="__main__":render_html_to_memory()
sample.docx is the sample file used in this example. Click here to download it.
5 pages, 30 resources
page 1 links its resources: True
Each create function has an optional release function: release_page_stream(page_number, stream), release_file_stream(stream) and release_resource_stream(page_number, resource_name, stream). GroupDocs.Viewer calls it once it has finished writing that output, with the object your create function returned, already complete: the place to upload a page and drop the buffer. Pass it after the create functions: PngViewOptions(create_page_stream, release_page_stream), PdfViewOptions(create_file_stream, release_file_stream), HtmlViewOptions.for_embedded_resources(create_page_stream, release_page_stream), and for external resources HtmlViewOptions.for_external_resources(create_page_stream, create_resource_stream, create_resource_url, release_page_stream, release_resource_stream).
Notes
Renders in parallel threads keep their own outputs: each view() fills only the streams its own functions returned.
A tempfile.SpooledTemporaryFile returned by a create function stays empty. Return an io.BytesIO or a file opened with open(path, "wb") instead.
An io.BytesIO holds at most 2 GB. For larger output, write to files.