Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to create PDF from XML XSLT in C#

I have a requirement to crate a PDF of XML Records. I think there is no way to directly create pdf from xml but using XSLT or XSL FO i believe it can be done. I have been reading lots of articles searching for the good way to do that using C#.

--> What's the best approach of during this? any example would really be great.

My Scenario:

I have XML that looks like:

<Products>
  <Brand name="Test">
    <Quantity value="2/>
     <Price value="$20"/>
  </Brand>
  <Brand name="Test2">
    <Quantity value="3/>
     <Price value="$30"/>
  </Brand>
  <Brand name="Test3">
    <Quantity value="4/>
     <Price value="$40"/>
  </Brand>
</Products>

How can i create a pdf that will have a table showing all this information?

I know there are lots of similar questions like this but most of them are outdated. Any help really appreciated.

like image 914
Bravo11 Avatar asked Jul 11 '13 08:07

Bravo11


People also ask

Can XSLT transform XML to PDF?

You can't convert directly from XML to PDF with XSLT.

Is there any benefit of converting XML to XSLT?

XSLT brings XML, schemas, and XPath together in a declarative programming language. It is used to query and transform XML (and, with XSLT3, JSON) data, enabling one to express data in new ways, or to create new data based on the content or structure of existing data.


2 Answers

In the past I've used a commercial library called Ibex PDF Creator to generate PDF documents from XML data using the XSL-FO standard that has worked really well.

Here's an example of how I would use it:

XML data:

<DocumentRoot>
    <!-- Some content -->
</DocumentRoot>

XSL-FO layout:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/DocumentRoot">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:ibex="http://www.xmlpdf.com/2003/ibex/Format">
            <ibex:properties
                title="Some document"
                subject=""
                author=""
                keywords=""
                creator="" />
            <fo:layout-master-set>
                <fo:simple-page-master master-name="A4" page-width="210mm" page-height="297mm">
                    <fo:region-body margin-bottom="1cm" margin-top="3cm"/>
                    <fo:region-before extent="20mm"/>
                    <fo:region-after extent="8mm"/>
                    <fo:region-start extent="1mm"/>
                    <fo:region-end extent="1mm"/>
                </fo:simple-page-master>
            </fo:layout-master-set>
        </<fo:root>
    </xsl:template>
</xsl:stylesheet>

Generating the PDF document in .NET:

var data = new MemoryStream(dataBytes);
var layout = new MemoryStream(layoutBytes);
var pdf = new MemoryStream();

// Using the Ibex PDF Creator .NET API
var doc = new FODocument();
doc.generate(data, layout, pdf);

I hope this helps.

like image 177
Enrico Campidoglio Avatar answered Nov 15 '22 18:11

Enrico Campidoglio


I used the Apache Fop.bat in a method like this. (using System.Diagnostics)

    private void XML_TO_PDF_VIA_FOP(String xmlName, String xsltName, String pdfName) 
    { 
      String batchFile = "XSLT\\FOPv1\\fop.bat";    
      String xmlFile = xmlName;
      String xsltFile = "XSLT\\" + xsltName; 
      String pdfFile = pdfName; 
      Process.Start(batchFile, " -xml " + xmlFile + " -xsl " + xsltFile + " -pdf " + pdfFile); 
    }
like image 21
gman Avatar answered Nov 15 '22 18:11

gman