PHP Get PDF File from Base64 Encoded Data String

PHP get pdf file from base64 encoded data string

Try this piece of code

$pdf_base64 = "base64pdf.txt";
//Get File content from txt file
$pdf_base64_handler = fopen($pdf_base64,'r');
$pdf_content = fread ($pdf_base64_handler,filesize($pdf_base64));
fclose ($pdf_base64_handler);
//Decode pdf content
$pdf_decoded = base64_decode ($pdf_content);
//Write data back to pdf file
$pdf = fopen ('test.pdf','w');
fwrite ($pdf,$pdf_decoded);
//close output file
fclose ($pdf);
echo 'Done';

Download PDF from Base64 string

The example here seems helpful: http://php.net/manual/en/function.readfile.php

In your case:

<?php
$decoded = base64_decode($base64);
$file = 'invoice.pdf';
file_put_contents($file, $decoded);

if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>

This should force the download to occur.

Convert Base64Binary to PDF using PHP

$data = base64_encode($contents);
file_put_contents('output.pdf', base64_decode($data));

PHP - Possible to take a base64 encoded pdf data string and compress it?

Ok, so I figured it out.

Imagick was the way to go, and my use of it was good. I just goofed up on the file name because I wasn't using a proper dynamic variable name. Code should have looked like this:

CODE

$imageBlob = base64_decode((string)$pdf->PDFBytes);
${'imagick'.$x} = new Imagick();
${'imagick'.$x}->readImageBlob($imageBlob);
${'imagick'.$x}->setImageFormat('jpeg');
${'imagick'.$x}->setImageCompressionQuality(60);
${'imagick'.$x}->adaptiveResizeImage(1024,768,true);
$imageBlob = ${'imagick'.$x}->getImageBlob();
$PDFdata[] = base64_encode($imageBlob);
$PDFfile[] = $FormCustomField . $x . '.jpg';

So the error I was getting was because of an invalid file name, because the $x variable in the previous code was getting junk values. Now everything works fine.

PHP base64 encode a pdf file

base64_encode takes a string input. So all you're doing is encoding the path. You should grab the contents of the file

$b64Doc = chunk_split(base64_encode(file_get_contents($this->pdfdoc)));

How to encode pdf file in base64 format?

      $query1 = mysqli_query($con, "SELECT  `offer_id` ,  `offer_img_url` ,  `terms` FROM  `offers` LEFT JOIN system ON system.shop_id = offers.shop_id LEFT JOIN bill ON bill.sys_id = system.sys_id WHERE yb_id =  '$yb_id' LIMIT 0 , 30 ");
if(mysqli_affected_rows($con)>0)
{
$data4 = array();
while ($row = mysqli_fetch_array($query1))
{
$data4['offer_id'] = $row['offer_id'];
$data4['offer_img_url'] = base64_encode($row['offer_img_url']);
$data4['terms'] = $row['terms'];
$data2[] = $data4;
}

Base64 to PDF in PHP - Getting corrupted file

try to remove "data:application/pdf;base64,"



Related Topics



Leave a reply



Submit