XML to PDF in groovy

I am trying to Convert XML to PDF using groovy through inputstream and aspose.jar following below:

below is my code:

def document = new Document("...dir/template.docx");
PdfSaveOptions saveOptions = new PdfSaveOptions();
ByteArrayOutputStream out = new ByteArrayOutputStream();
document.Save(out, saveOptions);

getting below error:

No signature of method: com.aspose.words.Document.Save() is applicable for argument types: (java.io.ByteArrayOutputStream, com.aspose.words.PdfSaveOptions) values: [, com.aspose.words.PdfSaveOptions@6194ebd6]

Any help would be really helpful and appreciated.

Thanks,

@Smita_Ahuja,
looks like your post is about Aspose.Words.
I’ll move your post to the appropriate category.

@Smita_Ahuja,

Could you please attach your problematic input document here for testing? We will check the issue and provide you more information.

Sure, please find attached templatetemplate.docx (11.8 KB)

Basically I am trying to convert below .Net code to java(groovy):

XmlDocument xmlDoc = JsonConvert.DeserializeXmlNode(applicationJson, XmlRootElementName);
XmlNodeReader xmlReader = new XmlNodeReader(xmlDoc);

DataSet ds = new DataSet();
ds.ReadXml(xmlReader, XmlReadMode.Auto);

string template = new Uri(Path.Combine(Path.GetDirectoryName(typeof(PdfDocumentGenerator).Assembly.CodeBase), TemplatesFolder, templateName)).AbsolutePath;
_logger.LogInformation(string.Format(@"Path to load RoT template : {0}", template));

if (!File.Exists(template))
{
    throw new Exception(string.Format(@"Template {0} does not exist.", templateName));
}

var document = new Aspose.Words.Document(template);

document.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs
        | MailMergeCleanupOptions.RemoveContainingFields
        | MailMergeCleanupOptions.RemoveUnusedRegions
        | MailMergeCleanupOptions.RemoveUnusedFields;

document.MailMerge.ExecuteWithRegions(ds);

PdfSaveOptions saveOptions = new PdfSaveOptions
{
    Compliance = PdfCompliance.PdfA1a
};

using (Stream stream = new MemoryStream())
{
    document.Save(stream, saveOptions);
    int size = (int)stream.Length;
    byte[] resultData = new byte[size];
    stream.Seek(0, SeekOrigin.Begin);
    stream.Read(resultData, 0, size);
    return Convert.ToBase64String(resultData);
}

Something like:

for (int i = 0; i < dataContext.getDataCount(); i++)
{
    InputStream is = dataContext.getStream(i);
    Properties props = dataContext.getProperties(i);

    SAXParser saxParser = factory.newSAXParser();
    XMLReader xmlReader = saxParser.getXMLReader();
    InputSource source = new InputSource(is);
    xmlReader.parse(source);
    DataSet customersDs = new DataSet();
    customersDs.ReadXml(xmlReader);
}

But getting:

No signature of method: com.aspose.words.net.System.Data.DataSet.ReadXml() is applicable for argument types: (com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser) values: [com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser@10331559]

Sample xml is:

<?xml version="1.0" encoding="utf-8"?>
<customers>
	<customer Name="John Ben Jan" ID="1" Domain="History" City="Boston"/>
	<customer Name="Lisa Lane" ID="2" Domain="Chemistry" City="LA"/>
	<customer Name="Dagomir Zits" ID="3" Domain="Heraldry" City="Milwaukee"/>
	<customer Name="Sara Careira Santy" ID="4" Domain="IT" City="Miami"/>
</customers>

I also tried with DOM DocumentBuilderFactory also, below is my code:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

for (int i = 0; i < dataContext.getDataCount(); i++)
{
    InputStream is = dataContext.getStream(i);
    Properties props = dataContext.getProperties(i);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(is);
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    logger.info("XML IN String format is: \n" + writer.toString());
    DataSet ds = new DataSet();
    ds.ReadXml(writer.toString());
    dataContext.storeStream(is, props);
}

but getting below error:

