skip to Main Content

I have XML in a base64Encoded string. My requirement is to attach base64Encoded xml into pdf. I am using the following example, which is getting XML from a file, but I need to first decode base64Encoded xml then attach it with a pdf

$mpdf = new MpdfMpdf([
    '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();

The above example works fine if we pass XML file path, but I have XML in base64 encoded string. How can we attach base64Encoded xml into pdf using Mpdf library in Laravel/PHP?

2

Answers


  1. You can attach a base64-encoded XML string as a file in a PDF using the Mpdf library like the following.

    $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 MpdfMpdf([
        'PDFA' => true,
        'PDFAauto' => true,
    ]);
    
    $mpdf->SetAssociatedFiles([$associatedFile]);
    $mpdf->WriteHTML('<h1>Hello world 1!</h1>');
    
    return $mpdf->Output();
    
    Login or Signup to reply.
  2. To attach data contained in a string, you need to use the content entry:

    $mpdf->SetAssociatedFiles([[
        'name' => 'phpunit.xml',
        'mime' => 'text/xml',
        'description' => 'some description',
        'AFRelationship' => 'Alternative',
        'content' => 'your file content'
    ]]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search