Remove header from base64 string in PHP - Hack The Tech - Latest News related to Computer and Technology

logo

Get Daily Latest News related to Computer and Technology and hack the world.

Saturday, February 19, 2022

demo-image

Remove header from base64 string in PHP

I'm using TCPDF using

$base64String = $pdf->Output('file.pdf', 'E');

So I can send the data via AJAX

The only problem is that it comes with header information in addition to the Base64 string

Content-Type: application/pdf;
 name="FILE-31154d59f28c63efae86e4f3d6a00e13.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="FILE-31154d59f28c63efae86e4f3d6a00e13.pdf"

So if I take the string that is created to base64_decode() or use with phpMailer in my case it errors. Is it possible to remove the headers so I only have the base64 string?

(The error is that the pdf can't be read by any PDF reader when opened)

I thought I'd be able to find something that solves this but I haven't found anything!!

UPDATE

This is what I've put in place to solve the issue

$base64String = preg_replace('/Content-[\s\S]+?;/', '', $base64String);
$base64String = preg_replace('/name=[\s\S]+?pdf"/', '', $base64String);
$base64String = preg_replace('/filename=[\s\S]+?"/', '', $base64String);

However it's not very elegant! So if anyone has a better solution please post it below :)



source https://stackoverflow.com/questions/71167041/remove-header-from-base64-string-in-php

No comments:

Post a Comment