I want to create PDF files using https://github.com/itplr-kosit/xrechnung-visualization.
Based on the documentation I have to create an intermediate file first which should be valid to “https://github.com/itplr-kosit/xrechnung-visualization/blob/master/src/xsd/xrechnung-semantic-model.xsd” than I should use “https://github.com/itplr-kosit/xrechnung-visualization/blob/master/src/xsl/xrechnung-html.xsl” or “https://github.com/itplr-kosit/xrechnung-visualization/blob/master/src/xsl/xr-pdf.xsl” to create HTML file or PDF file.
I am using Saxon-He to do this and I could generate the HTML file.
I am using Saxon-He to convert XML to XR like this:
public static void XmlToInterWithSaxon(byte[] xmlData, byte[] xslUblData, byte[] xslHtmlData, byte[] xslPdfData, string xmlPath, string xsdPath)
{
try
{
Processor processor = new Processor();
XsltCompiler xsltCompiler = processor.NewXsltCompiler();
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.DtdProcessing = DtdProcessing.Parse;
var interResultt = “”;
using (MemoryStream msXslt = new MemoryStream(xslUblData))
{
using (XmlReader reader = XmlReader.Create(msXslt, readerSettings))
{
xsltCompiler.BaseUri = new Uri(“C:Pathtoxsl”);
XsltExecutable xsltExecutable = xsltCompiler.Compile(reader);
var xsltTransformer = xsltExecutable.Load30();
using (MemoryStream inputStream = new MemoryStream(xmlData))
{
using (MemoryStream outputStream = new MemoryStream())
{
xsltTransformer.Transform(inputStream, processor.NewSerializer(outputStream));
interResultt = System.Text.Encoding.UTF8.GetString(outputStream.ToArray());
string directoryPath = Path.GetDirectoryName(xmlPath);
string newFileName = Path.GetFileNameWithoutExtension(xmlPath);
string destinationFilePath = Path.Combine(directoryPath, newFileName + "-inter" + ".xml");
File.WriteAllText(destinationFilePath, interResultt);
var isInterValid = ValidateXml(destinationFilePath, xsdPath);
byte[] interData = Encoding.UTF8.GetBytes(interResultt);
if (isInterValid)
{
InterToPDFWithSaxon(interData, xslPdfData, xmlPath);
}
else
{
throw new Exception("Error validating XML with XSD");
}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error transforming XML to HTML: " + ex.Message);
}
}
than I am using the same function to convert XR to PDF like this:
public static void InterToPDFWithSaxon(byte[] xmlData, byte[] xslPdfData, string xmlPath)
{
try
{
Processor processor = new Processor();
XsltCompiler xsltCompiler = processor.NewXsltCompiler();
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.DtdProcessing = DtdProcessing.Parse;
var pdfResult = “”;
var pdfFilePath = “”;
using (MemoryStream msXslt = new MemoryStream(xslPdfData))
{
using (XmlReader reader = XmlReader.Create(msXslt, readerSettings))
{
xsltCompiler.BaseUri = new Uri(“C:Pathtoxsl”);
XsltExecutable xsltExecutable = xsltCompiler.Compile(reader);
var xsltTransformer = xsltExecutable.Load30();
using (MemoryStream inputStream = new MemoryStream(xmlData))
{
using (MemoryStream outputStream = new MemoryStream())
{
xsltTransformer.Transform(inputStream, processor.NewSerializer(outputStream));
pdfResult = System.Text.Encoding.UTF8.GetString(outputStream.ToArray());
pdfFilePath = Path.ChangeExtension(xmlPath, ".pdf");
File.WriteAllText(pdfFilePath, pdfResult);
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error transforming XML to PDF and saving to file: " + ex.Message);
}
}
It generates a pdf file but when try to open it says: “There was an error opening this document, The file is damaged and could not be repaired”
Is there anything else that I should use?
mensur is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.