使用 mPDF 将 Base64 编码的 XML 嵌入/附加到 PDF 中

问题描述 投票:0回答:1

我有一个 base64Encoded 字符串中的 XML。我的要求是将base64Encoded xml附加到pdf中。我使用以下示例,该示例从文件中获取 XML,但我需要首先解码 base64Encoded xml,然后将其附加到 pdf

$mpdf = new \Mpdf\Mpdf([
    'PDFA' => true,
    'PDFAauto' => true,
]);

$mpdf->SetAssociatedFiles([[
    'name' => 'phpunit.xml',
    'mime' => 'text/xml',
    'description' => 'some description',
    'AFRelationship' => 'Alternative',
    'path' => base_path() . '/phpunit.xml'
]]);

$mpdf->WriteHTML('<h1>Hello world 1!</h1>');

return $mpdf->Output();

如果我们传递 XML 文件路径,上面的示例可以正常工作,但我有 Base64 编码字符串中的 XML。我们如何使用

Mpdf
中的
Laravel/PHP
库将base64Encoded xml附加到pdf中?

php pdf mpdf
1个回答
0
投票

您可以使用 Mpdf 库将 Base64 编码的 XML 字符串作为 PDF 中的文件附加,如下所示。

$base64EncodedXml = "a_base64_encoded_xml_string";
$decodedXml = base64_decode($base64EncodedXml);

$associatedFile = [
    'name' => 'your_xml_file.xml',
    'mime' => 'text/xml',
    'description' => 'XML File Description',
    'AFRelationship' => 'Alternative',
    'data' => $decodedXml,  // Attach the decoded XML content
];

$mpdf = new \Mpdf\Mpdf([
    'PDFA' => true,
    'PDFAauto' => true,
]);

$mpdf->SetAssociatedFiles([$associatedFile]);
$mpdf->WriteHTML('<h1>Hello world 1!</h1>');

return $mpdf->Output();
© www.soinside.com 2019 - 2024. All rights reserved.