En devdoc80 » History » Version 1

remi, 09/12/2011 09:42 AM

1 1 remi
h1. Developer Documentation
2 1 remi
3 1 remi
*This documentation apply to GLPI <= 0.80 only*.
4 1 remi
5 1 remi
6 1 remi
{{toc}}
7 1 remi
8 1 remi
h2. Generating PDF for a new type provided by a plugin
9 1 remi
10 1 remi
The plugin need to add 1 hook
11 1 remi
12 1 remi
<pre>
13 1 remi
$PLUGIN_HOOKS['plugin_pdf'][FOO_TYPE]='foo';
14 1 remi
</pre>
15 1 remi
16 1 remi
And 2 functions
17 1 remi
18 1 remi
<pre>
19 1 remi
/**
20 1 remi
 * Hook : options for one type
21 1 remi
 * 
22 1 remi
 * @param $type of item
23 1 remi
 * 
24 1 remi
 * @return array of string which describe the options
25 1 remi
 */
26 1 remi
function plugin_foo_prefPDF($type) {
27 1 remi
	global $LANG;
28 1 remi
	switch ($type) {
29 1 remi
		case FOO_TYPE:
30 1 remi
			return array("Option bar1", "Option bar2");
31 1 remi
	}
32 1 remi
	return false;
33 1 remi
}
34 1 remi
/**
35 1 remi
 * Hook to generate a PDF for foo type
36 1 remi
 * 
37 1 remi
 * @param $type of item
38 1 remi
 * @param $tab_id array of ID
39 1 remi
 * @param $tab of option to be printed
40 1 remi
 * @param $page boolean true for landscape
41 1 remi
 */
42 1 remi
function plugin_foo_generatePDF($type, $tab_id, $tab, $page=0) {
43 1 remi
	if ($type==FOO_TYPE) {
44 1 remi
		plugin_foo_generalPDF($tab_id, $tab, $page);	
45 1 remi
	}
46 1 remi
}</pre>
47 1 remi
48 1 remi
*prefPDF* is used for user's preferences.
49 1 remi
50 1 remi
_Tips_ : 
51 1 remi
* use the result of $Type->defineTabs();
52 1 remi
53 1 remi
*generatePDF* is called to generate a PDF.
54 1 remi
55 1 remi
_Tips_ : 
56 1 remi
* see others plugins for code sample
57 1 remi
* PDF plugin provides function to render lot or common part (ticket, notes, document, history, ...)
58 1 remi
59 1 remi
60 1 remi
h2. Add plugin information to GLPI (or other plugin) types
61 1 remi
62 1 remi
The plugin to add 1 more hook.
63 1 remi
64 1 remi
<pre>
65 1 remi
	$PLUGIN_HOOKS['headings_actionpdf']['foo'] = 'plugin_headings_actionpdf_foo';
66 1 remi
</pre>
67 1 remi
68 1 remi
And a function (name define in the hook value) 
69 1 remi
70 1 remi
<pre>
71 1 remi
/**
72 1 remi
 * Tell if our plugin handle PDF for another type
73 1 remi
 *
74 1 remi
 * @param $type 
75 1 remi
 * @return name of function to be called to render the PDF 
76 1 remi
 */
77 1 remi
function plugin_headings_actionpdf_foo($type){
78 1 remi
	
79 1 remi
   if ($type==COMPUTER_TYPE) {
80 1 remi
      return array( 1 => "plugin_headings_foo_computer_PDF" );
81 1 remi
   } else {
82 1 remi
      return false;
83 1 remi
   }	
84 1 remi
}
85 1 remi
</pre>
86 1 remi
87 1 remi
And the function called to render
88 1 remi
<pre>
89 1 remi
/**
90 1 remi
 * Render PDF of data provided by foo plugin to computer
91 1 remi
 *
92 1 remi
 * @param $pdf : a simplePDF object
93 1 remi
 * @param $ID : of the computer
94 1 remi
 * @param $type : COMPUTER_TYPE only for our example
95 1 remi
 * @return nothing (buf PDF)
96 1 remi
 */
97 1 remi
function plugin_headings_foo_computer_PDF($pdf,$ID,$type) {
98 1 remi
   $pdf->setColumnsSize(100);
99 1 remi
   $pdf->displayTitle('<b>A great example</b>');
100 1 remi
   $pdf->displaySpace();
101 1 remi
}
102 1 remi
</pre>
103 1 remi
104 1 remi
h2. The SimplePDF class
105 1 remi
106 1 remi
This class provides simple methods to render PDF without need to know which backend (library) is used internally.
107 1 remi
108 1 remi
Here is a little sample code
109 1 remi
110 1 remi
<pre>
111 1 remi
$pdf->setHeader("The page header sample");
112 1 remi
113 1 remi
# Start a page
114 1 remi
$pdf->newPage();
115 1 remi
116 1 remi
# Create a 2 columns layout (args are colums size in %)
117 1 remi
$pdf->setColumnsSize(50,50);
118 1 remi
119 1 remi
# Display a tile line (dark background)
120 1 remi
$pdf->displayTitle("first", "second");
121 1 remi
122 1 remi
# Display a ligne (light background)
123 1 remi
$pdf->displayLine("first", "second");
124 1 remi
125 1 remi
# Display a "comment" line (only 1 column, multi-line)
126 1 remi
$pdf->displayText("A long text\nwhich expand\non 3 lines");
127 1 remi
128 1 remi
# White separator space	
129 1 remi
$pdf->displaySpace();
130 1 remi
131 1 remi
# generate the PDF
132 1 remi
$pdf->render
133 1 remi
</pre>
134 1 remi
135 1 remi
_Quite simple ?_