En devdoc80 » History » Version 1

Version 1/2 - Next » - Current version
remi, 09/12/2011 09:42 AM


Developer Documentation

This documentation apply to GLPI <= 0.80 only.

Generating PDF for a new type provided by a plugin

The plugin need to add 1 hook

$PLUGIN_HOOKS['plugin_pdf'][FOO_TYPE]='foo';

And 2 functions

/**
 * Hook : options for one type
 * 
 * @param $type of item
 * 
 * @return array of string which describe the options
 */
function plugin_foo_prefPDF($type) {
    global $LANG;
    switch ($type) {
        case FOO_TYPE:
            return array("Option bar1", "Option bar2");
    }
    return false;
}
/**
 * Hook to generate a PDF for foo type
 * 
 * @param $type of item
 * @param $tab_id array of ID
 * @param $tab of option to be printed
 * @param $page boolean true for landscape
 */
function plugin_foo_generatePDF($type, $tab_id, $tab, $page=0) {
    if ($type==FOO_TYPE) {
        plugin_foo_generalPDF($tab_id, $tab, $page);    
    }
}

prefPDF is used for user's preferences.

Tips :
  • use the result of $Type->defineTabs();

generatePDF is called to generate a PDF.

Tips :
  • see others plugins for code sample
  • PDF plugin provides function to render lot or common part (ticket, notes, document, history, ...)

Add plugin information to GLPI (or other plugin) types

The plugin to add 1 more hook.

    $PLUGIN_HOOKS['headings_actionpdf']['foo'] = 'plugin_headings_actionpdf_foo';

And a function (name define in the hook value)

/**
 * Tell if our plugin handle PDF for another type
 *
 * @param $type 
 * @return name of function to be called to render the PDF 
 */
function plugin_headings_actionpdf_foo($type){

   if ($type==COMPUTER_TYPE) {
      return array( 1 => "plugin_headings_foo_computer_PDF" );
   } else {
      return false;
   }    
}

And the function called to render

/**
 * Render PDF of data provided by foo plugin to computer
 *
 * @param $pdf : a simplePDF object
 * @param $ID : of the computer
 * @param $type : COMPUTER_TYPE only for our example
 * @return nothing (buf PDF)
 */
function plugin_headings_foo_computer_PDF($pdf,$ID,$type) {
   $pdf->setColumnsSize(100);
   $pdf->displayTitle('<b>A great example</b>');
   $pdf->displaySpace();
}

The SimplePDF class

This class provides simple methods to render PDF without need to know which backend (library) is used internally.

Here is a little sample code

$pdf->setHeader("The page header sample");

# Start a page
$pdf->newPage();

# Create a 2 columns layout (args are colums size in %)
$pdf->setColumnsSize(50,50);

# Display a tile line (dark background)
$pdf->displayTitle("first", "second");

# Display a ligne (light background)
$pdf->displayLine("first", "second");

# Display a "comment" line (only 1 column, multi-line)
$pdf->displayText("A long text\nwhich expand\non 3 lines");

# White separator space    
$pdf->displaySpace();

# generate the PDF
$pdf->render

Quite simple ?