XML string to PDF File

Post Reply
matt.baile
Member
Posts: 43
Joined: Wed Nov 29, 2017 4:36 pm

XML string to PDF File

Post 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,!
Malcolm Mackenzie
Member
Posts: 121
Joined: Wed Mar 22, 2017 5:05 pm
Location: London, UK
Contact:

Re: XML string to PDF File

Post 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
freddyp
Advanced member
Posts: 1008
Joined: Thu Feb 09, 2012 3:53 pm

Re: XML string to PDF File

Post 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);
matt.baile
Member
Posts: 43
Joined: Wed Nov 29, 2017 4:36 pm

Re: XML string to PDF File

Post 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);
			
	
}
matt.baile
Member
Posts: 43
Joined: Wed Nov 29, 2017 4:36 pm

Re: XML string to PDF File

Post 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
Last edited by matt.baile on Tue Aug 13, 2019 6:52 pm, edited 3 times in total.
jan_suhr
Advanced member
Posts: 586
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: XML string to PDF File

Post 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
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
matt.baile
Member
Posts: 43
Joined: Wed Nov 29, 2017 4:36 pm

Re: XML string to PDF File

Post 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
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: XML string to PDF File

Post 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.
matt.baile
Member
Posts: 43
Joined: Wed Nov 29, 2017 4:36 pm

Re: XML string to PDF File

Post 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.
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: XML string to PDF File

Post 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 790 times
matt.baile
Member
Posts: 43
Joined: Wed Nov 29, 2017 4:36 pm

Re: XML string to PDF File

Post 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.
User avatar
magnussandstrom
Advanced member
Posts: 345
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: XML string to PDF File

Post by magnussandstrom »

Got to love the AI-bots..
Post Reply