Expected requirement - when user clicks button ajax will be call to controller where pdf will generate and generated pdf will gets downloaded on user's system.
Actual Scene - i able to create pdf using fpdf file and saved in folder and used force download function but nothing happend.
here's my code:
Controller :
$this->load->helper('download');
$this->load->library('myfpdf');
$this->load->view('pdf/namewise_report',[]);
$link = base_url().'uploads/pdfs/namewise.pdf';
force_download($link, NULL);
pdf/namewise_report.php (last line)
$this->myfpdf->Output('uploads/pdfs/namewise.pdf', "F");
is there anything to add in ajax success response
Related
I am creating a web application in Codeigniter 4 in which users can purchase a PDF book. After payment, the user is redirected to a page where he/she can download PDF directly. The page doesn't have any UI. The PDF download is implemented using headers. The PDF download function is working perfectly on PC, but on mobile, the file is downloading as an HTML file.
eg : filename.pdf.html.
My function is as below
public function download()
{
$file_url = WRITEPATH . 'uploads/file.pdf';
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=filename.pdf");
readfile($file_url);
}
I have searched and tried almost all the solutions. But still, the issue is not resolved in mobile.
Solved the issue.
Looks like Codeigniter needed exit() function after readfile()
Thanks to #vee who gave the tip.
i've developed laravel MVC web app that has a downloaded video in a Download page so When ever i click on download button it should get me the video downloaded, but instead it opens in a new tab in my browser
here is my code:
Controller download function:
public function download($file_name) {
$file_path = public_path('download/'.$file_name);
return response()->download($file_path);
}
Download button :
Download
I have a PHP file that includes a link to a .pdf file that opens in a new tab - this essentially simply opens the page which downloads the PDF to the user's downloads folder immediately - the new tab doesn't remain open in the browser. This is all working well.
I'm now trying to change it so that the php page will load and do the download of the pdf in the one page, without the user having to click another link to download the pdf file.
Here's the structure with the 2 page setup - the viewReport.php page has a link like this:
$downloadLink = 'downloadFile.php?fileName='.$fileName.'&path='.$url;
$displayLink = 'This Report was successfully created. Click here to view the PDF.';
The downloadFile.php page has the following:
$fileName = $_GET['fileName'];
header('Content-type: application/force-download');
header('Content-Disposition:filename="'.$fileName.'"');
echo $fm->getContainerData($_GET['path']);
which works to immediately download the PDF. I've now changed the viewReport.php page to include this:
$downloadLink = 'downloadFile.php?fileName='.$fileName.'&path='.$url;
header('Content-type: application/force-download');
header('Content-Disposition:filename="'.$fileName.'"');
echo $fm->getContainerData($url);
When this page loads it does download a PDF file that it cannot be opened and is about 1/3 the size of the one that is generated with the 2 page version, so obviously not a valid PDF file. Also the viewReport.php page appears to stop loading once it processes this line hat downloads the pdf file:
echo $fm->getContainerData($url);
and then appears to go back to the previous page.
This is my first time trying to do this in a single page so not sure if this can even be done this way?
You won't be able to do both actions of rendering a new page for the user to view while simultaneously triggering an automatic file download with a single request.
The problem is that both of those actions require specific headers being sent in order to deliver their payloads correclty. Obviously you're seeing the headers you need to set to trigger the file download, but the delivery of a web page also requires specific headers which are normally set for you in the background. And the headers for delivering a web page for viewing vs. a file download are not compatible with each other - you can only send one or the other in a single response.
With all that said, there are probably other workarounds possible using javascript and ajax. But ultimately it all comes down to sending separate requests in order to receive two distinct sets of headers required for the two scenarios.
I am wondering if it is possible to render an action's view without loading in the browser?
I'm using a cakephp component that used mPDF to generate PDF files. The component works well if you load the action and view in a browser but when I want to generate and save the pdf to the drive it doesn't seem to kick in, I'm assuming it's because the view isn't loaded property.
The component I am using is: https://github.com/segy/Mpdf
So basically I have a function called mail_merge which generates a pdf using the mentioned component, I need to call the mail_merge function from another function within the same controller to generate the pdf but without loading in the browser.
Thanks
UPDATE:
I worked out how to set the view as a variable and then write it to the pdf in the function.
ADDED THIS:
// RENDER THE VIEW AND SET AS A VARIABLE TO WRITE THE HTML
// --------------------------------------------------------------------------->
$response = $this->render('mail_merge');
$thebody = $response->body();
$this->Mpdf->WriteHTML($thebody);
Unfortunately my PDF still won't generate so I guess the issue is something else.
Try this way:
$response = $this->render('mail_merge');
$thebody = $response->body();
$this->Mpdf->WriteHTML($thebody);
$this->Mpdf->Output('filename.pdf');
I am using Yii Framework, TCPDF and jQuery to generate a pdf.
The pdf is generated by inputing in a form and submitting it using ajax.
The pdf is created but here is the problem when it returns to the client, it down not download.
here is the php code
$pdf->Output('Folder Label.pdf','D');
the jQuery on success function has
success: function(data) {
window.open(data);
}
Which i got from this site.
Can you please help
If the problem is that you are not getting the browser's download dialog for the PDF, then the solution is to do it this way:
First, redirect the browser (using window.location as the other answers say) to navigate to a special controller action in your application, e.g. with this url: http://your.application.com/download/pdf/filename.pdf.
Implement the action referenced in the URL like this:
public function actionPdf() {
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="filename.pdf";');
header('Content-Length: '.filesize('path/to/pdf'));
readfile('path/to/pdf');
Yii::app()->end();
}
This will cause the browser to download the file.
You need to save the PDF to somewhere on your server and then issue window.location = '/url/to/pdf-you-just-saved.pdf'; from your javascript. The users browser will then prompt them to download the PDF file.
in tcpdf , just pass this argument the Output method:
$pdf->Output('yourfilename.pdf', 'D');
that's all
Not quite, that will cause errors on some browsers, this is the correct way to set the window location.
window.location.assign( downloadUrlToPdf );
So
Send a request to make the pdf via Ajax to the server
Process and generate the pdf on the server
Return in the Ajax call the url to the file you just made
Use the above code fragment to open a download of said file