如何将 pdf 文档作为代码添加到 xml 文件 (PHP)

How to add pdf document as code in xml file (PHP)

我正在分别为发票创建 pdf 和 xml 文件。

但现在根据新规则,我需要在 xml 文件 中发送 pdf 文档作为代码(base64Binary [RFC 2045])。

在PHP中,我正在使用SimpleXMLElement()函数。

我应该使用 xml 中的编码 pdf 作为标签(附件)中的 taxt 吗?

您可以抓取文档内容并转换为 base64 并放入您的 XML 标签

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

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

接近它的东西。

$file = "file.pdf";


$content = file_get_contents($file);

$base64 = base64_encode($content);

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

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

print $xml->asXML();

你可以尝试类似的东西

$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);

解码 pdf 的代码。

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