Best way to create PDF from XML XSLT in C #

I have a requirement to create a PDF file in XML format. I think there is no way to directly create pdf files from xml, but using XSLT or XSL FO, I believe that this can be done. I have read many articles looking for a good way to do this with C #.

-> What is the best approach during this? any example will really be great.

My scenario:

I have an XML that looks like this:

<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 file with a table showing all this information?

I know that there are many similar questions, but most of them are out of date. Any help is really appreciated.

+6
source share
3 answers

In the past, I used a commercial library called Ibex PDF Creator to create PDF documents from XML data using the XSL-FO standard, which worked very well.

Here is an example of how I will use it:

XML data:

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

XSL-FO Scheme:

 <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> 

Creating a 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); 

Hope this helps.

+5
source

I used 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); } 
+1
source

You can create a pdf file from xml using Aspose.PDF for .NET API. I could find sample code for converting xml to pdf in C # /. NET on the Aspose documentation page for pdf.

-1
source

Source: https://habr.com/ru/post/949200/


All Articles