So I want to add an header image to my document in PHPWord in Laravel.
So this is my code
public function generateDocx()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$headerLogo = 'http://127.0.0.1:8000/img/logoAnevBulanan.png';
$section->addImage($headerLogo);
// Bunch of line to download the docx
}
And I got Maximum execution time of 60 seconds exceeded, when I try the other method from the documentation, I still got the same error. I try to use asset() helper from laravel and still did not work
Try getting your image using local path instead of URL
$source = file_get_contents('/path/to/my/images/earth.jpg');
$textrun->addImage($source);
Refer to documentation : https://phpword.readthedocs.io/en/latest/elements.html#images
Related
I am using dompdf with laravel. Wheni have to make PDF, i write the below code in controller.
public function download()
{
$courseoffers = Courseoffer::all()->get();
$pdf = PDF::loadView('admin.courses.pdf', compact('courseoffers'))->setPaper('a4','landscape');
return $pdf->download('courseoffer.pdf');
}
Here, pdf.blade.php is a specific file. But i don't want to use that. i want a specific portion of blade file to make pdf. How do i achieve it.?
RuntimeException This driver does not support creating temporary URLs.
I am trying to generating Temp Url for every request Laravel version 5.8 below code trying showing an error.
This driver does not support creating temporary URLs.
$url = "66.jpeg";
$url = Storage::disk('public')->url($url);
$url = Storage::disk('public')->temporaryUrl(
'66.jpeg', now()->addMinutes(5)
);
From my knowledge, temporaryUrl is a method used on a drivers such as s3 to create a temporary url for a privately stored asset.
If you would like to set a temporary url for a file, it may help to use Cache to temporarily store the path.
Cache can set a key/value for a set amount of time. A url can be create which links to an endpoint. Then endpoint can then be created which returns the contents of that file:
// Creating temp file index in cache
$image = '66.jpg';
Cache::put('/temp/' . $image, 300); // 5 minutes
Now in, for example, TempController.php (visiting http://example.com/temp/66.jpg):
public function show($image)
{
if (Cache::get('/temp/' . $image) && ! Storage::disk('public')->exists($image)) {
// not in cache or do not exist, maybe redirect...
};
return Storage::disk('public')->get($image);
}
This is a proof of concept however I hope this helps.
I'm trying to upload an image file . When i am calling my API to upload file using postman , getting error like Fatal error: Call to a member function file() on array.
I could not post this file to my Laravel controller .How to post files in Laravel. Anyone could help me to solve this issue?
Here is my controller function,
public function edit(Request $request){
$request = $request->input();
if(empty($request)) {
$request = json_decode(file_get_contents('php://input'),true);
}
$to_return = array();
$file = $request->file('files');
}
at this line $file = $request->file('files'); getting fatal error.
set method as post .. and at body ... change to option from form data to w-xxx-formurlencod ... and try ..
If you want to file uploads with Postman and Laravel, simply remove the Content-Type header setting in Postman.
set method as post
and at body ... select the radio that shows url encoded
see an image ... click on text set it as flie
When I want upload file with form in laravel, I cant remove file value from request.
This is my full code
if($request->hasFile('image')){
$string_name = str_random(12);
$image = $request->file('image')->move(getcwd().'/image/original',$string_name.'.'.$request->file('image')->getClientOriginalExtension());
$request->request->remove('image');
}
if($request->hasFile('thumbnail')){
$string_name = str_random(12);
$thumbnail = $request->file('thumbnail')->move(getcwd().'/image/thumbnail',$string_name.'.'.$request->file('thumbnail')->getClientOriginalExtension());
}
Portfolio::create($request->all());
but image or thumbnail file do not remove from $request. This means this line of code not working :
$request->request->remove('image');
I've tried many ways but the file does not get removed from the request.
Instead of removing the image from your $request before saving, you can explicitly mention what you would like to save to your model by using the ->only([]) method on $request.
Portfolio::create($request->only(['title', ...]));
This will allow you to specify exactly what you would like saved from the $request data.
You can do the reverse and use the ->except() method to remove the image too:
Portfolio::create($request->except('image'));
use this to remove the file
$request->offsetUnset('input_file_name');
or
$request->except('filename');
Hello I am new to laravel and still learning it and I come across different problems and issues and I cant find a way to solve it. I have been stuck to this since very long. Any help would be much appreciated.
The Problem
Whenever I upload a .docx file to my website i get a blank page without any error or without the content of that docx file. I dont know what the problem is. Please help me out with this.
Code
public function getUploadedFile() {
// $destinationPath = 'uploads';
// $path= public_path()."/". $destinationPath;
// $content = utf8_encode(File::get('/var/www/html/asad/File-Uploader/public/uploads/detailed_period_report_template.xls'));
// return view('/files', compact('path'));
$file = $this->request->file('file_name');
$file_name = $this->request->file_name;
if (File::isFile($file_name))
{
$file_name = File::get($file_name);
$response = Response::make($file_name, 200);
$content_types = [
'application/octet-stream', // txt etc
'application/msword', // doc
'application/vnd.openxmlformats-officedocument.wordprocessingml.document', //docx
'application/vnd.ms-excel', // xls
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', // xlsx
'application/pdf', // pdf
];
// using this will allow you to do some checks on it (if pdf/docx/doc/xls/xlsx)
$response->header('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
return $response;
}
I dont have any view for this. Do I need one for this ? If yes what would be it like because every docx file has it's set of properties like font, size, etc unlike excel sheets where we just defines columns. Please help me out. Thank you in advance
I have figured it out after so long finally.
It was sending "null" as a file name so what I did now is got the file name from the database instead of storage and then compared it that if that file is in storage and database then open it. Below is the code running for me now.
Thank you #MATEY for all your help though
$upload = new Upload;
$file= public_path(). "/uploads/" . $upload->file_name = $this->request->file_name;
//dd($file);
$files = File::get($file);
$response = Response::make($files, 200);
and changed the content type to $response->header('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
Thank you for the all help #MATEY. You gave me the motivation to fix it plus your else{...} part missing in if condition actually helped me alot finding out the error.