Not able to save generated pdf on my remote server - codeigniter

I am trying to save generated pdf in custom folder but its not saving. I want to pdf to send as attachment in email
Here is my code generate and save pdf
public function email($timestamp=0, $load_id=0, $type=0){
$this->load->library('tcpdf/Pdf');
$pdf = new Pdf('P', 'mm', 'LETTER', true, 'UTF-8', false);
$pdf->SetTitle('Document');
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetHeaderMargin(10);
$pdf->SetLeftMargin(10);
$pdf->SetRightMargin(10);
$pdf->SetTopMargin(10);
$pdf->SetAutoPageBreak(true, 25);
$pdf->SetAuthor('Author');
$pdf->SetDisplayMode('real', 'default');
$pdf->Write(5, 'Cal Sierra Load Document'); // add a page
$pdf->AddPage();
// $carriers = $this->admin_model->getCarriers(array(), array(), 0,0,1,"","","",1);
$load=$this->admin_model->getLoad($load_id);
$pickupDrops=$this->admin_model->getPickupDrops(0,0,1,0,$load_id);
$loadPickups=array();
$loadDrops=array();
if(!empty($pickupDrops)){
foreach($pickupDrops as $row){
if($row['type']==1)
$loadPickups[]=$row;
else
$loadDrops[]=$row;
}
}
$data['load'] = $load;
$data['loadPickups'] = $loadPickups;
$data['loadDrops'] = $loadDrops;
// $data['carriers'] = $carriers;
$data['settings'] = $this->settings_model->getSettingsFile();
$data['currentTime'] = $timestamp;
// echo "<pre>";
// print_r($data);
// exit();
switch ($type) {
case 1:
$html = $this->load->view('documents/doc1', $data, true);
break;
case 2:
$html = $this->load->view('documents/doc2', $data, true);
break;
case 3:
$html = $this->load->view('documents/doc3', $data, true);
break;
default:
}
$pdf->Output('custom'.'Document.pdf', 'F');
$this->Output("custom");
exit();
}
I am not sure where put my folder to pdf i am also adding my folder layout picture https://i.stack.imgur.com/tUadw.png

Try adding a more complete path to the output function. Something like:
$pdf->output(FCPATH . "my/directory/path/" . "pdfName.pdf", 'F');

Related

force_download not working in codeigniter after writing

tried to auto download file after file created but its not working. file creating and appending data perfectly but download functionality not working
public function getexportfile(){
$this->load->dbutil();
// $this->load->database('DB_utility','dbutil');
//$this->load->helper('download_helper','download');
$this->load->helper('download_helper');
$this->load->helper('file');
error_reporting(0);
ini_set('display_errors', 0);
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
## Condition Start
$sWhere = " where condition"; // where conditon
$date = date('Y-m-d');
$filename = 'app/logs/othermarketplace_'.$date.'.csv';
$data = $this->PMP->export($sWhere); // calling model
$result = $this->dbutil->csv_from_result($data);
write_file($filename,$result); // file creating write data perfectly
$data = file_get_contents($filename);
//$path = file_get_contents(base_url().$filename);
//$name = "othermarketplace_'.$date.'.csv";
//force_download($nme, $pth);
force_download($filename,$data);
}
anything i missed?
From docs:
// Contents of photo.jpg will be automatically read
force_download('/path/to/photo.jpg', NULL);
//$data = file_get_contents($filename);
//$path = file_get_contents(base_url().$filename);
//$name = "othermarketplace_'.$date.'.csv";
//force_download($nme, $pth);
if (!is_file($filename)) {
exit('No file found.');
}
force_download($filename, NULL);
Note:
These lines might be doing something weird. Perhaps remove them as they aren't being used:
error_reporting(0);
ini_set('display_errors', 0);
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);

How to use Ajax pager in Drupal7

