Create PDF from an item (FPDF)
Posted: 2019-10-27 17:29
Hi, a quick example for export an item in PDF (I use in my CRM).
Difficulty: ****
Useful Links and Tips
Difficulty: ****
Useful Links and Tips
- FPDF documentation for create your PDF: http://www.fpdf.org/en/doc/index.php
- Item who you can found in this post code is the name of tours table name, check before use!
- Add in hook a folder "includes" and inside a folder "pdf" with fpdfh.php and font from FPDF.
- In your /hook folder add a new file as item-dv.js and add a new button who point at a new PHP file (complete code before use).
Code: Select all
function create_pdf() {
var template = option.toLowerCase().replace(/\s/g, '_') + ".php";
window.location = "hooks/includes/pdf/pdf.php?id=" + selected_id;
}
$j(function() {
var selected_id = $j('[name=SelectedID]').val();
$j('#Item_dv_action_buttons .btn-toolbar').append(
'<p></p><div class="btn-group-vertical btn-group-lg" style="width: 100%;">' +
'<button type="button" class="btn btn-default btn-lg" onclick="create_pdf()">' +
'<i class="glyphicon glyphicon-print"></i> Create PDF</button>' +
'</div>'
);
});
- Add in /hook/includes/pdf/ a new file called pdf.php with this code (you must change for your use, check FPDF doc).
Code: Select all
<?php
include(dirname(__FILE__) . '/../../../lib.php');
require(dirname(__FILE__) . 'fpdfh.php');
/* grant access to the groups 'Admins' and 'Editor' */
$mi = getMemberInfo();
if(in_array($mi['group'], array('Admins', 'Editor'))){
$id = max(0, intval($_REQUEST['id']));
if(!$id){
header('Location: ' . PREPEND_PATH . 'Item_view.php');
exit;
}
$results = print_item_query($id);
$pdf = new PDF_HTML();
$pdf->AddPage();
$pdf->SetFont('Times','B',14);
$pdf->Cell(0,10,'Item: '. $results['id'],1,1);
$pdf->Cell(0,10,'Description: '. $results['description'],1,1);
$pdf->Output();
exit;
}