Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Dompdf Library

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 320
    Comment on it

    Dompdf is a standard library for generating a PDF file. This library is used to convert an HTML file to PDF file. Dompdf is a style-driven renderer, as it will download and read external stylesheets, style attributes of individual HTML elements.

     

    Some of the features of dompdf are:

    • It supports most presentational HTML attributes.
    • It supports complex tables, including row & column spans
    • Provides basic support for SVG documents using php-svg lib.

     

    Most common methods that are used :

    • loadHtml: this method is used for loading an HTML string.
    • loadHtmlFile: this method is used to load an HTML file.
    • output: this method returns the pdf file as a string.
    • render: this method furnish the HTML to PDF.

     

    Now, to get started, firstly you are required to download the dompdf lib which is easily available from Github. After downloading, extract the folder and paste it to your app/Vendor folder. Next thing is to import this dompdf folder and its file dompdf_config.inc.php by applying the following code in your controller file.

     

    App::import('Vendor','DomPDF',array('file'=>'DomPDF/dompdf_config.inc.php'));

     

     

    Next create a function in your controller file:

     

    public function dompdf(){
    
          $dompdf =  new DOMPDF();
                  
            $result = $this->Employee->find('all');
            $html1= "";
            foreach ($result as $value) {
    
                $html1 = $html1."<tr><td>".$value['Employee']['id']."</td>
                <td>".$value['Employee']['emp_name']."</td>
                <td>".$value['Employee']['emp_dept']."</td>
                <td>".$value['Employee']['emp_number']."</td>
                <td>".$value['Employee']['emp_address']."</td>
                <td>".$value['Employee']['emp_salary']."</td></tr>";
            }
    
            $html = '<html>
            <body>
            <p>Evon Employees List:</p>
            <table border=1>
            <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Department</th>
            <th>Number</th>
            <th>Address</th>
            <th>Salary</th>
            </tr>'.$html1.'
            </table>
            </body>
            </html>';
          // print_r($html);die();
          $dompdf->load_html($html);
    
          $dompdf->render();
    
          $output = $dompdf->output();
          file_put_contents("DomFile.pdf", $output);
        }
    

     

    Create a blank view file named dompdf.ctp and add this link to tour display.ctp file:

     

    <?php echo $this->Html->link('DomPDF File',array('controller'=>'employees','action'=>'dompdf'),array('target'=>'_blank'));?>

     

    Thats it, your pdf file is saved . Check your webroot folder for the saved file.

     

    Happy Coding :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: