Load document from stream

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Conversion MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-conversion -- dnx GroupDocs.Conversion.Mcp --yes
Folders, licensing and more →

There might be a case when your file is not physically located on the disk. Instead, you have the file in the form of a stream. In this case, to avoid the overhead of saving the stream as a file on disk, GroupDocs.Conversion enables you to convert the file streams directly.

To load a file from a stream, follow these steps:

  1. Specify the method to obtain the file stream.
  2. Pass the method’s name to the Converter class constructor.

The following code snippet serves this purpose:

const fs = require('fs')
const outputPath = "LoadDocumentFromStream.pdf"

try {
  const readStream = fs.createReadStream("sample.docx")
  const stream = await groupdocs.conversion.readDataFromStream(readStream)
  const converter = new groupdocs.conversion.Converter(stream);
  const convertOptions = new groupdocs.conversion.PdfConvertOptions()

  console.log(`Source document converted successfully to ${outputPath}`)
  return converter.convert(outputPath, convertOptions)

} catch (error) {
  throw new Error(error)
}

The snippet above uses the createReadStream method. Similarly, you can use any other type of stream. Just make sure that the source stream contains any of the supported file formats.