Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Discussion on convert Html to pdf in CakePHP 2.x

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 235
    Comment on it

    Hi Reader's,
    Welcome to FindNerd,today we are going to discuss convert Html to pdf in CakePHP 2.x.

    If we want to convert Html to pdf in our CakePHP web application then firstly we have to download TCPF library from Github. You can see below link.

    https://github.com/tecnickcom/tcpdf

    After downloading the packet we can see a TCPDF zip folder so we will extract it and move extract folder to /app/Vendor directory.

    Now we will create a file tcpdf.php in /app/Vendor/ directory and write below code:

    <?php
       App::import('Vendor','tcpdf/tcpdf');
        class XTCPDF extends TCPDF{
    
        }
    
    ?>

    Now create default.ctp layout in this directory: app/View/Layouts/pdf and write the following code:

    <?php
       header("Content-type: application/pdf");
       echo $content_for_layout;
    ?>

    Now we will write a function in our controller and our code will like below code:

    <?php
      public function create_pdf(){
        $users = $this->User->find('all');
        $this->set(compact('invoice'));
        $this->layout = '/pdf/default';
        $this->render('/Pdf/showpdfview');
      }
    ?>  

    Now we have to create a showpdfview.ctp file in /app/View/Pdf/ directory and write the following code in it and save the file.

    <?php
        App::import('Vendor','xtcpdf');
        $pdf = new XTCPDF('L', PDF_UNIT, 'A4', true, 'UTF-8', false);
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('XYZ');
        $pdf->SetTitle('Invoice');
        $pdf->SetSubject('Invoice Receipt');
        $pdf->SetKeywords('Invoice');
        //$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
        // set auto page breaks
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
        // set image scale factor
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
        $pdf->AddPage();
        // create some HTML content
        $subtable = '<table cellspacing="5" width="90%">
        <tr>
        <td border="1" bgcolor="#eee" align="center" border-color="#bbb">Invoice #</td>
        <td border="1" align="center">12462</td>
        </tr>
        <tr>
        <td border="1" bgcolor="#eee" align="center" border-color="#bbb">Date</td>
        <td border="1" align="center">'.$invoice['date'].'</td>
        </tr>
        <tr>
        <td border="1" bgcolor="#eee" align="center" border-color="#bbb">Amount</td>
        <td border="1" align="center">INR '.$invoice['amount'].'</td>
        </tr>
        </table>';
        $newtable='<table cellspacing="5" cellpadding="4">
        <tr>
        <th border="1" bgcolor="#eee" align="center"><h4>Item</h4></th>
        <th border="1" bgcolor="#eee" align="center"><h4>Category</h4></th>
        <th border="1" bgcolor="#eee" align="center"><h4>Sub-Category</h4></th>
        <th border="1" bgcolor="#eee" align="center"><h4>Duration</h4></th>
        <th border="1" bgcolor="#eee" align="center"><h4>Price</h4></th>
        </tr>
        <tr>
        <td border="1" align="center">Subscription Plan</td>
        <td border="1" align="center">'.$invoice['category'].'</td>
        <td border="1" align="center">'.$invoice['subcategory'].'</td>
        <td border="1" align="center">'.$invoice['duration'].' Years</td>
        <td border="1" align="center">INR '.$invoice['amount'].'</td>
        </tr>
        </table>';
        $card = ($invoice['paymentmode'] == "debit_card") ? "Debit Card" : ($invoice['paymentmode'] == "credit_card") ? "Credit Card" : "PayPal" ;
        $payment_mode='<table cellspacing="5" cellpadding="4" align="center" width="90%">
        <tr>
        <th border="1" bgcolor="#eee">Payment Mode</th>
        <td border="1">'.$card.'</td>
        </tr>
        <tr>
        <th border="1" bgcolor="#eee">Date</th>
        <td border="1">'.$invoice['date'].'</td>
        </tr>
        <tr>
        <th border="1" bgcolor="#eee">Total Amount</th>
        <td border="1">INR '.$invoice['amount'].'</td>
        </tr>
        </table>';
        $html = '<h1 bgcolor="#00000;" color="white" align="center">'.$invoice['title'].'</h1>
        <table cellspacing="3" cellpadding="4">
        <tr>
        <th>
        '.$invoice['first_name'].' '.$invoice['last_name'].'<br>
        Unique Reference No. : '.$invoice['reference_number'].'<br>
        '.$invoice['building_name'].'<br>
        '.$invoice['street_name'].'<br>
        '.$invoice['city'].' '.$invoice['postal_code'].' , '.$invoice['country'].'<br>
        Phone : '.$invoice['mobile_no'].'<br>
        Email : '.$invoice['email'].'
        </th>
        <th align="center"><img alt="" src="'.Configure::read('SITE_URL').'/img/logo.png"></th>
        </tr>
        <tr>
        <td><h1>Transaction Id : '.$invoice['transaction_id'].'</h1></td>
        <td align="right">'.$subtable.'</td>
        </tr>
        <tr>
        <td></td>
        <td></td>
        </tr>
        <tr>
        <td colspan="2">'.$newtable.'</td>
        </tr>
        <tr>
        <td></td>
        <td></td>
        </tr>
        <tr>
        <td></td>
        <td>'.$payment_mode.'</td>
        </tr>
        </table>
        <table align="center">
        <tr><td></td></tr>
        <tr><td align="center">For any queries and concern please feel free to contact us <a href="#">info@experconsultant.com</a></td></tr>
        <tr><td></td></tr>
        <tr><td align="center"><a href="'.Configure::read('SITE_URL').' ">www.ourdomain.com</a></td></tr>
        </table>
        ';
        $pdf->writeHTML($html, true, false, true, false, '');
        $pdf->lastPage();
        $pdf->Output(APP . 'webroot/files/pdf' . DS . .'newinvoice.pdf', 'F');
    
    
    
    ?>
    

    We Can modify this given html according our need for making any invoicing.

    Now before run the code firstly we have to create the following: /app/files/pdf/

    $pdf->Output(APP. 'files/pdf'.Ds.'newinvoice.pdf','F');
    

    Here F means that it will save the file to disk.

    If we want to save our created pdf file, then we will write below code in our controller:

    <?php 
      public function save_pdf() {
        $this->viewClass = 'Media';
        $params = array(
         'id' => 'test.pdf',
         'name' => 'your_test' ,
         'download' => true,
         'extension' => 'pdf',
         'path' => APP . 'files/pdf' . DS
        );
         $this->set($params);
      }
    
    ?>
    

    If we want to save or download our pdf file then we will use following URL:

    www.ourdomain.com/users/save_pdf

     

 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: