How to open the PDF generated in new tab - php

I have a Leave application project that generates a PDF file.
this is the code that generates the pdf after I click submit.
$id = DB::table('leave')->where('user_id', auth()->user()->id)->max('id');
$data = leave::where('id', $id)->select('*')->get();
$filename = auth()->user()->name." - S - ". "LEAVE - ".$randomString->randomString(8)."-".$request->filing_date.".pdf";
$path = storage_path('app/leave_file/'.$filename);
PDF::loadView('pdf_views.leave_pdf', $data[0])->setPaper('legal')->save($path);
if(file_exists($path)){
DB::table('leave_files')->insert([
'filename' => $filename,
'leave_id' => DB::table('leave')->where('user_id', auth()->user()->id)->max('id'),
]);
}
return redirect()->route('leave')->with('success', "success");
I wonder if I can make the newly generated pdf open in the new tab after I click submit on my leave application. thank you to anyone who can help

According to the Laravel docs this is what you need: https://laravel.com/docs/9.x/responses#file-responses
File Responses
The file method may be used to display a file, such as an image or PDF, directly in the user's browser instead of initiating a download. This method accepts the path to the file as its first argument and an array of headers as its second argument:
return response()->file($pathToFile);
return response()->file($pathToFile, $headers);

Use the Cache Facade, to store the generated pdf temporary.
$uuid = Str::uuid();
return response()->stream(function () use ($uuid) {
echo Cache::remember($uuid, 300, function() {
return 'genertated pdf content'
});
}, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => sprintf('attachment; filename=%s;', $uuid),
]);
Create a route to access to cached file. Name the route.
Now you can open it in a new tab from you frontend e.g. Generate the route with route('routename', ['param' => 123]) and pass it to your FE with a REST endpoint for example.
window.open(url,'_blank');

Related

OneNote error creating a page in Laravel using Microsoft Graph

i'm using Microsoft Graph in my Laravel application.
It works perfectly adding events to the user's calendar, getting events, creating OneNote's notebook.
The problem occurs when i try to add a page, i'm getting the next error:
The code i'm using is:
public function createNewNote()
{
$graph = $this->getGraph();
// Build the event
$newEvent = [
'body' => [
'content' => 'New page in default notebook',
'contentType' => 'text/html'
]
];
// POST /me/onenote/notebooks DEFAULT NOTEBOOK
$response = $graph->createRequest('POST', '/me/onenote/pages')
->attachBody($newEvent)
->setReturnType(Model\Notebook::class)
->execute();
dd("Success");
// return redirect('/calendar');
}
The next code works fine:
public function createNewNotebook()
{
$graph = $this->getGraph();
$newEvent = [
'displayName' => 'Creating new notebook'
];
// POST /me/onenote/notebooks DEFAULT NOTEBOOK
$response = $graph->createRequest('POST', '/me/onenote/notebooks')
->attachBody($newEvent)
->setReturnType(Model\Notebook::class)
->execute();
dd("Notebook Created");
// return redirect('/calendar');
}
I can't find the exact array or json structure for the body. What am i doing wrong? I also tried setting the text directly:
public function createNewNote()
{
$graph = $this->getGraph();
// POST /me/onenote/notebooks DEFAULT NOTEBOOK
$response = $graph->createRequest('POST', '/me/onenote/pages')
->attachBody('<div>Content</div>')
->setReturnType(Model\Notebook::class)
->execute();
dd("Success");
// return redirect('/calendar');
}
And getting this error:
[![enter image description here][1]][2]
As i said i can create events (calendar) and notebooks (onenote) but i can't add a page with simple html or text.
Thank you so much for your help!
As specified in the document since you are putting the HTML data in attachBody directly without specifying the content type.
That's the reason the other API calls work as they have JSON data specified properly.
As specified here in the example they have specified the content type.
I have tested the same with POSTMAN as shown below where you can see the content-type I have mentioned as text/html as the content we give is a div tag and it worked.

How to add a redirect from the backend controller in shopware

