Return an encoded B64 pdf file - php

I have a String representing a pdf file encoded in base64.
I can decode the string like this:
function get_file_from_b64($b64_file) {
//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);
return $pdf;
}
I would like to create a page that download the file but I don't want to save the file on the filesystem read it in a php page and then send it back to the browser.
There is a way to send back the file directly without saving it on the filesystem?
For example I can send a file back to the browser like this:
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=downloaded.pdf");
readfile("test/file.pdf");
This implies that the decoded file is saved on test/file.pdf.

The readfile command simply echos the content of the file. Instead of doing that echo the variable with your contexts in it.
You will base to base64 decode the PDF if it has been base64 encoded
For example
echo base64_decode($pdf_content);

Related

Word document Base64 encoded string downloads as zip file

I have a base64 encoded string converted with the php function below.
public function convert_image_to_base64($file_path){
$mime = get_mime_by_extension($file_path);
$data = file_get_contents($file_path);
return 'data:'. $mime . ';base64,' . base64_encode($data);
}
I want to download the file in a browser with the below html
Download
When I click, the link downloads a zip file.
This is the start of the resulting base64 string
data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,UEsDBBQABgAIAAAAI
I want to download the exact .docx or .doc file uploaded
I searched on stackoverflow and other forums but none of them address this exact problem. I also don't want to decode the file before submitting the html.
So the problem was the file extension of the downloaded file was not added in the base64 file downloaded. I solved the problem by forcing the name of the downloaded file using the html below from this link. Reference here
Download

How to send binary data over JSON object which I've got by reading PDF file?

I'm reading a PDF file content using php fread function. Now I want to post this data through JSON object. So, how can I send this data(content of PDF file)?
I'm using POSTMAN to send pdf file by selecting binary in header option and getting desired result. But how can I achieve this programatically using PHP.
$readfile = fopen("newfile.pdf", "r") or die("Unable to open file!");
echo fread($readfile,filesize("newfile.pdf"));
fclose($readfile);
Using this fread method I'm getting content of 'newfile.pdf'.
I want to send content of 'newfile.pdf' as a JSON object without base64 encode or decode.

PHP base64 encode a pdf file

I am using an API where I can send a document to something like dropbox. According to the documentation, the file which is sent needs to be BASE64 encoded data.
As such, I am trying something like this
$b64Doc = chunk_split(base64_encode($this->pdfdoc));
Where $this->pdfdoc is the path to my PDF document.
At the moment, the file is being sent over but it seems invalid (displays nothing).
Am I correctly converting my PDF to BASE64 encoded data?
Thanks
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)));
base64_encode() will encode whatever string you pass to it. If the value you pass is the file name, all you are going to get is an encoded filename, not the contents of the file.
You'll probably want to do file_get_contents($this->pdfdoc) or something first.
Convert base64 to pdf and save to server path.
// Real date format (xxx-xx-xx)
$toDay = date("Y-m-d");
// we give the file a random name
$name = "archive_".$toDay."_XXXXX_.pdf";
// a route is created, (it must already be created in its repository(pdf)).
$rute = "pdf/".$name;
// decode base64
$pdf_b64 = base64_decode($base_64);
// you record the file in existing folder
if(file_put_contents($rute, $pdf_b64)){
//just to force download by the browser
header("Content-type: application/pdf");
//print base64 decoded
echo $pdf_b64;
}

How do I generate a pdf-file from a binary file?

How do I generate a pdf-file from a binary file retrieved from database in php5? It comes base64 encoded, and I just decoded it, but don't know what to do next...
The binary data is simply the actual file, or rather the important contents of that file, just without file name.
$base64 = /* some base64 encoded data fetched from somewhere */;
$binary = base64_decode($base64);
And there you have the file data/contents of the file in the $binary variable. From here, it depends on what you want to do. You can write the data to a file, and you get an "actual" PDF file:
file_put_contents('my.pdf', $binary);
You can spit the data out to the browser with an appropriate header, and the user will receive something that looks like a PDF file to him:
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="my.pdf"');
echo $binary;
I'm repeating your last sentence .:) I dont know what is the question! :). If you want to pus the file to a browser, you can set the headers and stream the decoded content. Or if you want the file as is, write on to file system and use it. Please be more clear on your question!
Thanks!!

PHP object-oriented concept in file handling

I want to read content from a text file and download it into a pdf file using PHP object orientation. How I read content from a file, is it same as simple PHP?
The simplest way to get a file’s contents is file_get_contents:
$contents = file_get_contents('filename');
Do you want to know how to get the contents of a file using object-oriented notation, or how to store the contents as an object, or how to move the contents into a pdf?
Assuming the last 2:
//First set the file path and get the contents of the file:
$textfile->path = "path/to/file.txt";
$textfile->contents = file_get_contents($textfile->path);
//Next create the pdf, both as a handler and as a file on disk:
$pdf = PDF_new();
PDF_begin_document($pdf, "file.pdf", "");
//Then put the text file contents into the pdf:
PDF_show($pdf, $textfile->contents);
//Finally, save and close the pdf:
pdf_save($pdf);
pdf_close($pdf);
If you want the script to return the pdf from a request and not save it to the server, simply change "file.pdf" to "" and use the header() function to set the filename.

Categories