XML string to PDF File
-
- Member
- Posts: 43
- Joined: Wed Nov 29, 2017 4:36 pm
XML string to PDF File
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,!
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,!
-
- Member
- Posts: 121
- Joined: Wed Mar 22, 2017 5:05 pm
- Location: London, UK
- Contact:
Re: XML string to PDF File
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
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
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);
-
- Member
- Posts: 43
- Joined: Wed Nov 29, 2017 4:36 pm
Re: XML string to PDF File
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?
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);
}
-
- Member
- Posts: 43
- Joined: Wed Nov 29, 2017 4:36 pm
Re: XML string to PDF 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);
}
Last edited by matt.baile on Tue Aug 13, 2019 6:52 pm, edited 3 times in total.
Re: XML string to PDF File
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
-
- Member
- Posts: 43
- Joined: Wed Nov 29, 2017 4:36 pm
Re: XML string to PDF File
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
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.
-
- Member
- Posts: 43
- Joined: Wed Nov 29, 2017 4:36 pm
Re: XML string to PDF File
Ok, 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:
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:
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());
}
-
- Member
- Posts: 43
- Joined: Wed Nov 29, 2017 4:36 pm
Re: XML string to PDF File
Padawan wrote: ↑Wed Aug 14, 2019 1:16 pm Ok, 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:
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:Archive.zipCode: 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()); }
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.
- magnussandstrom
- Advanced member
- Posts: 365
- Joined: Thu Jul 30, 2020 6:34 pm
- Location: Sweden
- Contact: