how to view docx, xls, xlsx in browser using laravel? - php

I am using the below code. but still it's downloading the files instead of viewing.
public function viewFiles(StoreBusinessDevelopment $request, $file, $id)
{
$businessDevelopment = BusinessDevelopment::select('created_by', 'rfp_id')
->where('id', '=', $id)
->get();
$mimeType = File::mimeType(public_path('/uploads/'.$businessDevelopment[0]['created_by'].'/'.$businessDevelopment[0]['rfp_id'].'/'.$file));
return response()->file(public_path('/uploads/'.$businessDevelopment[0]['created_by'].'/'.$businessDevelopment[0]['rfp_id'].'/'.$file),[
'Content-Type' => $mimeType
]);
}

You can just convert files to PDF format and then return it to browser.

Browser unable to read .doc|xls|xlsx files. But you can view / rander it by using some tricks.
Example 1
First upload the file like i say .doc|xls|xlsx and make it as array of data. By using php -> laravel that have a module/repo for excel file read and parse it as array.
After make it as array and print this array as html table and browser must rander it.
For more info :
Laravel Excel

Related

larave convert arabic php to pdf by tcp pdf

I am trying to convert an html file that contains the Arabic language to pdf and I used tcp pdf
public function generatePdf(){
$users = User::all();
$reportHtml = view('pdf.users', ['users' => $users])->render();
return PDF::HTML($reportHtml);
}
give me this error
"message": "Symfony\\Component\\HttpFoundation\\Response::setContent(): Argument #1 ($content) must be of type ?string, Barryvdh\\Snappy\\PdfWrapper given, called in C:\\Users\\amrel\\Desktop\\topArt\\vendor\\laravel\\framework\\src\\Illuminate\\Http\\Response.php on line 72",
You can't return pdf file directly. Try either
Option #1. Return response in application/pdf type:
return Response::make(PDF::HTML($reportHtml), 200, [
'content-type'=>'application/pdf',
]);
Option #2. From Laravel 5.2 you can return file in storage/ by just:
return response()->file($pathToFile);
So you can save it to your storage and then return.
Option #3. You can send your pdf as downloadable file.
$pdf = PDF::HTML($reportHtml);
return $pdf->download('name.pdf');
Option #4. You can save pdf file to public storage and send url link to it and let the frontend decide.

Laravel - Create PDF file from raw content - JSON API

I am trying to create a temporarily PDF file from raw PDF string.
This is my input, which is sent through an API (JSON):
{
"name": "pdffilename.pdf",
"content": "%PDF-1.2 [.......]%%EOF"
}
The "content" string is the actual PDF raw data string.
Now this is my controller, that handles the API request:
/**
* Function to convert a PDF file to text
*/
public function PDFtoText(Request $request)
{
$name = $request->name;
$content = $request->content;
//Save PDF file on the server (temp files).
$pdf = Storage::disk('local')->put('/temp_files/' . $name, $content);
return response()->json([
'result' => "Success"
], 200);
}
An actual file is created in the temp_files folder, with the name pdffilename.pdf. However I cannot open the file, as it says the file is "corrupt".
What am I doing wrong here?
the best way to save the content of the document or file is using the base64_encode function, this transform de content in base64 encode. Then when you need this content the unique process you need to do is to call the base64_decode function. I tried and works fine for me. Any questions only write me.

Laravel Excel - Easiest way to include images from records?

I'm using Laravel Excel to dump a number of records to XLS format.
Each of my records has an image_url field that contains a remote URL to a JPG file.
I'd like to display these images in the final XLS file, in their own column.
I tried creating a Blade template for the export, but when using <img> tags, the images don't appear in the XLS file. I'm not getting any errors, and the documentation for Laravel Excel is very sparse when it comes to image integration.
So what would be the easiest way to get the images to display in the XLS, using Laravel Excel? Is there some way I can get the Blade template to work, or must I add the images more programmatically?
something like this should work, you gotta load the PHPExcel_Worksheet_Drawing library.
$filename = 'File Name';
Excel::create($filename, function($excel) {
$excel->sheet('sheet name', function($sheet) {
$objDrawing = new PHPExcel_Worksheet_Drawing;
$objDrawing->setPath(public_path('your/image/path.jpg')); //your public image path
$objDrawing->setPath(storage_path('your/image/path.jpg')); //your storage image path
$objDrawing->setCoordinates('A2');
$objDrawing->setWorksheet($sheet);
});
})->export('xls');

How to validate file types in Laravel

I am uploading a file on Laravel, and its validation doesn't seem to be working.
My controller code is as follows:
public function cv(Request $request) {
if ($request->hasFile('cv')) {
$file = $request->file('cv'); //i need this later to upload the file
$rules= [
'cv' => 'mimes:application/msword,text/plain,application/pdf,application/vnd.openxmlformats-officedocument.wordprocessingml.document'
];
$x = $request->all();
$validator=Validator::make($x, $rules);
if ($validator->passes()){
dd("Passes");
}else{
dd("fails");
}
}}
My html code for my file is: <input type='file' name='cv'><br>
For some reason this code seems to fail, and the validator passes only if the file is a pdf. I only want the user to upload pdf files, doc files, docx files, amd text/plain files. When I attempt to upload a microsoft doc file, it fails. Could someone help me out, I am unsure what I am doing incorrectly.
I think you reversed params
$validator=Validator::make($x, $rules);
Maybe try this instead:
'cv' => 'mimes:pdf'
https://laravel.com/docs/5.5/validation#rule-mimes
As per the documentation, mimetypes should be used when specifying the mime types as you are, whereas mimes should be used to specify file extensions. HTH!

Laravel Download from S3 To Local

I am trying to download a file that I stored on S3 to my local Laravel installation to manipulate it. Would appreciate some help.
I have the config data set up correctly because I am able to upload it without any trouble. I am saving it in S3 with following pattern "user->id / media->id.mp3" --> note the fact that I am not just dumping files on S3, I am saving them in directories.
After successfully uploading the file to S3 I update the save path in my DB to show "user->id / media->id.mp3", not some long public url (is that wrong)?
When I later go back to try and download the file I am getting a FileNotFoundException at S3. I'm doing this.
$audio = Storage::disk('s3')->get($media->location);
The weird thing is that in the exception it shows the resource that it cannot fetch but when I place that same url in a browser it displays the file without any trouble at all. Why can't the file system get the file?
I have tried to do a "has" check before the "get" and the has check comes up false.
Do I need to save the full public URL in the database for this to work? I tried that and it didn't help. I feel like I am missing something very simple and it is making me crazy!!
Late answer but important for others,
$s3_file = Storage::disk('s3')->get(request()->file);
$s3 = Storage::disk('public');
$s3->put("./file_name.tif", $s3_file);
The response of $s3_file will be a stream, you can save that stream data to file using Laravel put file method, you will find this stream file in storage/public directory.
You can give your Content-Type as desired and Content-Disposition as 'attachment' because your files are coming from S3 and you have to download it as an attachment.
$event_data = $this->ticket->where('user_id', $user_id)->first();
$data = $event_data->pdf;
$get_ticket = 'tickets/'. $data;
$file_name = "YOUR_DESIRED_NAME.pdf";
$headers = [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="'. $file_name .'"',
];
return \Response::make(Storage::disk('s3')->get($get_ticket), 200, $headers);
Say, you have AWS S3 as your default storage.
And you want to download my_file.txt from S3 to my_laravel_project\storage\app\my_file.txt
And you want to make it a one-liner
Storage::disk('local')->put('my_file.txt', Storage::get('my_file.txt'));

Categories