Convert XML to PDF?

Jun 10, 2012 at 12:59 PM
Edited Jun 10, 2012 at 1:06 PM

Hi, 

I need some help, please. 

I need to apply an XSLT file to an XML and then convert it to PDF.

How can I do that?

Where can I find an example?

Thanks in advanced

Jun 10, 2012 at 1:02 PM
Edited Jun 10, 2012 at 1:06 PM

This is urgent, please.

Sep 27, 2012 at 9:49 AM

You can simply use the .Net XslCompliedTransform class (cache them) to do the transformation of the Xml and Xsl into the FO stream if required. We return a byte array from our WCF service:

        /// <summary>
        /// Render the PDF byte array using FO.Net.
        /// </summary>
        private static Byte[] Render(String xml, String xslFile, Boolean returnLog, out String renderingLog)
        {
            renderingLog = null;

            // Get the FO XmlReader from the FO or xml and xsl file.
            using (XmlReader rdr = GetFOXmlReader(xml, xslFile))
            {
                FonetDriver driver = InitialiseFonetDriver(xslFile, returnLog);

                using (MemoryStream outputStream = new MemoryStream())
                {
                    try
                    {
                        driver.Render(rdr, outputStream);
                    }
                    finally
                    {
                        // Return the rendering process log if available.
                        if ((driver.Log != null) && (driver.Log.Length > 0))
                        {
                            renderingLog = driver.Log.ToString();
                        }
                    }

                    return outputStream.ToArray();
                }
            }
        }

        /// <summary>
        /// Get the FO stream from the xml directly (assumed where no xsl file specified)
        /// or transform the xml and xsl file specified into the FO stream.
        /// </summary>
        private static XmlReader GetFOXmlReader(String xml, String xslFile)
        {           
            if (!String.IsNullOrEmpty(xslFile))
            {
                // Get the XslCompiledTransform object to use.
                XslCompiledTransform transform = GetXslCompiledTransform(xslFile);              
               
                // Transform the xml and xsl into a FO.
                using (XmlReader reader = XmlReader.Create(new StringReader(xml), readerSettings))
                {
                    Stream foStream = new MemoryStream();
                    transform.Transform(reader, null, foStream);
                    foStream.Position = 0;

                    return XmlReader.Create(foStream, readerSettings);
                }
            }
            else
            {
                // When no xsl file was specified assume the xml supplied is a FO.
                return XmlReader.Create(new StringReader(xml), readerSettings);
            }
        }

        /// <summary>
        /// Get the XslCompiledTransform object from cache if possible.
        /// The cache manager will only be available where it has been configured in the hosting config file.
        /// </summary>
        private static XslCompiledTransform GetXslCompiledTransform(String xslFile)
        {
            XslCompiledTransform transform = null;

            // Use the full xslFile path as part of the cache key.
            String cacheKey = String.Format("FONetXsl:{0}", xslFile.ToLower());

            if ((m_cacheTimeout > 0) && (m_cacheManager != null))
            {
                // Transform to an FO using the xml and the cached XslCompiledTransform object
                // else create and cache a new XslCompiledTransform object.
                transform = (XslCompiledTransform)m_cacheManager.GetData(cacheKey);
            }

            if (transform == null)
            {
                XsltSettings xsltSettings = new XsltSettings()
                {
                    EnableScript = false,
                    EnableDocumentFunction = true
                };

                transform = new XslCompiledTransform();
                transform.Load(xslFile, xsltSettings, new XmlUrlResolver() { Credentials = CredentialCache.DefaultCredentials });

                if ((m_cacheTimeout > 0) && (m_cacheManager != null))
                {
                    m_cacheManager.Add(cacheKey, transform, CacheItemPriority.Normal, null, new AbsoluteTime(TimeSpan.FromMilliseconds(m_cacheTimeout)));
                }
            }

            return transform;
        }

 

Jul 6, 2015 at 2:21 PM
I'm trying to acheive the same thing as Ramon84. The example provided looks easy enough but there are a lot of stuff not implemented which makes this non-functional as stand-alone. Do you need to use cache and log? I'm looking for a minimalistic approach to simply generating an xsl-fo file from an xml file and a xsl stylesheet file. From this, I want to then generate the PDF and return it to the browser (a REST Service using WCF).

Sorry for hijacking this old thread but It seems to fit my question.