0

I am trying to insert base64_encoded pdf content in xml with below code.

1) Getting pdf data in variable.

$varFilePath = 'temp_invoices/' . $varFileName;

$arrFileData = file_get_contents($varFilePath);

$varPdfBase64 = base64_encode($arrFileData);

2) Using base64 data in xml tag

$Attachment->addChild('Attachment', $varPdfBase64);

The variable $varPdfBase64 has too big content and its creating problem.

Now when I am trying to download xml file this is showing plain text instead downloading with xml tags.

Note:- All tags are present in source code(ctrl+u).

How can I resolve this issue?

Deepak Dholiyan
  • 1,774
  • 1
  • 20
  • 35
  • I know it's not the response, but this post maybe can help you : https://stackoverflow.com/questions/17835928/write-xml-in-base64-encoding you can encode a part or all your xml with this method. – Shim-Sao Dec 21 '18 at 12:11
  • This doesn't work – Deepak Dholiyan Dec 21 '18 at 12:21
  • Are you using [DOMDocument](http://php.net/manual/en/class.domdocument.php), [SimpleXML](http://php.net/manual/en/book.simplexml.php) or something else? – Code4R7 Dec 21 '18 at 12:23
  • I add in title, I am using SimpleXmlElement :) – Deepak Dholiyan Dec 21 '18 at 12:24
  • Maybe with this : $Attachment->addChild('Attachment', '<![CDATA[' . $varPdfBase64. ']]>'); – Shim-Sao Dec 21 '18 at 12:31
  • I think the problem is with `'Attachment'`, that does not look like valid HTML to me. No browser will recognize the tag and the PDF will be rendered as plain text with the rest of the HMTL. Instead you should use an anchor tag `a` with a `href` attribute that links to the binary PDF file, or do something fun with JavaScript. – Code4R7 Dec 21 '18 at 12:34
  • @Code4R7, No this is not an issue. We can use own tag in xml – Deepak Dholiyan Dec 21 '18 at 12:36
  • @Shim-Sao, I have checked with CDATA but this does not work. This just add with text. – Deepak Dholiyan Dec 21 '18 at 12:36
  • As per new rule, I need to send annexed pdf with electronic invoice. – Deepak Dholiyan Dec 21 '18 at 12:39
  • Then I don't get the problem I think. You create an XML file with a base64 encoded attribute, and it won't be interpreted as XML by.. which application? Perhaps the XML result is invalid, or you forgot to add a HTTP Content Type, or have a wrong encoding? Does it work with smaller attachments? – Code4R7 Dec 21 '18 at 12:39
  • @Code4R7, I am creating xml with SimpleXMLElement() function successfully. I need to add pdf content(base64 encoded) with the xml. – Deepak Dholiyan Dec 21 '18 at 12:43
  • Base64 encoded content creating issue. On removing this tag this is working fine. – Deepak Dholiyan Dec 21 '18 at 12:44
  • Me too, how do you open/interpret your xml ? HTTP, javascript... have nothing to do with XML, it is not the question. There is character issue on the encoding that break XML I think. Have you try to validate it ? https://www.xmlvalidation.com/ – Shim-Sao Dec 21 '18 at 12:46
  • You set me on the wrong track by mentioning that the PDF was too big and it (therefore?) caused a problem, and you also mentioned that you weren't able to download something. As the XML appears as plain text the most likeliky result is that the XML markup is invalid, cause by that one line of code. Have you checked if the output XML is valid, by means of an XML validator? – Code4R7 Dec 21 '18 at 12:49
  • @Code4R7, I mean base64 encoded text creating issue with XML. I try to validate xml but showing some error because of attchment tag text. Error:- The processing instruction target matching "[xX][mM][lL]" is not allowed. – Deepak Dholiyan Dec 21 '18 at 12:56
  • All the explanation for this error here : https://stackoverflow.com/questions/19889132/error-the-processing-instruction-target-matching-xxmmll-is-not-allowed – Shim-Sao Dec 21 '18 at 13:19

0 Answers0