Converting html to pdf after clicking download button in codeigniter

I've created a resume builder app. The user enters all the required data and I save all the data in the database. Then I fetch these posts and display them in different templates. I have created 3 templates. Now I want to download pdf and download as two docx download buttons. As soon as the user clicks on any of the buttons, the file will be downloaded in the selected format. Either pdf or docx.

Is it possible to achieve in codeigniter?

i used Tcpdf

Controller code

the code written in controller download $this - > load - > library("Pdf");
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set auto page breaks
$pdf - > SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf - > setImageScale(PDF_IMAGE_SCALE_RATIO);
// Add a page
// This method has several options, check the source code documentation for more information.
$pdf - > AddPage();
// Set some content to print 
$html = $this - > load - > view('resume/res_template1_view', array('left_sidebar' => 'sidebar/left_sidebar', '             right_sidebar' => 'sidebar/right_sidebar'), $data, TRUE);
// Print text using writeHTMLCell()
$pdf - > writeHTML($html, 0, 0, '', '', 0, 1, 0, true, '', true);
//$pdf->writeHTML($html, true, false, true, false, ''); 

// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf - > Output('resume.pdf', 'D');
      

Run codeHide result


+3


source to share


1 answer


Perhaps you need to use some library in CI to achieve this goal. Please check below. you can use Html2Pdf for CI3 library. https://github.com/shyamshingadiya/HTML2PDF-CI3

How to use

There are 4 options that you must set to create PDF; folder, file name, paper and HTML

//Load the library
$this->load->library('html2pdf');

//Set folder to save PDF to
$this->html2pdf->folder('./assets/pdfs/');

//Set the filename to save/download as
$this->html2pdf->filename('test.pdf');

//Set the paper defaults
$this->html2pdf->paper('a4', 'portrait');

//Load html view
$this->html2pdf->html(<h1>Some Title</h1><p>Some content in here</p>);

      

The create function has two options 'save'

and 'download'

. Save will simply write the PDF to the folder you choose in the settings. The download will force the PDF to download in the clients browser.



//Create the PDF
$this->html2pdf->create('save');

      

Extended use

There is no reason why it is not possible to create and pass a view to a function html()

. Also note that the function create()

will return the saved path if you select the option 'save'

.

$data = array(
    'title' => 'PDF Created',
    'message' => 'Hello World!'
);

//Load html view
$this->html2pdf->html($this->load->view('pdf', $data, true));

if($path = $this->html2pdf->create('save')) {
    //PDF was successfully saved or downloaded
    echo 'PDF saved to: ' . $path;
}

      

Please check the above solution. Let me know if this doesn't work.

0


source







All Articles