Convert html to pdf in codeigniter - codeigniter

I am using dompdf library to convert html content to pdf.I am facing some problems.
1) I am able to generate pdf but On My HTML content there are some images but when I am generating pdf images are not coming in PDF.
2) Also PDF is not showing complete data means data is coming surely but not visible of complete width.
Please help!!!

Here is my sample of making my html to pdf...I wish i give you some idea
LIBRARIES:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dompdf_gen {
public function __construct() {
require_once APPPATH.'third_party/dompdf/dompdf_config.inc.php';
$pdf = new DOMPDF();
$CI =& get_instance();
$CI->dompdf = $pdf;
}
}
Controller:
function pdf($id){
$data['contract_items_all'] = $this->contract_items->get_serial_infos($this->uri->segment(4));
$data['child_info'] = $this->contract_items->get_children_info($cid);
$this->load->library('dompdf_gen');
$html = $this->load->view('contracts/equipment_pdf', $data, true);//this equipment_pdf contain the html that you will convert to pdf
$this->load->library('dompdf_gen');
$dompdf = new DOMPDF();
// Convert to PDF
$this->dompdf->load_html($html);
$this->dompdf->render();
$this->dompdf->stream("Contract.pdf",array("Attachment"=>0)); // VIEW IN OTHER TAB
//$this->dompdf->stream("Contract.pdf"); // SAVE FILE TO DISK
}

why are you loading libraries twice?
// ...
$this->load->library('dompdf_gen');
$html = $this->load->view('contracts/equipment_pdf', $data, true);//this equipment_pdf contain the html that you will convert to pdf
$this->load->library('dompdf_gen');
$dompdf = new DOMPDF();
// ...

Related

Convert image to webp after adding watermark on image

I am currently working on a project on which I have to add watermark to image and then convert the image into webp format.I come up with two libraries to do so but they both require uploaded image as parameters, but after adding watermark the input for the webp library becomes object of Image Intervention.
This is my Laravel Controller Code
use Illuminate\Http\Request;
use File;
use Webp;
use Intervention\Image\ImageManagerStatic as Image;
public function store(Request $request)
{
foreach ($request->image as $img) {
$destinationPath = 'uploads/auction';
$watermark = 'uploads/auction/logo.png';
$originalFile = $img->getClientOriginalName();
$filename= uniqid().'-'.time().'-image.jpg';
$name = $destinationPath.'/'.$filename;
$img = Image::make($img);
$img = $img->insert($watermark, 'bottom-right', 1, 1);
$img->save($name);
WebP::make($img)->save($name);
}
}
Can anyone provide me a solution to achieve this??

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");

Getting blank pages in laravel while using dom-pdf

I am using laravel 4.2 with dompdf.
My controller:
public function getLedgerReport() {
$pdf = PDF::loadView('sales.ledger_report')->setPaper('a4');
$pdf->stream();
}
My view:
<?php
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
$html = '';
$html .= '<html><body>hi</body></html>';
echo $html;
?>
The code which gives a 'hi' in the Pdf file.
public function getLedgerReport() {
$pdf = PDF::loadView('sales.ledger_report')->setPaper('a4');
$pdf->stream();
$invoice_path = 'downloads/invoices/invoice.pdf';
$pdf->save($invoice_path);
}
I get a blank page instead of getting a 'hi' text.
But when I save the same as a pdf file I do see a 'hi' in the file.

codeigniter export csv in windows platform

