PDF creation with XML and XLS with parameters

Hi,
I’m trying to convert XML to PDF with a XLS-stylesheet. That goes well using XslFoLoadOptions
But what I cannot find in de documentation is how to add parameters defined in the XLS-document like this: <xsl:param name=“afbeelding”/>
By this way it is possible to add some variables that are not contained in the XML.
Can someone tell how to add these parameters when loading the XSL?

@josnext

Can you please share the sample XML and XSL file with us along with the code snippet that you are using to generate PDF with these files? We will further test the scenario in our environment and address it accordingly.

@asad.ali

The code we use is this:
public static void Example_XSLFO_to_PDF()
{
var _dataDir = @“C:\tmp”;
// Instantiate XslFoLoadOption object
var options = new Pdf.XslFoLoadOptions(“employees.xslt”);
// Create Document object
var pdfDocument = new Aspose.Pdf.Document(“employees.xml”, options);
pdfDocument.Save(_dataDir + “data_xml.pdf”);
}

That works fine with any xsl and xml file we have. My question is that in some cases there are variables defined in the xsl file like this <xsl:param name=“picture”/>, and I wonder if it is possible to include the values for these variables in Aspose. In the Apache Fop solutions there is a command like ‘-param [name] [value]’ that has to be sent along with the xsl and xml file.
What can be used in Aspose? I could not find it in the documentation.
Thanks! (i will attach a xsl file example where you can see the code for these variables at the beginning of the file)

gbav.zip (4.2 KB)

@josnext

We need to further investigate the feasibility of your requirements. For the purpose, an investigation ticket has been logged in our issue tracking system under the ID PDFNET-50068. We will further look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.

We are sorry for the inconvenience.

Hi @assad.ali

I don’t understand. Is there no one that can answer my question straight away?
Aspose is not very client friendly when it comes to support. Our former issue is still not answered and unresolved. We pay almost $9000 for a licence… I’m sorry to say so.

@josnext

We apologize for the inconvenience. Please note that Aspose.PDF used to have this feature in old Aspose.Pdf.Generator where you can use and specify values using variables in XSL file while generating PDF. The BindXml() method of the API is used to generate the PDF with XML and XSL file. Furthermore, you inquiry is related to XslFo files and we need to investigate this feature in new Aspose.Pdf DOM approach. Which is why we have logged this ticket. Nevertheless, we are gathering the related information against your inquiry and will get back to you shortly.

@josnext

We have performed an initial investigation of your requirements. The feature you are looking for can be a new option for Xsl-FO -> PDF converter. The earlier logged ticket PDFNET-50068 will be treated as a new feature request. We will inform you as soon as the requested feature is implemented.

We apologize for the inconvenience.

Ok that is good to hear. Thanks!
Looking forward to it.

@josnext

We have performed an initial investigation against the logged ticket. Here is a workaround for the current version of Aspose.PDF for .NET. It works with our test data and we hope that it will work with your data as well. If your requirements are not satisfied, please share some test data for our reference. Only XSL file is not enough.

        public static void Example_XSLFO_to_PDF()
        {
            var XmlContent = File.ReadAllText(_dataDir + "employees.xml");
            var XsltContent = File.ReadAllText(_dataDir + "employees.xslt");

            var options = new Aspose.Pdf.XslFoLoadOptions();

            //Example of using XsltArgumentList
            XsltArgumentList argsList = new XsltArgumentList();
            argsList.AddParam("isBoldName", "", "yes");
            //---------------------

            var pdfDocument = new Aspose.Pdf.Document(TransformXml(XmlContent, XsltContent, argsList), options);
            pdfDocument.Save(_dataDir + "data_xml.pdf");
        }

        public static MemoryStream TransformXml(string inputXml, string xsltString, XsltArgumentList argsList=null)
        {
            var transform = new XslCompiledTransform();
            using (var reader = XmlReader.Create(new StringReader(xsltString)))
            {
                transform.Load(reader);
            }
            var memoryStream = new MemoryStream();

            var results = new StreamWriter(memoryStream);
            using (var reader = XmlReader.Create(new StringReader(inputXml)))
            {
                transform.Transform(reader, argsList, results);
            }

            memoryStream.Position = 0;
            return memoryStream;
        } 