I have created a plugin and after the other processes of the plugin are done i would like to redirect to a given url from a controller in the backend.
i have created a plugin that creates a documents from orders and that is working fine. however at the end of the process i would like to redirect to a url that can download or open the document that has been created. i know the url for doing so is structured like this (http://localhost:8000/backend/Order/openPdf?id=harshvalueforpdf). i am using shopware version 5.5.1 in docker on my local host.
public function redirectmyurlAction()
{
$harsh = "9ce6b9a9cd5d469386fbb5bd692f9644";
$search_word = $harsh;
error_log(print_r(array('Reached redirect action'), true)."\n", 3, Shopware()->DocPath() . '/test.log');
$this->redirect(
array(
'module'=> backend,
'controller' => 'Order',
'action' => 'openPdf?id='.$search_word,
)
);
}
i expect that when the process reached this action the user is redirected to the created url and then it should be able to download or show the pdf. But it logs the log i put before the redirect but does not redirect. nothing is logged in errors or console. When i put the same redirect on the frontend i get the CSRFTokenValidationException which is what i expect, but it shows the redirect works there so why not in the backend.
Update:
After the responses,i have copied the function and modified it as below but it logs everything there and still does nothing am i missing something?
public function openmyPdf($DocHarsh, $orderId)
{
error_log(print_r(array('Entered openmyPdf function',$DocHarsh,$orderId,$date), true)."\n", 3, Shopware()->DocPath() . '/error.log');
$filesystem = $this->container->get('shopware.filesystem.private');
$file = sprintf('documents/%s.pdf', basename($DocHarsh));
if ($filesystem->has($file) === false) {
error_log(print_r(array('Entered if statement, file doesnt exists ',$DocHarsh,$orderId,$date), true)."\n", 3, Shopware()->DocPath() . '/error.log');
$this->View()->assign([
'success' => false,
'data' => $this->Request()->getParams(),
'message' => 'File not exist',
]);
return;
}
// Disable Smarty rendering
$this->Front()->Plugins()->ViewRenderer()->setNoRender();
$this->Front()->Plugins()->Json()->setRenderer(false);
$orderModel = Shopware()->Models()->getRepository(Document::class)->findBy(['hash' =>$DocHarsh]);
$orderModel = Shopware()->Models()->toArray($orderModel);
$orderId = $orderModel[0]['documentId'];
$response = $this->Response();
$response->setHeader('Cache-Control', 'public');
$response->setHeader('Content-Description', 'File Transfer');
$response->setHeader('Content-disposition', 'attachment; filename=' . $orderId . '.pdf');
$response->setHeader('Content-Type', 'application/pdf');
$response->setHeader('Content-Transfer-Encoding', 'binary');
$response->setHeader('Content-Length', $filesystem->getSize($file));
$response->sendHeaders();
$response->sendResponse();
$upstream = $filesystem->readStream($file);
$downstream = fopen('php://output', 'wb');
while (!feof($upstream)) {
fwrite($downstream, fread($upstream, 4096));
}
error_log(print_r(array('leaving the pdf function',$DocHarsh,$orderId,$upstream,$downstream), true)."\n", 3, Shopware()->DocPath() . '/error.log');
}
Please have a look at backend-controller of the order module. It should be the same case. This function is used for opening/downloading a document from the backend:
https://github.com/shopware/shopware/blob/5.5/engine/Shopware/Controllers/Backend/Order.php#L1113
I think it might be confusing for backend users to be redirected (from the backend context) to a new blank page with a download.
According to my own evaluation. I think the issue you are having is because this is not an action but simply a function try making it an Action and run it through the browser like the original one.
Don't forget to whitelist it.
use the class use Shopware\Components\CSRFWhitelistAware;
then
something like this
/**
* {#inheritdoc}
*/
public function getWhitelistedCSRFActions()
{
return [
'youropenPdfActionnamewithoutthewordAction'
];
}
and also add the implements CSRFWhitelistAware to your class declaration.

Maps image download error, routes are not coming in the image

I created a function in PHP that downloads a map created in google maps, I am able to download, however, the route is not coming when I am saving.
How the picture is coming:
How it should come:
Code:
function download_remote_file_with_curl($file_url, $save_to) {
$arrContextOptions = array(
"ssl" => array(
"verify_peer" => false,
"verify_peer_name" => false,
),
);
$response = file_get_contents($file_url, false, stream_context_create($arrContextOptions));
//echo $response;
//file_put_contents($save_to, $response);
$downloaded_file = fopen($save_to, 'wb');
fwrite($downloaded_file, $response);
fclose($downloaded_file);
}
$urlDownload = "https://maps.googleapis.com/maps/api/staticmap?size=1000x1000&maptype=roadmap&path=enc:zklaAbqcyJzCmF~#b#fBdAzCiEnKyQlFpCnMdH~_#nU~JlFlCNrw#oL~IcApEZxd#nQpgBhq#tRtStgA``Aty#`o#ll#pa#tNhFzMvJfG~D|Y~Hxd#rMll#`Rtb#jLjNzExWtFz`#bJvM|ItG`LdF|V`GvD|CL|JiBv]aGld#cHrk#cLdU_Fpb#wGdMg#vaAqAti#g#z_#Cj~A{#raAfDlp#xAfYa#tyAuDtVEbTdApO`#rGk#dP`#vgEo#baCuA`|AaArHGIzAwCtHqDlNf#rl#lA~bAnAphAMj`#hBlj#\hd#s#tL\pHnCff#~A`|#lMnmEnBbKrFbDn`#jP~RtQ~k#hVhNtGb#xFgPheAsAnOuIlaBpj#|qCtDzGb`A~`AzDvGd#vI{Af_#x#~GxItHzLvGp\fLxc#t\pe#d`#h|AnmA`WvQnEDnF}Bd\uUjGeB~Gb#nEvB~KnMri#ni#fjCbfCpmArkAv|#pz#bjAjhApcEt}Ddx#pv#r_#lZzyB|hBtoHneGzZvVh|#xq#fb#jZh^fZjD~E`FlOzMbc#|L|`#hQrS~H`MhQ~w#hEdzB~B~hAbAbk#Ux^kNx~BeGriAaCbe#|#zSt#`ChDnBnZlAlFtCl`#`AzMpBpRvJlKvFbKlLxd#pcAdk#~oAjbAbiAnPxLfq#b^ndDpkBbdDfjBbqAdt#hFjAtNhCfMbD|g#tYhxDfxBrqFx~C|CpCzA|EE~E}i#raDsOb}#uO~{#{f#zvCr#hG~CdDf`Bly#hL|Lt#bClD|Brx#naAvAnAzb#lg#zj#veDjMbI|rBhdAlJzFdAjCbBpgBpMlsQdIhhO]p^?hb#z#dsAp#jo#l#pWffDp`FzyB~eDxdEhlGpNrSvv#hn#duEvuDnsJp~HtpLvvJ~bBtvAjw#hp#hDnFnHvpBlSh}FrFd_BvAno#tu#buTtPxwEnXvhJbRddGbQ~nGlNjqFlJv~BvC~rAdFppBvNhvEhShsGw#nQ}i#htBsq#lgCcJd]}BjPkG|y#gPptBu]byE_H~gArB`EfG|FxXnYfv#nv#nWhX`RjRjHj`#bfBdgKlCrKzGhQp`#lbAtdBnjEr_#z`A`~#n}Bth#fuAdpDzdJ|W`o#`ClAxIj#beA~A~p#bAx|F~I`lEzGvwAbCdMx#neDjlAbp#hUrE}#prB_qCzBaAjJAxOKhWfAfP?tm#}Q~{Bkr#r{EeyAxPoApfB~WzK|AHTdBDrO}A~RmBrXsCzZuA~NJx#bD|#dAfX`#{#nG&markers=color:green|label:A|-10.8819552,-61.95483539999998&markers=color:red|label:B|-12.457280801085,-64.2310285267651&key=yourkey";
download_remote_file_with_curl($urlDownload, realpath('C:/test') . '/' . 'test' . '.jpg');
?>
Thank you!
I don't know if what I'm about to write makes sense, but the route that you want to download is an item that appears once it's selected.
For example when the "SKIP" buttom in Youtube appears to quit an advertisement, it creates an instance of that box in HTML, and then it dissappears when Add is over.
I think that the route is this kind of instance and you must add a line of code to also take that piece of html

Laravel Excel download not working

The problem is the same as described here Laravel Excel Download using Controller
But I just can not believe that there is no method to deal with Excel downloads in Laravel without using another resource. I was already able handle the instant downloads in controller with response() for PDFs.
Mabybe the headers are wrong? My code:
public function getFile($file) {
$path = storage_path('app/excel/exports/' . $file);
$headers = array('Content-Type' => File::mimeType($path));
return response()->download($path, $file, $headers);
}
So the excel file is created and saved correctly in my storage folder (happens before the code above). Then I use an axios.get method to download the file with the function above.
Headers I am getting:
Accept-Ranges:bytes
Cache-Control:public
Connection:keep-alive
Content-Disposition:attachment; filename="test_file.xlsx"
Content-Length:7066
Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
But whatever I do or try to change the download just won't start.
You can try with these two headers.
return response()->download($path, $file, [
'Content-Type' => 'application/vnd.ms-excel',
'Content-Disposition' => "attachment; filename='Report.xls'"
]);
Thanks,
I manged to solve it.
It seems to be not possible to solve this with a simple axios.get request to the route. Instead I needed to open the route directly with a link.
Did not work with:
HTML
<button #click="downloadExcel()">Download Table as Excel File</button>
downloadExcel() {
axios.get('/customers/export');
}
Simple solution (instead of just using axios.get):
<a :href="/customers/export"><button>Download Table as Excel File</button></a>
So it would also be possible to open the route after the axios request with:
downloadExcel() {
let newWindow = window.open();
axios.get('/customers/export')
.then(response => {
newWindow.location = 'http://' + window.location.hostname + '/customers/export';
});
}

How to download a file from URL without showing the full path in laravel?

Download link:-
<a href='"+ downloadlink + '/attachment-download/' + $('#employee_ID').val()+'/'+ res[i].file_name +"'>
Route:-
Route::get('/attachment-download/{id}/{filename}', array(
'uses' => 'AttachmentsController#getDownloadAttachments'
));
Attachment Controller:-
public function getDownloadAttachments($id,$filename){
$file="./img/user-icon.png";
$resource = \Employee::WhereCompany()->findOrfail($id);
$path = $this->attachmentsList($resource);
foreach($path as $index => $attachment){
if ($filename == $attachment['file_name']) {
$filePath = $attachment['url'];
}
}
//return \Response::download($file);
return \Response::download($filePath);
}
File URL Output:-
https://zetpayroll.s3.ap-south-1.amazonaws.com/employees/81/Screenshot%20from%202017-04-26%2015%3A07%3A45.png?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAI57OFN3HPCBPQZIQ%2F20170612%2Fap-south-1%2Fs3%2Faws4_request&X-Amz-Date=20170612T144818Z&X-Amz-SignedHeaders=host&X-Amz-Expires=3600&X-Amz-Signature=59ecc4d11b7ed71bd336531bd7f4ab7c84da6b7424878d6487679c97a8c52ca7
In this, if try to download the file by using a static path like
$file="./img/user-icon.png";
return \Response::download($file);
it is downloaded fine. But not possible to downloading file from AWS URL, Please help me how to down file automatically using URL. Or How to get Path from URL in laravel or PHP.
Thank you.
Using the above function all the files are being downloaded. But while trying to open the files, text, pdf, ... files open (.text, .csv, .pdf..) without problem, but images don't.
$fileContent = file_get_contents($filePath);
$response = response($fileContent, 200, [
'Content-Type' => 'application/json',
'Content-Disposition' => 'attachment; filename="'.$filename.'"',
]);
return $response;

Categories