can't reach page while export pdf/downloading file in dompdf - codeigniter

I want to export data tables to PDF.
I'm using Codeigniter Framework, and use dompdf plugin.
I think the code is correct, because it doesn't show any error information when I click the button to export to PDF, the page spends a long time loading, and in the end, it just displays "can't reach the page".
This problem also happens when I try to download files from my local directory.
Here is the code:
actually issue happened when i try to download more number of records
The controller:
$this->load->library('pdf'); // change to pdf_ssl for ssl
$data = array();
$data['result'] = $finaldata;
$data['search_header'] = $search_header;
$html = $this->load->view('admin/report/school_pdf', $data, TRUE);
$this->pdf->create($html, $filename);

Related

How can I download pdf before loading another view in laravel?

after completing a registration formular, when user clicks submit button, I want to first download a pdf and then redirect user to a view. Here is my code :
public function formularSave(Request $request) {
if(!isset($_REQUEST['token'])) {
abort(404);
}
$token = $_REQUEST['token'];
$upd_app = Application::where('token', $token)->update([
'status' => 22
]);
$result = "Registration complete.";
$html .= 'Some test code here
<br>
<p>Date: '.date('d.m.Y H:i:s').'</p>
';
$pdf = PDF::loadHTML($html);
$filename = substr(md5(uniqid().time()), 0, 17) . '.pdf';
$pdf->save(storage_path().'/app/public/uploads/rezolutii/'.$filename);
//code for download pdf HERE!!!!
return view('site.pages.registercomplete', compact('result'));
}
How can I download the pdf, after I create it?
It's impossible on Laravel.
This will not work. Your browser operates on simple requests one goes out one comes in.
There is no way for browser to know if user finished downloading the file and saved it somewhere. The final response from browser if file to be downloaded, nothing can follow that as far as I understand.
Now I'm not sure how that can be handled in javascript but in pure html requests it will not work.
Check this https://laracasts.com/discuss/channels/laravel/redirect-after-download

How to display PDF Documents on the browser using a View in Laravel 5.8

I'm working on a web application using Laravel 5.8, I'm new to Laravel framework. I would like to display PDF documents on the browser when users click on some buttons. I will allow authenticated users to "View" and "Download" the PDF documents.
I have created a Controller and a Route to allow displaying of the documents. I'm however stuck because I have a lot of documents and I don't know how to use a Laravel VIEW to display and download each document individually.
/* PDFController*/
public function view($id)
{
$file = storage_path('app/pdfs/') . $id . '.pdf';
if (file_exists($file)) {
$headers = [
'Content-Type' => 'application/pdf'
];
return response()->download($file, 'Test File', $headers, 'inline');
} else {
abort(404, 'File not found!');
}
}
}
/The Route/
Route::get('/preview-pdf/{id}', 'PDFController#view');
Mateus' answer does a good job describing how to setup your controller function to return the PDF file. I would do something like this in your /routes/web.php file:
Route::get('/show-pdf/{id}', function($id) {
$file = YourFileModel::find($id);
return response()->file(storage_path($file->path));
})->name('show-pdf');
The other part of your question is how to embed the PDF in your *.blade.php view template. For this, I recommend using PDFObject. This is a dead simple PDF viewer JavaScript package that makes embedding PDFs easy.
If you are using npm, you can run npm install pdfobject -S to install this package. Otherwise, you can serve it from a CDN, or host the script yourself. After including the script, you set it up like this:
HTML:
<div id="pdf-viewer"></div>
JS:
<script>
PDFObject.embed("{{ route('show-pdf', ['id' => 1]) }}", "#pdf-viewer");
</script>
And that's it — super simple! And, in my opinion, it provides a nicer UX for your users than navigating to a page that shows the PDF all by itself. I hope you find this helpful!
UPDATE:
After reading your comments on the other answer, I thought you might find this example particularly useful for what you are trying to do.
According to laravel docs:
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.
All you need to do is pass the file path to the method:
return response()->file($pathToFile);
If you need custom headers:
return response()->file($pathToFile, $headers);
Route::get('/show-pdf/{id}', function($id) {
$file = YourFileModel::find($id);
return response()->file(storage_path($file->path));
})->name('show-pdf');
Or if file is in public folder
Route::get('/show-pdf', function($id='') {
return response()->file(public_path().'pathtofile.pdf');
})->name('show-pdf');
then show in page using
<embed src="{{ route('show-pdf') }}" type="text/pdf" >

Tracking clicks on Download button and save in database

