To load a source file from a stream, you can use the Converter class constructor in GroupDocs.Conversion. The API provides several overloads to accommodate different settings and options:
Each constructor requires the stream parameter, which represents the input stream for the source file. This allows you to load documents directly from memory or other input sources. Be mindful that if the stream is not properly initialized or if it reaches the end unexpectedly, an exception will be raised.
Tip
GroupDocs.Conversion will access the stream only when an action (e.g., conversion) is performed using the Converter class instance.
Example 1: Load file from stream with load options
The following Python example demonstrates loading a file from a stream and converting it to PDF. In this example we get and specify a default load options for DOCX file conversion:
importosfromgroupdocs.conversionimportConverterfromgroupdocs.conversion.options.convertimportPdfConvertOptionsdefget_file_stream(file_path:str):file_stream=open(file_path,"rb")returnfile_streamdefget_load_options(file_path:str):file_extension=os.path.splitext(file_path)[1]possible_conversions=Converter.get_possible_conversions_by_extension(file_extension)load_options=possible_conversions.load_optionsreturnload_optionsdefload_file_from_stream():# Set file pathfile_path="./annual-review.docx"# Retrieve file stream and load optionsfile_stream=get_file_stream(file_path)load_options=get_load_options(file_path)# Specify source file stream and load optionsconverter=Converter(file_stream,load_options)# Specify output file location and convert optionsoutput_path="./annual-review.pdf"pdf_options=PdfConvertOptions()# Convert and save to output pathconverter.convert(output_path,pdf_options)if__name__=="__main__":load_file_from_stream()
annual-review.docx is sample file used in this example. Click here to download it.
annual-review.pdf is converted PDF document. Click here to download it.
Explanation
Retrieve File Stream: The method get_file_stream retrieves a file stream from a storage.
Retrieve Load Options: The method get_load_options retrieves a default load options based on file extension.
Load Source File: The Converter class is instantiated with the source documents stream and load options.
Conversion Options: An instance of PdfConvertOptions is created to define the settings for PDF conversion.
Execute Conversion: The convert method is used to convert the document and save it to the specified output path (“annual-review.pdf”).
Example 2: Load file from stream and detect file type automatically
When loading a document from stream GroupDocs.Conversion can detect the file type automatically in most cases.
Warning
Depending on the file type and size, automatic file type detection consumes additional resources such as memory and CPU time. Therefore, we recommend specifying the file type whenever you know the source file type. See Example 1: Load file from stream with load options that demonstrates how to specify load options.
The following Python example demonstrates loading a file from a stream and converting it to PDF:
fromgroupdocs.conversionimportConverterfromgroupdocs.conversion.options.convertimportPdfConvertOptionsdefget_file_stream(file_path:str):file_stream=open(file_path,"rb")returnfile_streamdefdetect_file_type_automatically():# Retrieve file streamfile_path="./annual-review.docx"file_stream=get_file_stream(file_path)# Specify source file locationconverter=Converter(file_stream)# Specify output file location and convert optionsoutput_path="./annual-review.pdf"pdf_options=PdfConvertOptions()# Convert and save to output pathconverter.convert(output_path,pdf_options)if__name__=="__main__":detect_file_type_automatically()
annual-review.docx is sample file used in this example. Click here to download it.
annual-review.pdf is converted PDF document. Click here to download it.
Explanation
Retrieve File Stream: The method get_file_stream retrieves a file stream from storage.
Load Source File: The Converter class is instantiated with the source documents stream.
Conversion Options: An instance of PdfConvertOptions is created to define the settings for PDF conversion.
Execute Conversion: The convert method is used to detect file type of the source document stream, convert the document and save it to the specified output path (“annual-review.pdf”).