439
edits
(Added how to make a generic customfields pdf template) |
|||
| Line 2: | Line 2: | ||
If you had an experience with CustomFields, you can also add here your case as an example for others to get inspired. | If you had an experience with CustomFields, you can also add here your case as an example for others to get inspired. | ||
= How to make a generic PDF template listing all custom fields = | |||
To make a generic PDF template listing all custom fields, you just need to take an existing PDF template, then you need to follow these 3 steps: | |||
1/ Edit the class name (required, else it will conflict with the original template). | |||
Eg: if we take the crabe template for invoices, we change: | |||
<source lang="php"> | |||
class pdf_crabe extends ModelePDFFactures { | |||
// ... php code ... | |||
function __constructor($db) { | |||
// ... php code ... | |||
$this->name = "crabe"; | |||
</source> | |||
into: | |||
<source lang="php"> | |||
class pdf_customfields extends ModelePDFFactures { | |||
// ... php code ... | |||
function __constructor($db) { | |||
// ... php code ... | |||
$this->name = "customfields"; | |||
</source> | |||
2/ Copy this generic function at the end of the PDF: | |||
<source lang="php"> | |||
/** | |||
* \brief Show the customfields in a new page | |||
* \param pdf PDF factory | |||
* \param object Object invoice/propale/order/etc... (CustomFields simpler functions will automatically adapt) | |||
* \param outputlangs Object lang for output | |||
*/ | |||
function _pagecustomfields(&$pdf,$object,$outputlangs) | |||
{ | |||
global $conf; | |||
$default_font_size = pdf_getPDFFontSize($outputlangs); // set default PDF font size | |||
// Init and main vars | |||
include_once(DOL_DOCUMENT_ROOT.'/customfields/lib/customfields_aux.lib.php'); | |||
// Filling the $object with customfields (you can then access customfields by doing $object->customfields->cf_yourfield) | |||
$customfields = customfields_fill_object($object, null, $outputlangs, null, true); | |||
// Setting the starting position of the text cursor | |||
$pdf->SetXY($this->page_largeur - $this->marge_droite - ($pdf->GetStringWidth($titre) + 3), $pdf->GetY()+4); | |||
$pdf->SetY($pdf->GetY()+1); | |||
// Printing the customfields | |||
foreach ($object->customfields as $label=>$value) { // $value is already formatted! | |||
// Get translated label | |||
$translatedlabel = $customfields->findLabelPDF($label, $outputlangs); // translated label of the customfield (not translated by default in customfields_fill_object() because a field should always be accessible by a base name, whatever the translation is) | |||
// PDF formatting, placement and printing | |||
$pdf->SetFont('','B', $default_font_size); | |||
$pdf->MultiCell(0,3, $translatedlabel.' ($object->customfields->'.$label.'): '.$value, 0, 'L'); // printing the customfield | |||
$pdf->SetY($pdf->GetY()+1); // line return for the next printing | |||
} | |||
return 1; | |||
} | |||
</source> | |||
3/ Call the _pagecustomfields function at the right place (generally, after _pagefoot and just before $pdf->Close()): | |||
<source lang="php"> | |||
// Pied de page | |||
$this->_pagefoot($pdf,$object,$outputlangs); | |||
$pdf->AliasNbPages(); | |||
// CustomFields | |||
if ($conf->global->MAIN_MODULE_CUSTOMFIELDS) { // if the customfields module is activated... | |||
// Add a page with the customfields | |||
$pdf->AddPage(); | |||
$this->_pagecustomfields($pdf,$object,$outputlangs); | |||
$pagenb++; | |||
} | |||
$pdf->Close(); // you should first search for this, you should only find one occurrence | |||
</source> | |||
Then go to the module's admin panel (eg: invoice admin panel) and enable the template you've just created. | |||
You can now use it to test if your customfields work. | |||
= Conditions on fields selection = | = Conditions on fields selection = | ||
edits