Page 1 of 1

XML string to PDF File

Posted: Mon Aug 12, 2019 5:39 pm
by matt.baile
Hello,
I have a new project where we're receiving a base 64 string inside of an XML element. The XML file that we receive will move through a SWITCH flow and I'd like to use something to convert the string into a PDF file. Is there anything in SWITCH that can do this or a source for better information on how this needs to be done?

For example, the XML element might look something like this:
<InvoiceFile>JVBERi0xLjQKJeLjz9M....<InvoiceFile>

Is there a way to use SWITCH to convert this to the pdf file it needs to be?

Thanks,!

Re: XML string to PDF File

Posted: Mon Aug 12, 2019 10:31 pm
by Malcolm Mackenzie
Hi Matt
Interesting, seems like you can do it with execute command.
Haven't done it here though.

https://www.igorkromin.net/index.php/20 ... -or-macos/
or
https://www.proxoft.com/base64.aspx

Test the file
https://base64.guru/converter/decode/pdf

Re: XML string to PDF File

Posted: Tue Aug 13, 2019 9:20 am
by freddyp
It is not difficult to do with a script. Here is a code snippet that shows how to decode from base64 and save the result to a file:

Code: Select all

var xml = new Document( somePathToTheXMLfile);
var base64String = xml.evalToString( "/xpath/to/the/InvoiceFile");
var decodedResult = new ByteArray( base64String, "FromBase64");
var decodedFilePath = job.createPathWithName( someFileName);
File.writeByteArray( decodedFilePath, decodedResult);

Re: XML string to PDF File

Posted: Tue Aug 13, 2019 4:21 pm
by matt.baile
Thank you!

I went the route of a script as it seemed easier and I don't have a text file with the string, rather it appears as a node within the xml and I wasn't sure how to call that out.

Right now I'm dropping the file into the folder on our network at \\primus\PowerSwitch\working_folders\cpi so I've included that path. I then set the
string to the proper path and have the file name as file.pdf. I'm not getting any errors in my log, but the file just sits in the folder I drop it to and nothing happens.

Any thoughts?

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	
	
var xml = new Document( "//primus/PowerSwitch/working_folders/cpi");
var base64String = xml.evalToString("/metadata/orders/order/InvoiceFile", null);
var decodedResult = new ByteArray( base64String, "FromBase64");
var decodedFilePath = job.createPathWithName( "file.pdf");
File.writeByteArray( decodedFilePath, decodedResult);
			
	
}

Re: XML string to PDF File

Posted: Tue Aug 13, 2019 4:28 pm
by matt.baile
freddyp wrote: Tue Aug 13, 2019 9:20 am It is not difficult to do with a script. Here is a code snippet that shows how to decode from base64 and save the result to a file:


In addition to my last post, don't I need to go through an XML Pickup element in order to use the xml.evalToString piece of this or will it work without going through that?

If I change the flow and put a folder and xml picku up, I then use the job.getPath command, which seems to recognize the file, but I get an error. Code and error below:

Code: Select all

function jobArrived( s : Switch, job : Job )
{
var xml = new Document(job.getPath());
var base64String = xml.evalToString("/metadata/orders/order/InvoiceFile", null);
var decodedResult = new ByteArray( base64String, "FromBase64");
var decodedFilePath = job.createPathWithName( "file.pdf");
File.writeByteArray( decodedFilePath, decodedResult);
}

Error. Invalid arguments were passed to ByteArray constructor

Re: XML string to PDF File

Posted: Tue Aug 13, 2019 4:32 pm
by jan_suhr
Yes, you need to pickup the XML to a dataset. Then you can call that dataset and set an XPath to the node where the base64 line is.


Jan

Re: XML string to PDF File

Posted: Tue Aug 13, 2019 4:35 pm
by matt.baile
jan_suhr wrote: Tue Aug 13, 2019 4:32 pm Yes, you need to pickup the XML to a dataset. Then you can call that dataset and set an XPath to the node where the base64 line is.


Jan


Thanks! I just edited my reply with an error message that I'm getting. This script is honestly a bit out of my league, so sorry for all of the questions.
-Matt

Re: XML string to PDF File

Posted: Tue Aug 13, 2019 5:55 pm
by Padawan
Most likely the XPath expression is incorrect. Would it be possible to share your XML file? Then we can tailor an example specific to the structure of your XML file.

Re: XML string to PDF File

Posted: Tue Aug 13, 2019 6:07 pm
by matt.baile
Padawan wrote: Tue Aug 13, 2019 5:55 pm Most likely the XPath expression is incorrect. Would it be possible to share your XML file? Then we can tailor an example specific to the structure of your XML file.

sure thing. I wasn't' apple to upload the xml file directly so I placed it in a zip archive.

Re: XML string to PDF File

Posted: Wed Aug 14, 2019 1:16 pm
by Padawan
ImageOk, I used the earlier mentioned code and created a flow with it, you can find it in the attached zip. The flow looks like this:

Image


Basically:
- An XML Pickup to attach your xml file as metadata
- A script to extract the part of the xml which is the PDF and save it as a PDF. The output from this script is only the PDF.
There are some properties to make it adjustable: name of the XML dataset (this needs to match the one in the XML Pickup), Xpath expression which says the script where in the xml file the file is stored, and output file extension which does what it says.
- The output file is the PDF file with the XML as dataset, ready to be used.

This is the adjusted code:

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	
	var ds = job.getDataset(s.getPropertyValue("datasetName"));
	var dsPath = ds.getPath();
	var xml = new Document(dsPath);
	var base64String = xml.evalToString(s.getPropertyValue("xpath"), null);
	var decodedResult = new ByteArray( base64String, "FromBase64");
	var filename = job.getNameProper() + "." + s.getPropertyValue("extension");
	var decodedFilePath = job.createPathWithName( filename);
	File.writeByteArray( decodedFilePath, decodedResult);
	job.sendToSingle(decodedFilePath);
	job.sendToNull(job.getPath());
}
Archive.zip
(16.86 KiB) Downloaded 982 times

Re: XML string to PDF File

Posted: Wed Aug 14, 2019 4:40 pm
by matt.baile
Padawan wrote: Wed Aug 14, 2019 1:16 pm ImageOk, I used the earlier mentioned code and created a flow with it, you can find it in the attached zip. The flow looks like this:

Image


Basically:
- An XML Pickup to attach your xml file as metadata
- A script to extract the part of the xml which is the PDF and save it as a PDF. The output from this script is only the PDF.
There are some properties to make it adjustable: name of the XML dataset (this needs to match the one in the XML Pickup), Xpath expression which says the script where in the xml file the file is stored, and output file extension which does what it says.
- The output file is the PDF file with the XML as dataset, ready to be used.

This is the adjusted code:

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	
	var ds = job.getDataset(s.getPropertyValue("datasetName"));
	var dsPath = ds.getPath();
	var xml = new Document(dsPath);
	var base64String = xml.evalToString(s.getPropertyValue("xpath"), null);
	var decodedResult = new ByteArray( base64String, "FromBase64");
	var filename = job.getNameProper() + "." + s.getPropertyValue("extension");
	var decodedFilePath = job.createPathWithName( filename);
	File.writeByteArray( decodedFilePath, decodedResult);
	job.sendToSingle(decodedFilePath);
	job.sendToNull(job.getPath());
}
Archive.zip

This is excellent! I just plugged in the script and set the properties, and I got a PDF file that looks great!!! I cannot thank you enough for this, it is very much appreciated.

Re: XML string to PDF File

Posted: Tue Oct 10, 2023 8:34 am
by magnussandstrom
Got to love the AI-bots..