I have simple Download button which is looks like this
<a download="{{ $thumb }}" href="{{ $thumb }}" class="btn btn-success">Download Image</a>
It is appearing on page when the image is found. The button is working perfectly and is downloading the image.
The page has simple input field which user can search the image and if image is found it's showed on page with button download.
I've made a function which saves each string which is searched and now I'm wondering if I can save also if button Download is clicked e.g. the image is downloaded.
Can someone show me an example? I'm using laravel 5.4 here. So maybe I need to pass to the controller click event?
Current controller function
public function getImage(Request $request)
{
$url = get_curl_content_tx('http://example.com/api?url='.$request->input('url'));
$items = json_decode($url, true);
$thumb = $items['thumbnail_url'];
$db_save = new Image();
$ip = $request->ip();
$ip = DB::connection()->getPdo()->quote($ip);
$db_save->url = $request->input('url');
$db_save->ip = DB::raw("inet_aton($ip)");
$db_save->save();
return view('getImage',compact('thumb'));
}
Yes. Make sure that the href is going to a route where you keep track of these records and you can pass on the specific image to update the Record Model of the Image model itself if the counter is there? Something like this:
public function TrackDownload(Image $image){
$imageRecord = Record::where('image_id', '=', $image->id);
$imageRecord->update([
'download_counter' => $imageRecord->download_counter + 1;
]);
return redirect()->route();
}

Converting html to pdf after clicking on download button in codeigniter

I have created a resume builder application.User enters all required details and i am storing all details in database. Then i am fetching those records and displaying it in different templates. I have created 3 templates. Now I want to give download as pdf and download as docx two download buttons. Once user clicks on any of buttons file will be downloaded in their chosen format. Either pdf or docx.
Is it possible to achieve in codeigniter?
i used Tcpdf
Controller Code is
the code written in controller download $this - > load - > library("Pdf");
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set auto page breaks
$pdf - > SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf - > setImageScale(PDF_IMAGE_SCALE_RATIO);
// Add a page
// This method has several options, check the source code documentation for more information.
$pdf - > AddPage();
// Set some content to print
$html = $this - > load - > view('resume/res_template1_view', array('left_sidebar' => 'sidebar/left_sidebar', ' right_sidebar' => 'sidebar/right_sidebar'), $data, TRUE);
// Print text using writeHTMLCell()
$pdf - > writeHTML($html, 0, 0, '', '', 0, 1, 0, true, '', true);
//$pdf->writeHTML($html, true, false, true, false, '');
// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf - > Output('resume.pdf', 'D');
Probably you need to use some library in CI to achieve this. Please check below.
you can use Html2Pdf for CI3 library. https://github.com/shyamshingadiya/HTML2PDF-CI3
How to Use
There are 4 options you a required to set in order to create a PDF; folder, filename, paper and HTML
//Load the library
$this->load->library('html2pdf');
//Set folder to save PDF to
$this->html2pdf->folder('./assets/pdfs/');
//Set the filename to save/download as
$this->html2pdf->filename('test.pdf');
//Set the paper defaults
$this->html2pdf->paper('a4', 'portrait');
//Load html view
$this->html2pdf->html(<h1>Some Title</h1><p>Some content in here</p>);
The create function has two options 'save' and 'download'. Save will just write the PDF to the folder you choose in the settings. Download will force the PDF to download in the clients browser.
//Create the PDF
$this->html2pdf->create('save');
Advanced usage
Theres no reason why you can't build and pass a view to the html() function. Also note the create() function will return the saved path if you choose the 'save' option.
$data = array(
'title' => 'PDF Created',
'message' => 'Hello World!'
);
//Load html view
$this->html2pdf->html($this->load->view('pdf', $data, true));
if($path = $this->html2pdf->create('save')) {
//PDF was successfully saved or downloaded
echo 'PDF saved to: ' . $path;
}
Please check above mentioned solution. Let me know if it not works.

I am using the upload function of codeigniter but how do I attach the image uploaded on the server once I edit that item?

I have been successful in using the File Uploading Library to upload my files on the server (mostly images).
My problem is when I need to update that specific item, I need to browse for photo again. Is there a way I can just attach the image uploaded already in the server to my upload button? I am thinking to make a media page like that of the wordpress but is there any starting point that anyone knows who can point me to? Thanks so much!
You can use a hidden field...check this example
View
<?php echo form_upload("vLogo", $campaign->vLogo , "id='vLogo'"); ?>
<?php echo form_hidden("vLogo_old", $campaign->vLogo , "id='vLogo_old'"); ?>
Controller
(Edit function)
//The way you do when you click on browse and select a file
if(isset($_FILES['vLogo']['tmp_name']) && !empty($_FILES['vLogo']['tmp_name']))
{
$this->load->library('upload', $config);
// Initialaizing Logo upload
$this->upload->initialize($config);
if ( !$this->upload->do_upload('vLogo')){
$logo_error = array('vLogo' => $this->upload->display_errors());
}
else{
$data1 = array('upload_data' => $this->upload->data());
}
}
else
{
//The browse filed is empty..So hidden input is taken
$data1['upload_data']['file_name'] = $post['vLogo_old'];
}

Resources