signature of method: com.aspose.words.net.System.Data.DataSet.ReadXml() is applicable for argument types: (java.lang.String) values: [

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<customers>
	 <customer Name="John Ben Jan" ID="1" Domain="History" City="Boston"/>
 	<customer Name="Lisa Lane" ID="2" Domain="Chemistry" City="LA"/>
	 <customer Name="Dagomir Zits" ID="3" Domain="Heraldry" City="Milwaukee"/>
 	<customer Name="Sara Careira Santy" ID="4" Domain="IT" City="Miami"/>
</customers>

@Smita_Ahuja You can simply read XML from stream into DataSet. Please see the following code:

// You can use other type of input stream.
InputStream xmlIs = new FileInputStream("C:\\Temp\\in.xml");
// Read data set from stream
DataSet ds = new DataSet();
ds.readXml(xmlIs);

DataSet.readXml method accepts as parameter either path to XML file or input stream with XML data.

Hi @alexey.noskov Thanks for your reply, I am trying from Dell Boomi grovy script
If I pass inputstream , I am getting:

for (int i = 0; i < dataContext.getDataCount(); i++)
{
    InputStream is = dataContext.getStream(i);
    Properties props = dataContext.getProperties(i);
    DataSet ds = new DataSet();
    ds.ReadXml(is);
}
Caused by: com.boomi.process.ProcessException: No signature of method: com.aspose.words.net.System.Data.DataSet.ReadXml() is applicable for argument types: (com.boomi.util.io.FastByteArrayInputStream) values: [com.boomi.util.io.FastByteArrayInputStream@9ebe7ac]

@Smita_Ahuja Could you please try with java.io.ByteArrayInputStream:

java.io.ByteArrayInputStream byteStream = new java.io.ByteArrayInputStream(bytes);
DataSet ds = new DataSet();
ds.readXml(byteStream);

Now getting:

Caused by: com.boomi.process.ProcessException: No signature of method: com.aspose.words.net.System.Data.DataSet.ReadXml() is applicable for argument types: (java.io.ByteArrayInputStream) values: [java.io.ByteArrayInputStream@28a1d103]
Possible solutions: readXml(com.aspose.words.internal.zzYzy), readXml(com.aspose.words.internal.zzZoc), readXml(java.io.InputStream), readXml(java.lang.String), readXml(java.io.InputStream, com.aspose.words.net.System.Data.XmlReadMode), readXml(java.lang.String, com.aspose.words.net.System.Data.XmlReadMode) (in groovy2 script)
	at com.boomi.process.graph.ProcessShape.failProcess(ProcessShape.java:784)
	at com.boomi.process.graph.ProcessShape.notifyDocumentStatus(ProcessShape.java:642)
	at com.boomi.document.util.DocumentUtil.failProcess(DocumentUtil.java:125)
	at com.boomi.document.impl.OutboundDocumentGroupImpl.failRemainingDocuments(OutboundDocumentGroupImpl.java:138)
	at com.boomi.document.handler.DocumentMultiProcessExecutor.invoke(DocumentMultiProcessExecutor.java:36)
	at com.boomi.document.util.DataProcessExecutor.executeStep(DataProcessExecutor.java:111)
	at com.boomi.document.util.DataProcessExecutor.executeDataProcess(DataProcessExecutor.java:65)
	at com.boomi.process.shape.DataProcessShape.execute(DataProcessShape.java:49)

Below is my code:

for (int i = 0; i < dataContext.getDataCount(); i++)
{
    InputStream is = dataContext.getStream(i);
    Properties props = dataContext.getProperties(i);

    byte[] buff = new byte[8000];

    int bytesRead = 0;

    ByteArrayOutputStream bao = new ByteArrayOutputStream();

    while ((bytesRead = is.read(buff)) != -1)
    {
        bao.write(buff, 0, bytesRead);
    }

    byte[] data = bao.toByteArray();

    ByteArrayInputStream bin = new ByteArrayInputStream(data);

    DataSet ds = new DataSet();
    ds.ReadXml(bin);
    dataContext.storeStream(is, props);
}

@Smita_Ahuja It is odd, because even in the suggested solution there is readXml(java.io.InputStream) overload. Have you tried to explicitly cast the stream to java.io.InputStream?

ds.ReadXml((java.io.InputStream)bin);

with:
ds.ReadXml((java.io.InputStream)bin);
still same error:

Caused by: com.boomi.process.ProcessException: No signature of method: com.aspose.words.net.System.Data.DataSet.ReadXml() is applicable for argument types: (java.io.ByteArrayInputStream) values: [java.io.ByteArrayInputStream@1f9b0dd7]
Possible solutions: readXml(com.aspose.words.internal.zzYzy), readXml(com.aspose.words.internal.zzZoc), readXml(java.io.InputStream), readXml(java.lang.String), readXml(java.io.InputStream, com.aspose.words.net.System.Data.XmlReadMode), readXml(java.lang.String, com.aspose.words.net.System.Data.XmlReadMode) (in groovy2 script)
	at com.boomi.process.graph.ProcessShape.failProcess(ProcessShape.java:784)
	at com.boomi.process.graph.ProcessShape.notifyDocumentStatus(ProcessShape.java:642)
	at com.boomi.document.util.DocumentUtil.failProcess(DocumentUtil.java:125)
	at com.boomi.document.impl.OutboundDocumentGroupImpl.failRemainingDocuments(OutboundDocumentGroupImpl.java:138)
	at com.boomi.document.handler.DocumentMultiProcessExecutor.invoke(DocumentMultiProcessExecutor.java:36)
	at com.boomi.document.util.DataProcessExecutor.executeStep(DataProcessExecutor.java:111)
	at com.boomi.document.util.DataProcessExecutor.executeDataProcess(DataProcessExecutor.java:65)
	at com.boomi.process.shape.DataProcessShape.execute(DataProcessShape.java:49)
	... 13 more

@Smita_Ahuja,

actually looks like a simple typo. In Java (Groovy), use

document.save(out, saveOptions)

instead of

document.Save(out, saveOptions)

So the first code snippet you sent will look like this:

def document = new Document("...dir/template.docx");
PdfSaveOptions saveOptions = new PdfSaveOptions();
ByteArrayOutputStream out = new ByteArrayOutputStream();
document.save(out, saveOptions);

Similar to Data.DataSet.readXml() etc. You can read more about the Aspose.Words for Java syntax in the Developer Guide.