Reputation: 340
I have an XSL-FO file and data is available in xml/json formats. I wanted to create a pdf using this xsl structure.
Can anyone suggest any open source libraries for conversion? I want it to be done at the C# level.
Note: I tried converting to html but as it is xsl-fo file I can't get the alignment.
Upvotes: 3
Views: 8812
Reputation: 2039
Not sure if your project is a .Net Core
project, but I have created a pure port of Apache FOP version 2.8
that runs in .Net Core
. Check it out:
https://github.com/sorcerdon/FOP.NetCore
Here is the nuget:
Since this is a pure port, that means that it can do anything that Apache FOP can do.
Upvotes: 2
Reputation: 340
I tried using fo.net and it worked for me, here is the sample code
string lBaseDir = System.IO.Path.GetDirectoryName("e:\thermalpdf.xsl");
XslCompiledTransform lXslt = new XslCompiledTransform();
lXslt.Load("e:\thermalpdf.xsl");
lXslt.Transform("e:\billingData1.xml", "books1.fo");
FileStream lFileInputStreamFo = new FileStream("books1.fo", FileMode.Open);
FileStream lFileOutputStreamPDF = new FileStream("e:\response2.pdf", FileMode.Create);
FonetDriver lDriver = FonetDriver.Make();
lDriver.BaseDirectory = new DirectoryInfo(lBaseDir);
lDriver.CloseOnExit = true;
lDriver.Render(lFileInputStreamFo, lFileOutputStreamPDF);
lFileInputStreamFo.Close();
lFileOutputStreamPDF.Close();
Upvotes: 1
Reputation: 3095
You can use Apache FOP (https://xmlgraphics.apache.org/fop/) tool. It can generate PDF document from XSL-FO input.
From the C# it is possible to start Apache FOP process pointing it to the XSL-FO file (or use stdin, so you don't have to use any temporary files). After process exists you get PDF file (in file on disk or stdout).
For start you can make Apache FOP read XSL-FO file and write PDF file to disk, for that use Process
class (https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process?view=netframework-4.8):
Draft code snippet (may contains errors but it should be a good start for you):
Process.Start("C:\\path\\to\\fop input_xsl-fo.xml output.pdf").WaitForExit();
Upvotes: 4