I want to use ajax_pager in my block.
I've installed Libraries API and Ajax Pager API (as it described on the AP API page). But it's still not working.
When I use 'pager' instead of 'ajax_pager' it works.
What I'm doing wrong?
<?php
function latest_news_block_info() {
$blocks['latest_news_block'] = array(
// info: The name of the block.
'info' => t('Latest News'),
);
return $blocks;
}
function latest_news_block_view($delta = '') {
// The $delta parameter tells us which block is being requested.
switch ($delta) {
case 'latest_news_block':
// Create your block content here
$block['subject'] = t('Last news');
$query = new EntityFieldQuery();
//change news by the name of your content type
$entities = $query->entityCondition('entity_type', 'node')
->pager(5, 0)
->entityCondition('bundle', 'news')
->propertyOrderBy("created", "DESC")
->execute();
$content = "";
foreach($entities['node'] as $obj) {
$node = node_load($obj->nid);
$content .= "<img src='" .file_create_url($node->field_image['und'][0]['uri']) . "' width='200px' height='200px'>" . l($node->title, "node/" . $node->nid) . truncate_utf8($node->body['und'][0]['value'], 200, true, true) . '<br><hr>';
$block['content'] = $content;
}
break;
}
$pager = theme('ajax_pager', array('parameters' => array('selector' => 'block-system-main')));
$block['content'] .= $pager;
return $block;
}

file upload directory not created in my vps

file upload directory not created in my vps its show me blank page in error massege
Here is my code.
$dir_exist = true; // flag for checking the directory exist or not
if (!is_dir('./assets/uploads/profile_pictures/' . $id))
{
mkdir('./assets/uploads/profile_pictures/' . $id, 0777, true);
$dir_exist = false; // dir not exist
}
if ( ! $this->upload->do_upload('upload_profile_picture'))
{
if(!$dir_exist)
rmdir('./assets/uploads/profile_pictures/' . $id);
$error = array('error' => $this->upload->display_errors());
//$this->session->set_flashdata('error', $error[1]);
print_r($error);
}
else
{
$upload_data = $this->upload->data();
$new_name=$upload_data['file_name'];
//$this->session->set_flashdata('error', $data[1]);
}
if($new_name=='0'){
$new_name=$temp_profile_pic;
}
$date = str_replace( ':', '', $date);
if (!is_dir('uploads/'.$date)) {
mkdir('./uploads/' . $date, 0777, TRUE);
}
Please try above code and check if this code is working then definately the issue is different.
and check the id is not getting : either it will never created new dir.
Here is the reference: https://www.socketloop.com/tutorials/codeigniter-php-create-directory-if-does-not-exist-example

Creating a Dynamic PDF and Saving it in a folder using Loop with MYSQL and Codeigniter

