En devdoc80 » History » Version 2

remi, 09/12/2011 09:42 AM

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