Xml to base64 pdf

I have an xml invoice. At the bottom of this xml, I want to add the base64 (pdf). How can I do this ?

So you want convert a PDF to a base64 string and add it to an element inside your XML file?
You may run into memory issues trying to read a PDF, convert it to base64 and then add it to an arbitrary XML file.

May I ask why you need to do that? Is it because you want to display the PDF as part of your document?

I have an xml and I want to add the pdf code inside my xml file. My customer asked for it because he needs to import my invoice in his erp. He already gets these kind of invoices from another supplier. Here is part of a sample he sent me

image

A script like this should work. You may have to adjust according to your own requirements:

var pdfName = "C:/myTests/PDFTest.pdf";
var xmlName = Watch.GetJobFileName();

// Open PDF as binary stream
var stream = new ActiveXObject("ADODB.Stream");
stream.Type = 1;       // 1=Binary, 2=Text
stream.open();
stream.loadFromFile(pdfName);

// Open XML as XML Document Object
var xmlDoc = new ActiveXObject("MSXML.DOMDocument");
xmlDoc.load(xmlName);
// Create empty PDFBinary element and set its type to base64
var elem = xmlDoc.createElement("PDFBinary");
elem.dataType = "bin.base64";

// Read binary stream and store it in element. The binary stream gets
// converted automatically to base64 because of the element's data type
elem.nodeTypedValue = stream.Read();
stream.close();
// append new element as last child of XML's root element, then save the file
xmlDoc.documentElement.appendChild(elem)
xmlDoc.save(xmlName);

Hi Phil, I have tried replacing var pdfName = “C:/Invoice_74338815-00.pdf”; by var pdfName = “C:/Invoice_%1.pdf”; and it does not work. What am I doing wrong ?

To expand variables, you must use the Watch.ExpandString() method:

var pdfName = Watch.ExpandString(“C:/Invoice_%1.pdf”);