-1

I am creating pdf and xml files for invoices separately.

But now as per new rule I need to send pdf doc in the xml file as code (base64Binary [RFC 2045]).

In PHP, I am using SimpleXMLElement() function.

Should I use encoded pdf in xml as taxt in a tag(attachment)?

Deepak Dholiyan
  • 1,774
  • 1
  • 20
  • 35

2 Answers2

2

You can try something like that

$xmlString = <<<XML 
<?xml version='1.0'?> 
<document> 
    <cmd>login</cmd> 
    <login>Richard</login> 
</document> 
XML; 
$base64Pdf = base64_encode(file_get_contents('/path/to/file.pdf'));
$simpleXml = new SimpleXMLElement($xmlString);
$simpleXml->addChild('document', $base64Pdf);

Code to decode pdf.

$simpleXml = new SimpleXMLElement($xmlString);
$encodedPdf = $simpleXml->document;
$decodedPdf = base64_decode($encodedPdf);
file_put_contents("/path/to/decoded.pdf", $decodedPdf);
Eleftherios
  • 186
  • 1
  • 13
1

You can grab the contents of the document and transform into base64 and put in your XML tag

https://secure.php.net/manual/en/function.file-get-contents.php

https://secure.php.net/manual/en/function.base64-encode.php

something close to it.

$file = "file.pdf";


$content = file_get_contents($file);

$base64 = base64_encode($content);

$xml = new SimpleXMLElement('<xml/>');

$xml->addChild('file', "$base64");

print $xml->asXML();
Jerfeson Guerreiro
  • 745
  • 1
  • 10
  • 19