testdata.zip (1.1 KB)

@asad.ali

Thanks for the reply. Unfortunately i’m not able to reproduce the above example. We’re using Aspose.pdf for java.
I don’t succeed in creating the ‘TransformXml’ function that is needed for the conversion. Do you think is must be possible in the java version as well?

@josnext

Earlier you posted .NET Code, so we investigated the ticket from this perspective. We have now logged a new ticket as PDFJAVA-40677 in our issue management system to achieve a similar thing using Java. We will further look into its details and keep you posted with the status of its resolution. Please be patient and spare us some time.

@asad.ali

Aha, sorry for the confusion. When I check the documentation I often compare various examples. In real we work with Servoy (http://www.servoy.com) that is based on Java, but can be scripted with Javascript. We have the Aspose java libraries included in the Servoy environment.
Thanks for the help.

@josnext

Please try using the below Java Code and let us know about your feedback:

public static void Example_XSLFO_to_PDF() throws TransformerException {
        String XmlContent = stringFromFile(dataDir + "employees.xml");
        String XsltContent = stringFromFile(dataDir + "employees.xslt");

        XslFoLoadOptions options = new XslFoLoadOptions();
        Document pdfDocument = new Document(transformXml(XmlContent, XsltContent), options);
        pdfDocument.save(dataDir + "data_xml_21_7.pdf");
    }
    public static String stringFromFile(String fileName) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        if(line.startsWith("\uFEFF"))
            line = line.substring(1);

        while (line != null) {
            sb.append(line);
            sb.append("\r\n");
            line = br.readLine();
        }
        br.close();
        return sb.toString();
    }
    public static ByteArrayInputStream transformXml(String inputXml, String xsltString) throws TransformerException {

        javax.xml.transform.TransformerFactory transformerFactory = javax.xml.transform.TransformerFactory.newInstance();
        javax.xml.transform.stream.StreamSource xsltSource = new javax.xml.transform.stream.StreamSource(new java.io.StringReader(xsltString));

        ///Example of using setParameter
        javax.xml.transform.Transformer xsltTransformer = transformerFactory.newTransformer(xsltSource);
        xsltTransformer.setParameter("isBoldName", "yes");
        //---------------------

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        javax.xml.transform.stream.StreamSource xmlSource = new javax.xml.transform.stream.StreamSource(new java.io.StringReader(inputXml));
        javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(baos);
        xsltTransformer.transform(xmlSource, result);

        byte[] buf = baos.toByteArray();
        return new ByteArrayInputStream(buf);
    }

The issues you have found earlier (filed as PDFNET-50068) have been fixed in Aspose.PDF for .NET 21.7.

@asad.ali

Thanks! Untill now I was not able to test it but when I see the code I think we can use it well. I will let you know!

1 Like

@asad.ali

I have tested your code and it works almost fine. The only thing that does not work is the implementation of a jpg image in the pdf as a variable in the xsl.
I include some example files that I used. In the Fop(apache) lib the path to the image should be like this ‘file:///’ + [path], but may be that syntax in not right.Archief.zip (13.1 KB)

@josnext

Would you please share the error details which is thrown by the API at your end?

@asad.ali

Unfortunately there is no error in the log. It stays blank. The code is executed, and the PDF is produced, but the logo is not included.
I use the exact code as shown above. I filled in the variables though:

xsltTransformer = transformerFactory.newTransformer(xsltSource);
xsltTransformer.setParameter(‘Debiteurnummer’,‘12345’);
xsltTransformer.setParameter(‘Vraagstelling’,‘Okidoki’);
xsltTransformer.setParameter(‘afbeelding’,‘file:///Logo.jpg’);

@asad.ali

Sorry to have bothered you. I found ou it was a matter of writing the path to the file. It all works fine now. The subject can be closed.
Thanks!