Jump to content

Recommended Posts

Posted

When you receive an electronic invoice in the formats ZUGFeRD, Factur-X, X-Rechnung or UBL, you may need to visualize the XML file. You can use our DynaPDF functions to extract the XML from the ZUGFeRD invoice. Once you have the XML for one of the formats UBL or Cross Industry Invoice, you may want to convert it to HTML and show it in a web viewer.

 

We leverage the XRechnung Visualization Transformators project from GitHub. This project has a couple of XML stylesheets to convert our invoices to HTML. For this they first convert the UBL or CII XML to an intermediate XML and finally convert that to XML with a second transformation. There is also an alternative output to produce a XML for Apache FOP to make it a PDF file.

SaxonVisualize2.png

Step 1: Load Saxon

 

First load the Saxon library. You download the libraries either from our website or directly from Saxonica website. We suggest to copy the Saxon library to the same folder as the plugin. Then you only need to pass the library file name to load it. 

# Load Saxon in file Saxon Visualize electronic Invoices
 
Set Variable [ $path ; Value: "" ]
# you may need to put in your path or have the libraries in the folder for the plugin
If [ MBS("IsMacOS") ]
	Set Variable [ $path ; Value: "libsaxon-eec-12.5.0.dylib" ]
Else If [ MBS("IsWindows") ]
	Set Variable [ $path ; Value: "libsaxon-eec-12.5.0.dll" ]
Else If [ MBS("IsLinux") ]
	Set Variable [ $path ; Value: "libsaxon-eec-12.5.0.so" ]
End If
# 
Set Variable [ $r ; Value: MBS( "Saxon.Load"; $Path ) ]
If [ MBS("IsError") ]
	Show Custom Dialog [ "Failed to load saxon library." ; $r ]
	Halt Script
End If

Step 2: Check the type

 

The main script loads Saxon if needed. Then we check if we got the folder with the xrechnung-visualization xsl files. You download the release of xrechnung-visualization, unpack the zip/tar file and then tell our plugin the native path to the xsl folder, e.g. "/Users/cs/xrechnung-3.0.2-xrechnung-visualization-2024-06-20/xsl". 

 

We check the content of the XML and if it contains the namespace declaration for UBL, we use the ubl-invoice-xr.xsl stylesheet to transform the XML. But if we find the CrossIndustryInvoice namespace, we can use the cli-xr.xsl stylesheet instead and call the Run XSLT script.

# Run in file Saxon Visualize electronic Invoices
 
# Load Saxon once if not yet loaded
If [ MBS("Saxon.IsLoaded") = 0 ]
	Perform Script [ Load Saxon ; Specified: From list ; Parameter:    ]
End If
# 
If [ IsEmpty ( Saxon Visualize electronic Invoices::XSLFolder ) ]
	Show Custom Dialog [ "You need to specify the folder…" ; "Please download from github and put the path to XSL folder path into the field." ]
	Open URL [ With dialog: Off ; "https://github.com/itplr-kosit/xrechnung-visualization/releases" ]
	Exit Script [ Text Result:    ]
End If
# 
# We can do UBL invoices and Cross Industry Invoices (XRechnung)
If [ Position ( Saxon Visualize electronic Invoices::Invoice XML ; "urn:oasis:names:specification:ubl:schema:xsd:Invoice" ; 1 ; 1 ) > 1 ]
	Perform Script [ Run XSLT ; Specified: From list ; Parameter: "ubl-invoice-xr.xsl" ]
Else If [ Position ( Saxon Visualize electronic Invoices::Invoice XML ; "unece:uncefact:data:standard:CrossIndustryInvoice" ; 1 ; 1 ) ]
	Perform Script [ Run XSLT ; Specified: From list ; Parameter: "cii-xr.xsl" ]
End If

 

SaxonVisualize1.png

Step 3: Perform conversion

 

In the following script we perform the transformation. We first set the working directory to the xsl folder, so the related files referenced by the stylesheet can be found. Technically you could resolve this and make it one big stylesheet, but we just use them as they are. And the stylesheets need the common-xr.xsl file with common transformations. We read the XSLT to use and apply it on the invoice XML. When we get that XML, we can apply the stylesheet xrechnung-html.xsl to create the HTML. When we store the HTML in the field, our web viewer shows it.

# Run XSLT in file Saxon Visualize electronic Invoices
 
Set Variable [ $XSLTName ; Value: Get(ScriptParameter) ]
# 
# Set the folder to find related files
Set Variable [ $r ; Value: MBS( "Saxon.SetCurrentWorkingDirectory"; Saxon Visualize electronic Invoices::XSLFolder) ]
# 
# we read invoice XML into variable
Set Variable [ $invoiceXML ; Value: Saxon Visualize electronic Invoices::Invoice XML ]
# 
# read the XSLT to convert
Set Variable [ $path ; Value: MBS("Path.AddPathComponent"; Saxon Visualize electronic Invoices::XSLFolder; $XSLTName) ]
Set Variable [ $InvoiceXSLT ; Value: MBS("Text.ReadTextFile"; $path; "utf-8") ]
# 
# Apply the XSLT
Set Variable [ $IntermediateXML ; Value: MBS( "Saxon.XSLT"; $invoiceXML; $InvoiceXSLT ) ]
Set Field [ Saxon Visualize electronic Invoices::Intermediate XML ; $IntermediateXML ]
# 
# now get second XSLT to convert to HTML
Set Variable [ $path ; Value: MBS("Path.AddPathComponent"; Saxon Visualize electronic Invoices::XSLFolder; "xrechnung-html.xsl") ]
Set Variable [ $HTMLXSLT ; Value: MBS("Text.ReadTextFile"; $path; "utf-8") ]
# 
# Generate the HTML
Set Variable [ $InvoiceHTML ; Value: MBS( "Saxon.XSLT"; $IntermediateXML; $HTMLXSLT ) ]
# 
# Store in the field
Set Field [ Saxon Visualize electronic Invoices::HTML ; $InvoiceHTML ]

To have the web viewer show the html, we use a calculation for the web viewer URL. If the HTML field is empty, we use "about:blank" as an URL, so we see an empty page there. Otherwise we build the data URL to pass the HTML directly. We pass data type with charset and encoding. We encode the HTML to avoid trouble with passing special characters within an URL.

If(
	IsEmpty( Saxon Visualize electronic Invoices::HTML );
	"about:blank";
	"data:text/html;charset=utf-8;base64," &
	Base64Encode( Saxon Visualize electronic Invoices::HTML )
)

Please try and let us know if you like it. You may want to modify the stylesheet if you prefer a different style.

 

Download here

 

See also

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.