In the view page "ShowInformation.php" I have the code something like this including the omit css style code and other stuff
<?php foreach ($preUser as $value): ?>
Citizen ID : <?php echo $value->cid;?></br>
First Name : <?php echo $value->name;?></br>
...
<?php endforeach; ?>
$preUser is a variable passing from the controller and its from database.
I need to add the download button in that page and generate the PDF with exact the same PHP/HTML/CSS, supporting UTF-8.
Any suggestion ?
i'm using html2pdf and it's fine with CI 2+.
it took me sometime to find the right examples for my case but i finally succeeded.
Here's how i did :
1- INSTALL
Get html2pdf at: http://www.html2pdf.fr/en
Copy html2pdf folder into your CI/application/third_party/
*(you'll get something like: application/third_party/html2pdf/html2pdf.class.php)*
2- The controller function to process PDF
<?php
class Somecontroller {
function dopdf($mydat){
$this->CI->output->enable_profiler(false);
$this->CI->load->library('parser');
require_once(APPPATH.'third_party/html2pdf/html2pdf.class.php');
// set vars
$tpl_path = 'path/to/view_tpl.php';
$thefullpath = '/path/to/file_pdf.pdf';
$preview = false;
$previewpath = '/path/to/preview_pdf.pdf';
// PDFs datas
$datas = array(
'first_name' => $mydat->first_name,
'last_name' => $mydat->last_name,
'site_title' => config_item('site_title'),
);
// Encode datas to utf8
$tpl_data = array_map('utf8_encode',$datas);
//
// GENERATE PDF AND SAVE FILE (OR OUTPUT)
//
$content = $this->CI->parser->parse($tpl_path, $tpl_data, TRUE);
$html2pdf = new HTML2PDF('P','A4','fr', true, 'UTF-8', array(7, 7, 10, 10));
$html2pdf->pdf->SetAuthor($tpl_data['site_title']);
$html2pdf->pdf->SetTitle($tpl_data['site_title']);
$html2pdf->pdf->SetSubject($tpl_data['site_title']);
$html2pdf->pdf->SetKeywords($tpl_data['site_title']);
$html2pdf->pdf->SetProtection(array('print'), '');//allow only view/print
$html2pdf->WriteHTML($content);
if (!$preview) //save
$html2pdf->Output($thefullpath, 'F');
else { //save and load
$html2pdf->Output($previewpath, 'D');
}
}
}
3- The view file
Create a template like file in "application/views/path/to/view_tpl.php"
Use placeholders like {myvar} instead of <?php echo $myvar ?>
Remember! do not include: html,head, body tags !
<!-- do not include: html,head, body tags -->
<style type="text/css">
<!--
P {text-align:justify;font-size: 12pt;}
li {text-align:justify;font-size: 12pt;}
table.page_footer {width: 100%; border: none; border-top: solid 1px #000000; }
-->
</style>
<page backtop="14mm" backbottom="14mm" backleft="10mm" backright="10mm" style="font-size: 12pt">
<page_footer>
<table class="page_footer">
<tr>
<td style="width: 100%; text-align: right">
page [[page_cu]]/[[page_nb]]
</td>
</tr>
</table>
</page_footer>
<div style="width:100%;border: 1px solid #000000;text-align:center">
<h1><b>{site_title}</b></h1></div>
<p>
Firstname: {first_name}<br>
lastname: {last_name}<br>
</p>
<img src="<?php echo BASE_URL.'assets/img/test.png'; ?>" width="490" height="306">
</page>
4. PDF FILE DESTINATION
The pdf can be saved anywhere. Just specify a destination in the controller function (see $thefullpath
and $previewpath
)
Hope it can help !
WOW I worked
but I have my doubts this is the best way?
CI 2.1.3
public function mostrar()
{
require_once(APPPATH.'third_party/html2pdf/html2pdf.class.php');
//vista template pdf
$template_pdf = $this->load->view('pdf_mostrar', '', TRUE);
$html2pdf = new HTML2PDF('P','A4','fr');
$html2pdf->WriteHTML($template_pdf);
$html2pdf->Output('exemple.pdf');
}