1

I have a clean HTML page. I need to print (download) it as a pdf file. I have searched in internet. But everyone gives some plugins and libraries. Can I do that without those? I mean how to download as pdf using php code. I am using codeigniter

<table><tr>
<td>Name</td>
<td><?php echo $name;?></td>
</tr></table>

This is how my code looks. $name value will be dynamic

Joel James
  • 1,315
  • 1
  • 20
  • 37
  • PDF and HTML are two very different beasts. Most modern browsers can render PDFs natively but they use a completely different engine than the HTML rendering engine. I say this so that you know that you can't just switch to "PDF mode" on the fly. PDF is a binary file format almost as old as HTML and looks absolutely nothing like HTML. The PDF spec is open so you can definitely write it using PHP but unless you're bored (like me) it will take you a while to read through the 1300+ pages. It is really much easier to just use a tried and true library. For PHP I often use FPDF http://www.fpdf.org/ – Chris Haas Apr 22 '14 at 03:21

3 Answers3

0

I think you will have to go through the plugin/library route. Not sure if PHP natively provides PDF generation capability.

There is one nice opensource library I have used for PDF generation but it has a steep learning curve if you are not familiar with XSLT. Its called XSL-FO.

Apache FOP is a nice FO Processor which I have used.

And here is a tutorial link I just googled to use XSL-FO with PHP. This was the first link which I encountered I am sure you can find many others which might be better than this one.

Sachin Sharma
  • 585
  • 1
  • 5
  • 16
0

simple way to create html page to pdf using dompdf

your library of dom pdf

<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * CodeIgniter PDF Library
 *
 * Generate PDF's in your CodeIgniter applications.
 *
 * @package         CodeIgniter
 * @subpackage      Libraries
 * @category        Libraries
 * @author          Chris Harvey
 * @license         MIT License
 * @link            https://github.com/chrisnharvey/CodeIgniter-PDF-Generator-Library
 */

require_once(dirname(__FILE__) . '/dompdf/dompdf_config.inc.php');

class Pdf extends DOMPDF
{
    /**
     * Get an instance of CodeIgniter
     *
     * @access  protected
     * @return  void
     */
    protected function ci()
    {
        return get_instance();
    }

    /**
     * Load a CodeIgniter view into domPDF
     *
     * @access  public
     * @param   string  $view The view to load
     * @param   array   $data The view data
     * @return  void
     */
    public function load_view($view, $data = array())
    {
        $html = $this->ci()->load->view($view, $data, TRUE);

        $this->load_html($html);
    }
}

you can download the dompdf folder

put int to the library folder

https://github.com/dompdf/dompdf

controller like this call

       $data['etc_data'] =     $this->model->data($id); 
      $data['evn_etc_data'] =  $this->model->ext_data($id);
      $this->load->view('pdf',$data);     
      $this->load->view('pdf',$data);
      $this->load->library('pdf');
      $this->pdf->load_view('pdf',$data);
      $this->pdf->render();
      $this->pdf->stream("test.pdf");

your view like this

<?php print_r($etc_data);?>
<?php print_r($evn_etc_data);?>

//here write your html code and your logic other wise you can see this link implement based helper

https://github.com/EllisLab/CodeIgniter/wiki/PDF-generation-using-dompdf

Raja Ram T
  • 854
  • 8
  • 8
-1

You can use window.print function for this.

<script type="text/javascript">
$(document).ready(function(){

$('#pdf').click(function(){

   printDiv();
    function printDiv() {

    var printContents = $(".content").html();

 var originalContents = document.body.innerHTML;
 document.body.innerHTML = printContents;
 window.print();
 document.body.innerHTML = originalContents;
 }



});
});

</script>

where content is div class name. You can create pdf of that div.

Kedar B
  • 774
  • 5
  • 18
  • 47