function export_csv(){
$this->load->helper('csv');
if(isset($_POST['term_val'])&&$_POST['term_val']<>'0'){
$term = $this->Search_model->get_term($_POST['term_val']);
$term = json_decode($term);
}else{
$term=array();
}
$orders = $this->Order_model->get_orders_export($term,'place_order');
if(count($orders)>0){
foreach($orders as $order){
$sublist['order_id']= $order->order_number;
$sublist['ship_to']= $order->ship_firstname.' '.$order->ship_lastname;
$sublist['order_date']= date('d-m-Y H:i:s',strtotime($order->ordered_on));
$sublist['email_to']= $order->ship_email;
$sublist['city']= $order->ship_city;
$sublist['pincode']= $order->ship_zip;
$sublist['ship_address']= $order->ship_address1.' , '.$order->ship_address2;
$sublist['phone']= $order->ship_phone;
$sublist['product_name']= $order->name;
$sublist['product_id']= $order->product_id;
$sublist['status']= $order->status;
$sublist1[]= $sublist;
}
$delimiter = ";";
$newline = "\r\n";
$heading[]=array('order_id'=>'Order Id','ship_to'=>'Ship To','order_date'=>'Order Date','email_to'=>'Email To',
'city'=>'City','pincode'=>'Pincode','ship_address'=>'Ship Address','phone'=>'Phone','product_name'=>'Product Name','product_id'=>'Product ID','status'=>'status');
$get_csv=array_to_csv1($sublist1,$heading,$delimiter, $newline);
ob_start();
$filename = "orders_" . date('Ymd') . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
// header("Content-type: text/x-csv");
//header("Content-type: text/csv");
// header("Content-type: application/csv");
// header("Content-Disposition: attachment; filename=orders".date('d-M-Y').".csv");
print_r($get_csv);
}else{
redirect($this->config->item('admin_folder').'/orders');
}
}
The above code is the controller function for export CSV and the image is the action taking place when exporting.
Problem : Actually if we export csv using codeigniter it is showing that image as shown above. If we press export csv using codeigniter it it should not show that image , it should directly export csv into excel .
if you want to generate reports in csv format it is very easy with codeigniter. Your model function
function index(){
return $query = $this->db->get('my_table');
//Here you should note i am returning
//the query object instead of
//$query->result() or $query->result_array()
}
Now in controller
function get_report(){
$this->load->model('my_model');
$this->load->dbutil();
$this->load->helper('file');
// get the object
$report = $this->my_model->index();
//pass it to db utility function
$new_report = $this->dbutil->csv_from_result($report);
//Now use it to write file. write_file helper function will do it
write_file('csv_file.csv',$new_report);
//Done
}
No externals are required everything is available in Codeigntier.
The above method will force the file to be downloaded instead of
being opening. Cheers! If you want to write xml file it is easy too.
Just use xml_from_result() method of dbutil and use write_file('xml_file.xml,$new_report)
Visit these links they will help.
Database Utility Class
And
File Helper
***> if you are trying to generate reports in csv format then it will quite
> easy with codeigniter.***
Place this code in your Controller
function get_report()
{
$this->load->model('Main_Model');
$this->load->dbutil();
$this->load->helper('file');
/* get the object */
$report = $this->Main_Model->print_report();
$delimiter = ",";
$newline = "\r\n";
$new_report = $this->dbutil->csv_from_result($report, $delimiter, $newline);
write_file( 'application/third_party/file.csv', $new_report);
$this->load->view('report_success.php');
}
Put this code into Model
public function print_report()
{
return $query = $this->db->query("SELECT * FROM Table_name");
}
report_success.php is just Successful Notification.
Your Report is being Exported. Thank you
Finally Your "file.csv" is generated.
its basically stored at physical storage.
In CodeIgniter/application/third-party/file.csv
it works.
it will help you.

CodeIgniter - Displaying and entering dynamic meta data

I have started using CodeIgniter ... Im finding it is quite good, although I have a bit of an issue.
Whats the best way to handle meta data? ... In the views folder, I have created another folder called 'includes' then in there I have added header, footer, nav views.
So I'm taking it that for each controller meta data needs to be entered and then passed to the header view.
If I could get some examples of how you all go about this, that would be fantastic.
Cheers,
I know this is an out of date question, but in case anyone is looking for a simple solution for this in Codeigniter 2.2
I found the simplest thing was to create a helper for this.
Create a file located in config/seo_config.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['seo_title'] = 'My title';
$config['seo_desc'] = 'My description';
$config['seo_robot'] = true;
/* End of file seo_config.php */
/* Location: ./application/config/seo_config.php */
Create a file located in application/helers/seo_helper.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* SEO Helper function
*
* Generates Meta tags for SEO
*
* #author Henrik Oldenborg
* #version 1.0
*/
/**
* meta_tags()
*
* Generates tags for title, description and robots
* Using title and description from config file as default
*
* #access public
* #param string Title
* #param string Description (155 characters)
* #param bool Robots follow or no folow
*/
if(! function_exists('meta_tags')){
function meta_tags($meta)
{
$CI =& get_instance();
$CI->config->load('seo_config');
if(!isset($meta['title']))
$meta['title'] = $CI->config->item('seo_title');
if(!isset($meta['desc']))
$meta['desc'] = $CI->config->item('seo_desc');
if(!isset($meta['robot']))
$meta['robot'] = $CI->config->item('seo_robot');
$html = '';
//uses default set in seo_config.php
$html .= '<title>'.$meta['title'].'</title>';
$html .= '<meta name="title" content="'.$meta['title'].'"/>';
$html .= '<meta name="description" content="'.$meta['desc'].'"/>';
if($meta['robot'] == true){
$html .= '<meta name="robots" content="index,follow"/>';
} else {
$html .= '<meta name="robots" content="noindex,nofollow"/>';
}
echo $html;
}
}
/* End of file seo_helper.php */
/* Location: ./application/helpers/seo_helper.php */
Load up the helper - (Either in the controller or in config/autoload.php)
$this->load->helper('seo_helper');
Add the following code in your view between the two header tags
<?=(isset($meta) ? meta_tags($meta) : meta_tags());?>
Now, all you need to do is declare the $meta variable in your controller like so
$data['meta']['title'] = 'My special title';
$data['meta']['desc'] = 'My special description';
$this->load->view('mytemplate',$data)
Usefull tip
You might want to declare your meta data in the controllers constructor. This way you can easily override it when needed in a underlying function, like so.
class Forum extends CI_Controller {
private $data = array();
function __construct()
{
$this->data['meta']['title'] = 'My forum title';
$this->data['meta']['robot'] = false;
parent::__construct();
}
public function index()
{
$this->data['content'] = 'forum';
$this->load->view('userarea/template',$this->data);
}
public function topic($topic_id)
{
$this->data['meta']['title'] = 'My specific title';
$this->data['content'] = 'topic';
$this->load->view('userarea/template',$this->data);
}
}
Create a new file in your libraries folder:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class View_lib {
public function __construct()
{
$this->CI =& get_instance();
}
public function load_view($template, $data = NULL)
{
$this->CI->load->view('header', $data);
$this->CI->load->view($template);
$this->CI->load->view('footer');
}
}
/* End of file view_lib.php */
/* Location: ./system/application/libraries/view_lib.php */
Then load this library in your controller:
$this->load->library('view_lib');
Then call your view file in a function like this:
$this->view_lib->load_view('name_of_view_file', $data);
or (if you call a static file without any data to pass):
$this->view_lib->load_view('name_of_view_file');
There are many ways of doing this, but this one works nicely for the applications I am working on. In one of my projects I have multiple functions in the view_lib library to load with or without sidebar, different headers and footers depending if a user is logged in.
Hope this helps, cheers.
I find its just easiet to pass it right in the head of the page with arrays
$meta = array(
array('name' => 'description', 'content' => 'Political website, Liberal, progressive, blog, posts,'),
array('name' => 'keywords', 'content' => 'politics, McCain, Beck, Hannity, Rush Limbaugh, Environment, Obama, ZB Block, Sarah Palin, Republicans, GOP, Democrats, Liberals, Conservatives, Reagan, Politicususa, FreakOut Nation, Raw Story, Congress, Senate, Representatives, Constitution, White, Black, racial, racsim, blog, blogging, Lemon, Lemonrose, Fox, Fox News,
political, partys, president'),
array('name' => 'Content-type', 'content' => 'text/html; charset=utf-8', 'type' => 'equiv'),
);
echo meta($meta);

Resources