Generate PDF with base64 encode string

18.4k views Asked by At

I am using an API which provides me base64 encoded content. It is mentioned that we can download the PDF file by saving the content to a .pdf file.

In the API documentation, it's clearly mentioned

A PDF that contains the contents of the invoice. You encode the content with a Base64 string to create a Base64-encoded .pdf file.

You can query this field to download the invoice PDF for a specific invoice. After you query it, you can save the string to a .pdf file, then view the file in any PDF reader.

I am using TCPDF to write the content in PDF file. But it generates a blank PDF file.

$PDF = new PDF('Test Title', 'Test Subject');

$PDF->GetTCPDF()->write(0, 0, '', '', base64_decode($this->Get(self::Body)), 0, 1, 0, true, '', true);

$PDF->Download();

Am I doing anything wrong?

2

There are 2 answers

0
M2sh On BEST ANSWER

I think you should use this code to get your pdf file :

$data = base64_decode($this->Get(self::Body));
file_put_contents('mypdf.pdf',$data);

Then you can open it.

Or you can echo the content to your page like this :

$data = base64_decode($this->Get(self::Body));
header('Content-Type: application/pdf');
echo $data;

Good luck

0
Avian On

The last time I get a blank PDF I forgot adding a new Page with AddPage(). Apart from this I think M2sh is right.