This is the public function with looping (Edited)
public function generate_salary_slip(){
$this->load->helper('file');
$this->load->library('m_pdf');
$data = [];
$slip = $this->Admin_Login_Model->generate_salary_slip();
//$html = array();
$html='';
if(!empty($slip)){
foreach($slip as $slip_list){
$id = $slip_list->emp_id;
$data['attendance'] = $this->Admin_Login_Model->get_slip_attendance($id);
$data['emp_profile'] = $this->Admin_Login_Model->get_emp_details($id);
$data['late_day'] = $this->Admin_Login_Model->get_late_days($id);
$data['half_days'] = $this->Admin_Login_Model->get_half_days($id);
$data['salary'] = $this->Admin_Login_Model->get_salary_slip($id);
$data['second_half'] = $this->Admin_Login_Model->get_half_Second_days($id);
//$html=$this->load->view('admin/payslip',$data);
$pdfFilePath = date("F_Y")."salary_slip.pdf";
$html .= $this->load->view('admin/payslip', $data, true);
}
$htmlval = $html;
//echo $html;exit;
$this->m_pdf->pdf->WriteHTML($htmlval);
$this->m_pdf->pdf->Output('salary_slips/'.$pdfFilePath, "F");
$this->session->set_flashdata('success','Salary slips saved in directory!');
redirect('admin_attendance_list');
}else{
$this->session->set_flashdata('message','Salary slips not available for this month!');
redirect('admin_attendance_list');
}
}
File is getting saved in the folder, but Only with the First Record. Also, Repeated loop is getting generated, but with same record.
How to create a HTML by loop.
PS: Using MPDF to generate PDF File
Any hep will be highly appreciated.
You need to take the render, writeHtml and Output calls outside the forech loop, there is you problem.
You could save all the data in a var and then render the HTML as you are doing now and then generate the PDF.
EDIT: Your code should look something like this (please read comments below):
$htmlData = array();
if(!empty($slip)){
foreach ($slip as $slip_list) {
//
$htmlData[] = $data;
}
$pdfFilePath = date("F_Y")."salary_slip.pdf";
$html = $this->load->view('admin/payslip', $htmlData, true);
$this->m_pdf->pdf->WriteHTML($html);
$this->m_pdf->pdf->Output('salary_slips/'.$pdfFilePath, "F");

How to validate duplicate entries before inserting to database - Codeigniter

I have developed simple application, i have generated checkbox in grid dynamically from database, but my problem is when user select the checkbox and other required field from grid and press submit button, it adds duplicate value, so i want to know how can i check the checkbox value & other field value with database value while submitting data to database.
following code i use to generate all selected items and then save too db
foreach ($this->addattendee->results as $key=>$value)
{
//print_r($value);
$id = $this->Attendee_model->save($value);
}
i am using codeigniter....can any one give the idea with sample code plz
{
$person = $this->Person_model->get_by_id($id)->row();
$this->form_data->id = $person->tab_classid;
$this->form_data->classtitle = $person->tab_classtitle;
$this->form_data->classdate = $person->tab_classtime;
$this->form_data->createddate = $person->tab_crtdate;
$this->form_data->peremail = $person->tab_pemail;
$this->form_data->duration = $person->tab_classduration;
//Show User Grid - Attendee>>>>>>>>>>>>>>>>>>>>>>>>
$uri_segment = 0;
$offset = $this->uri->segment($uri_segment);
$users = $this->User_model->get_paged_list($this->limit, $offset)->result();
// generate pagination
$this->load->library('pagination');
$config['base_url'] = site_url('person/index/');
$config['total_rows'] = $this->User_model->count_all();
$config['per_page'] = $this->limit;
$config['uri_segment'] = $uri_segment;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
// generate table data
$this->load->library('table');
$this->table->set_empty(" ");
$this->table->set_heading('Check', 'User Id','User Name', 'Email', 'Language');
$i = 0 + $offset;
foreach ($users as $user)
{
$checkarray=array('name'=>'chkclsid[]','id'=>'chkclsid','value'=>$user->user_id);
$this->table->add_row(form_checkbox($checkarray), $user->user_id, $user->user_name, $user->user_email,$user->user_language
/*,anchor('person/view/'.$user->user_id,'view',array('class'=>'view')).' '.
anchor('person/update/'.$user->user_id,'update',array('class'=>'update')).' '.
anchor('person/showattendee/'.$user->user_id,'Attendee',array('class'=>'attendee')).' '.
anchor('person/delete/'.$user->user_id,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure want to delete this person?')"))*/ );
}
$data['table'] = $this->table->generate();
//end grid code
// load view
// set common properties
$data['title'] = 'Assign Attendees';
$msg = '';
$data['message'] = $msg;
$data['action'] = site_url('person/CreateAttendees');
//$data['value'] = "sssssssssssssssssss";
$session_data = $this->session->userdata('logged_in');
$data['username'] = "<p>Welcome:"." ".$session_data['username']. " | " . anchor('home/logout', 'Logout')." | ". "Userid :"." ".$session_data['id']; "</p>";
$data['link_back'] = anchor('person/index/','Back to list of Classes',array('class'=>'back'));
$this->load->view('common/header',$data);
$this->load->view('adminmenu');
$this->load->view('addattendee_v', $data);
}
The code is quite messy but I have solved a similar issue in my application I think, I am not sure if its the best way, but it works.
function save_vote($vote,$show_id, $stats){
// Check if new vote
$this->db->from('show_ratings')
->where('user_id', $user_id)
->where('show_id', $show_id);
$rs = $this->db->get();
$user_vote = $rs->row_array();
// Here we are check if that entry exists
if ($rs->num_rows() == '0' ){
// Its a new vote so insert data
$this->db->insert('show_ratings', $rate);
}else{
// Its a not new vote, so we update the DB. I also added a UNIQUE KEY to my database for the user_id and show_id fields in the show_ratings table. So There is that extra protection.
$this->db->query('INSERT INTO `show_ratings` (`user_id`,`show_id`,`score`) VALUES (?,?,?) ON DUPLICATE KEY UPDATE `score`=?;', array($user_id, $show_id, $vote, $vote));
return $update;
}
}
I hope this code snippet gives you some idea of what to do.
maybe i have same trouble with you.
and this is what i did.
<?php
public function set_news(){
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$query = $this->db->query("select slug from news where slug like '%$slug%'");
if($query->num_rows()>=1){
$jum = $query->num_rows() + 1;
$slug = $slug.'-'.$jum;
}
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);
return $this->db->insert('news', $data);
}
?>
then it works.

Resources