GroupDocs.Assembly allows you to use a string as an input template and to get the output as a string instead of a document file. This is convenient when templates are stored in a database or generated by code, and when the result is consumed by the application directly.
DocumentAssembler.assembleDocument has overloads that take a Java InputStream and a Java OutputStream. In Node.js, create them through the java module (a dependency of the package), or use the StreamBuffer helper exported by the package.
Using Java byte-array streams
Follow these steps:
Initialize DocumentAssembler
Define the template string, for example "<<[yourValue]>>"
Convert the string to a Java byte array and wrap it into a java.io.ByteArrayInputStream
Create a java.io.ByteArrayOutputStream for the result and call DocumentAssembler.assembleDocument()
Convert the bytes of the output stream back to a string with Buffer
constgroupdocs=require('@groupdocs/groupdocs.assembly');constjava=require('java');constassembler=newgroupdocs.DocumentAssembler();// Template as a string
constsourceString="<<[yourValue]>>";// Wrap the string bytes into a Java input stream
constsourceBytes=java.newArray('byte',Array.from(Buffer.from(sourceString,'utf8')));constsourceStream=java.newInstanceSync('java.io.ByteArrayInputStream',sourceBytes);// Collect the result in a Java output stream
consttargetStream=java.newInstanceSync('java.io.ByteArrayOutputStream');assembler.assembleDocument(sourceStream,targetStream,newgroupdocs.DataSourceInfo("Hello, World!","yourValue"));// Convert the result bytes back to a string
consttargetString=Buffer.from(targetStream.toByteArray()).toString('utf8');console.log(targetString);// Hello, World!
process.exit(0);
Using the StreamBuffer helper
StreamBuffer is a java.io.ByteArrayOutputStream with two extra methods: write(chunk) accepts a Node.js Buffer, and toInputStream() returns a java.io.ByteArrayInputStream over the written bytes. It can therefore serve both as the template source and as the output target. The example also sets the input format explicitly with LoadSaveOptions and passes two data sources:
constgroupdocs=require('@groupdocs/groupdocs.assembly');constassembler=newgroupdocs.DocumentAssembler();// Template source
constsource=newgroupdocs.StreamBuffer();source.write(Buffer.from('Dear <<[name]>>, your total is <<[total]:"0.00">>.','utf8'));// Output target
consttarget=newgroupdocs.StreamBuffer();assembler.assembleDocument(source.toInputStream(),target,newgroupdocs.LoadSaveOptions(groupdocs.FileFormat.TEXT),newgroupdocs.DataSourceInfo("John","name"),newgroupdocs.DataSourceInfo(12.5,"total"));console.log(target.toString("UTF-8"));// Dear John, your total is 12.50.
process.exit(0);
Note
The number format in the output follows the current culture (the JVM default locale). For example, with a Russian locale the result is 12,50.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
On this page
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.