Welcome Guest, Not a member yet? Register   Sign In
DomPdf plugin
#1

[eluser]Ngulo[/eluser]
Hello guys,

i was trying creating Pdf using this plugin i've found http://codeigniter.com/wiki/PDF_generati...ng_dompdf/

pdf is created but i can't move that to my preferred dir and also when i put code for creating in my controller i see pdf comes out is outputted, i would like to generate pdf without it is visible when page load on browser :/

any suggestions guys?

here is my piece of code:

PDF_pi.php plugin :

Code:
function pdf_create($html, $filename,$move_to, $stream=TRUE)
{
    require_once("dompdf/dompdf_config.inc.php");
    spl_autoload_register('DOMPDF_autoload');

    $dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $dompdf->render();
    if ($stream) {
        $dompdf->stream($filename.".pdf");
    } else {
        $CI =& get_instance();
        $CI->load->helper('file');
        write_file($move_to.$filename.".pdf", $dompdf->output());
    }
}


Controller.php
Code:
function index()
    {
                $view = false;
                
                 $this->load->model('product');
            
                 $view['lastInsertedProducts'] = $this->product->getLastInserted();

                 $this->load->plugin('PDF');
     // page info here, db calls, etc.
           $html = 'TRUE';
            pdf_create($html, 'firstPDF',base_url().'uploads/');
                
        $this->load->view('home/home_view',$view);
                
    }
#2

[eluser]pabloheim[/eluser]
hi !

maybe its because you are loading the view : $this->load->view('home/home_view',$view);
try deletind that line
#3

[eluser]Matt S.[/eluser]
In your controller you need to set you HTML variable up like this:

Code:
$html = $this->load->view('home/home_view',$view, TRUE);

Specifying the bool TRUE in the third parameter will prevent the view from being outputted to the browser. So your controller should look like this now:

Code:
function index()
    {
                $view = false;
                
                 $this->load->model('product');
            
                 $view['lastInsertedProducts'] = $this->product->getLastInserted();

                 $this->load->plugin('PDF');
     // page info here, db calls, etc.
           $html = $this->load->view('home/home_view',$view,TRUE);
            pdf_create($html, 'firstPDF',base_url().'uploads/');
                
    }


EDIT: After re-reading what you posted, I think your solution is this:

Code:
function index()
    {
                $view = false;
                
                 $this->load->model('product');
            
                 $view['lastInsertedProducts'] = $this->product->getLastInserted();

                 $this->load->plugin('PDF');
     // page info here, db calls, etc.
           $html = 'TRUE';
            pdf_create($html, 'firstPDF',base_url().'uploads/', FALSE); // Insert FALSE as the last parameter to prevent streaming
                
        $this->load->view('home/home_view',$view);
                
    }

By default, the function is set to stream the PDF rather than write it to the directory. To prevent that, just specify the boolean value FALSE as the last parameter in the pdf_create() function.
#4

[eluser]Ngulo[/eluser]
thanks guys you're both right!! and i solved the creation not outputted now,

but when i open my pdf files adobe reader tells me the file is empty or corrupted, in fact file is 0kb Sad

i just write inside $html = 'Hello world!';

seems it is creating pdf always empty file with this code :/

any other suggestions?


really thanks
#5

[eluser]pabloheim[/eluser]
hi , this is the configuration that im using .
i generate a pdf file with data from database :

the plugin :
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename, $stream=TRUE)
{
require_once("dompdf/dompdf_config.inc.php");

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
if ($stream) {
$dompdf->stream($filename.".pdf");
} else {
$CI =& get_instance();
$CI->load->helper('file');
write_file("./temp/invoice_$filename.pdf", $dompdf->output());
}
}

the function inside the controller :
sorry its in spanish, but it takes a hash and return data from a book (libro) using the libro_model. then get the html code after passing the data to the view file "codigo"
finally generate the pdf file

Code:
function etiqueta(){
    
        $hash= $this->uri->segment(3,0);
        
        if($hash ==0){ redirect('inicio');}
        else{
        $data['detalle'] = $this->Libro_model->get_data_hash($hash);
        $this->load->plugin('to_pdf');
        $html = $this->load->view('codigo',$data,true);

        pdf_create($html,'etiquetaLibroLibreChile');
        }
    }


the view file : codigo.php

Code:
<html>
<head>
<style type="text/css">

body{

font-size :50%;
}

.cuerpo{

padding : 20px;
font-family : Helvetica;
width : 500px;
border : 3px dashed #ccc;
margin : 0 auto ;
}

.codigo{background: #ccc;}




</style>



</head>


<body>
<div class="cuerpo">

<img src="public/img/logo_jpg.jpg" />


<table>    
    <tr>
    
    <td>
    
    
    
    
    &lt;?php

            if ($detalle->num_rows() > 0)
        {
        echo '<table>';

           foreach ($detalle->result() as $row)
           {
             echo '
             <tr><td>Título : </td><td>'.$row->titulo.'</td></tr>
             <tr><td>Autor : </td><td>'.$row->autor.'</td></tr>
             <tr><td><b>Código : </b></td><td> <b> '.$row->codigo.'</b> </td></tr>';
            
           }
          
           echo '</table>';
        }

    ?&gt;
    
    
    
    </td>
    
    <td width="250px" style="text-align:justify;">
    

        Lorem ipsum

    
    
    
    </td>
    </tr>
    
    </table>
    
    




</div>
&lt;/body&gt;
&lt;/html&gt;

hope it helps you!
#6

[eluser]Ngulo[/eluser]
right thanks man Wink

i found out what was my problem :/

i did not used $dompdf->render(); inside plugin script :/

now everythings works!!!! really thanks anyway , it's good also to see other code samples Wink

and just last question guys.... what about $stream ?

what is this? i can't understand if false or true,what's the effect ... ?

thanks again




Theme © iAndrew 2016 - Forum software by © MyBB