1<?php
2
3/*****************************************************************************/
4/** Inclusions */
5
6  define ( "HTML2PS_BASEDIR", "@@@@ change this value for yours needs @@@@" );
7  require_once ( HTML2PS_BASEDIR . "config.inc.php" );
8  require_once ( HTML2PS_DIR . "pipeline.factory.class.php" );
9  require_once ( HTML2PS_DIR . "fetcher.url.class.php" );
10
11
12/*****************************************************************************/
13/** Definitions */
14
15  // ** Valid post processing directives
16  global $VALID_POST_PROCESSING_DIRECTIVES;
17                                           // DIRECTIVE            => true: multi-value, false: single-value
18  $VALID_POST_PROCESSING_DIRECTIVES = array ( // VISIBILITY SPECIFICATION (ids lists)
19                                              "only_first_page"    => true,
20                                              "only_last_page"     => true,
21                                              "all_but_first_page" => true,
22                                              "all_but_last_page"  => true,
23                                              "only_even_page"     => true,
24                                              "only_odd_page"      => true,
25                                              // MARGIN (mm)
26                                              "margin_left"        => false,
27                                              "margin_right"       => false,
28                                              "margin_top"         => false,
29                                              "margin_bottom"      => false,
30                                              // PAGE WIDTH (pixel)
31                                              "page_width"         => false
32                                            );
33
34
35/*****************************************************************************/
36/** Run-time settings */
37
38  // ** PHP
39  set_time_limit ( 10000 );
40
41  // ** HTML2PS
42  ini_set ( "user_agent", DEFAULT_USER_AGENT );
43  $g_css_index = 0;
44  $g_stylesheet_title = ""; // Title of styleshee to use (empty if no preferences are set)
45
46
47/*****************************************************************************/
48/** Parameters */
49
50  // Input url
51  $fi = trim ( urldecode ( get_var ( "fi", $_GET, 255, "" ) ) );
52  // Output file name (without ".pdf")
53  $fo = trim ( urldecode ( get_var ( "fo", $_GET, 255, "document" ) ) );
54  // Margin value in mm (see page definition in test.html)
55  $ml = (int) get_var ( "ml", $_GET, 255, -1 );
56  $mr = (int) get_var ( "mr", $_GET, 255, -1 );
57  $mt = (int) get_var ( "mt", $_GET, 255, -1 );
58  $mb = (int) get_var ( "mb", $_GET, 255, -1 );
59  // Page width
60  $pw = (int) get_var ( "pw", $_GET, 255, -1 );
61
62
63/*****************************************************************************/
64/** Parameters validation */
65
66  if ( $fi == "" ) { die ( "Wrong parameters." ); }
67
68
69/*****************************************************************************/
70/** Get post-processing information */
71
72  // *** Init
73  global $POST_PROCESSING_DIRECTIVES;
74  $POST_PROCESSING_DIRECTIVES = array();
75  // *** Get file content in row (array)
76  $filerows = file ( $fi );
77  if ( $filerows == false ) { die ( "Unable to get file content." ); }
78  // *** Search for directives block
79  $viewed_post_process_open  = false;
80  $viewed_post_process_close = false;
81  for ( $i = 0; $i < count ( $filerows ); $i++ ) {
82    if ( strpos ( trim ( $filerows[$i] ), "HTML2PDF_POST_PROCESSING_DIRECTIVES -->" ) === 0 ) {
83      // Directives block ended
84      $viewed_post_process_close = true;
85      break;
86    }
87    if ( $viewed_post_process_open ) { // Am i in directives lock?
88      // Check if comment
89      if ( strpos ( trim ( $filerows[$i] ), "//" ) === 0 ) { continue; } // Skip comment line
90      // Normal line
91      $tmp = explode ( ":", $filerows[$i] );
92      $row_type = ( isset ( $tmp[0] ) ? trim ( $tmp[0] ) : "" );
93      $row_info = ( isset ( $tmp[1] ) ? trim ( $tmp[1] ) : "" );
94      // This row is a valid directive?
95      if ( ! isset ( $VALID_POST_PROCESSING_DIRECTIVES[$row_type] ) ) {
96        die ( "Unknown POST PROCESSING directive: |$row_type|." );
97      }
98      $mulval = $VALID_POST_PROCESSING_DIRECTIVES[$row_type];
99      // Save directive
100      $values = explode ( ",", $row_info );
101      if ( $mulval ) {
102        // Multi-value directive
103        if ( count ( $values ) > 0 ) {
104          if ( ! isset ( $POST_PROCESSING_DIRECTIVES[$row_type] ) ) {
105            $POST_PROCESSING_DIRECTIVES[$row_type] = $values;
106          } else {
107            $POST_PROCESSING_DIRECTIVES[$row_type] = array_merge ( $POST_PROCESSING_DIRECTIVES[$row_type], $values );
108          }
109        }
110      } else {
111        // Single-value directive
112        if ( ! isset ( $values[0] ) ) {
113          die ( "Specify a value for |$row_type| directive." );
114        }
115        $POST_PROCESSING_DIRECTIVES[$row_type] = $values[0];
116      }
117    }
118    if ( strpos ( trim ( $filerows[$i] ), "<!-- HTML2PDF_POST_PROCESSING_DIRECTIVES" ) === 0 ) {
119       // Directives block started
120       $viewed_post_process_open  = true;
121    }
122  }
123  if ( $viewed_post_process_open != $viewed_post_process_close ) {
124    die ( "Error reading POST PROCESSING directives." );
125  }
126
127
128/*****************************************************************************/
129/** Use post-processing information (not all of them yet) */
130
131  // ** Overwrite margin value with directives
132  if ( $ml == -1 ) {
133    $ml = ( isset ( $POST_PROCESSING_DIRECTIVES["margin_left"] ) ?
134            $POST_PROCESSING_DIRECTIVES["margin_left"] :
135            0 );
136  }
137  if ( $mr == -1 ) {
138    $mr = ( isset ( $POST_PROCESSING_DIRECTIVES["margin_right"] ) ?
139            $POST_PROCESSING_DIRECTIVES["margin_right"] :
140            0 );
141  }
142  if ( $mt == -1 ) {
143    $mt = ( isset ( $POST_PROCESSING_DIRECTIVES["margin_top"] ) ?
144            $POST_PROCESSING_DIRECTIVES["margin_top"] :
145            0 );
146  }
147  if ( $mb == -1 ) {
148    $mb = ( isset ( $POST_PROCESSING_DIRECTIVES["margin_bottom"] ) ?
149            $POST_PROCESSING_DIRECTIVES["margin_bottom"] :
150            0 );
151  }
152  // ** Overwrite page width value with directives
153  if ( $pw == -1 ) {
154    $pw = ( isset ( $POST_PROCESSING_DIRECTIVES["page_width"] ) ?
155            $POST_PROCESSING_DIRECTIVES["page_width"] :
156            800 );
157  }
158
159
160/*****************************************************************************/
161/** Configuration */
162
163  $GLOBALS['g_config'] = array ( 'cssmedia'                => "screen",
164                                 'media'                   => "A4",
165                                 'scalepoints'             => true,
166                                 'renderimages'            => true,
167                                 'renderfields'            => true,
168                                 'renderforms'             => false,
169                                 'pslevel'                 => 3,
170                                 'renderlinks'             => true,
171                                 'pagewidth'               => $pw,
172                                 'landscape'               => false,
173                                 'method'                  => "fpdf",
174                                 'margins'                 => array ( 'left'   => $ml,
175                                                                      'right'  => $mr,
176                                                                      'top'    => $mt,
177                                                                      'bottom' => $mb,
178                                                                    ),
179                                 'encoding'                => "",
180                                 'ps2pdf'                  => false,
181                                 'compress'                => false,
182                                 'output'                  => 1,
183                                 'pdfversion'              => "1.2",
184                                 'transparency_workaround' => false,
185                                 'imagequality_workaround' => false,
186                                 'draw_page_border'        => false,
187                                 'debugbox'                => false,
188                                 'html2xhtml'              => true,
189                                 'mode'                    => 'html',
190                                 'smartpagebreak'          => true
191                               );
192
193
194/*****************************************************************************/
195/** Inizializza pipeline */
196
197  // ** Parse configuration file
198  parse_config_file ( HTML2PS_BASEDIR . "html2ps.config" );
199
200  // ** Media
201  $g_media = Media::predefined ( $GLOBALS['g_config']['media'] );
202  $g_media->set_landscape ( $GLOBALS['g_config']['landscape'] );
203  $g_media->set_margins ( $GLOBALS['g_config']['margins'] );
204  $g_media->set_pixels ( $GLOBALS['g_config']['pagewidth'] );
205
206  // ** Pipeline
207  // *** Initialize the coversion pipeline
208  $pipeline = new Pipeline();
209  // *** Fetchers
210  $pipeline->fetchers[] = new FetcherUrl();
211  // *** Data filters
212  $pipeline->data_filters[] = new DataFilterDoctype();
213  $pipeline->data_filters[] = new DataFilterUTF8 ( "" );
214  $pipeline->data_filters[] = new DataFilterHTML2XHTML();
215  // *** Parser
216  $pipeline->parser = new ParserXHTML();
217  // *** Pre-tree filters
218  $pipeline->pre_tree_filters = array();
219  $pipeline->pre_tree_filters[] = new PreTreeFilterHTML2PSFields();
220  // *** Layout engine
221  $pipeline->layout_engine = new LayoutEngineDefault();
222  // *** Post-tree filters
223  $pipeline->post_tree_filters = array();
224  // *** Output driver
225  $pipeline->output_driver = new OutputDriverFPDF();
226  // *** Destination
227  $pipeline->destination = new DestinationDownload ( $fo );
228
229  // *** Install event handler
230  $dispatcher =& $pipeline->getDispatcher();
231  $dispatcher->add_observer ( 'before-page', 'visible_hidden_by_id');
232
233
234/*****************************************************************************/
235/** Main */
236
237  // ** Generate PDF file
238  $status = $pipeline->process ( $fi, $g_media );
239  if ( $status == null ) {
240    print ( $pipeline->error_message() );
241    syslog ( LOG_ERR, "PHP: Error in conversion pipeline" );
242    die();
243  }
244
245
246/*****************************************************************************/
247/** Functions */
248
249  // ** Event handler
250
251  function visible_hidden_by_id ( $params ) {
252    global $POST_PROCESSING_DIRECTIVES;
253    // ** Read page number
254    $exppag = $params["pipeline"]->output_driver->get_expected_pages();
255    $pageno = $params["pageno"] + 1; // Plus 1 because in "before-page" pageno isn't yet incremented
256    // ** Show elements only in first page
257    if ( isset ( $POST_PROCESSING_DIRECTIVES["only_first_page"] ) ) {
258      $value = ( $pageno == 1 ? VISIBILITY_VISIBLE : VISIBILITY_HIDDEN );
259      foreach ( $POST_PROCESSING_DIRECTIVES["only_first_page"] as $k => $id ) {
260        $elem = $params["document"]->get_element_by_id ( trim ( $id ) );
261        if ( $elem != NULL ) {
262          $elem->setCSSProperty ( CSS_VISIBILITY, $value );
263        }
264      }
265    }
266    // ** Show elements only in last page
267    if ( isset ( $POST_PROCESSING_DIRECTIVES["only_last_page"] ) ) {
268      $value = ( $pageno == $exppag ? VISIBILITY_VISIBLE : VISIBILITY_HIDDEN );
269      foreach ( $POST_PROCESSING_DIRECTIVES["only_last_page"] as $k => $id ) {
270        $elem = $params["document"]->get_element_by_id ( trim ( $id ) );
271        if ( $elem != NULL ) {
272          $elem->setCSSProperty ( CSS_VISIBILITY, $value );
273        }
274      }
275    }
276    // ** Show elements in all pages but first
277    if ( isset ( $POST_PROCESSING_DIRECTIVES["all_but_first_page"] ) ) {
278      $value = ( $pageno != 1 ? VISIBILITY_VISIBLE : VISIBILITY_HIDDEN );
279      foreach ( $POST_PROCESSING_DIRECTIVES["all_but_first_page"] as $k => $id ) {
280        $elem = $params["document"]->get_element_by_id ( trim ( $id ) );
281        if ( $elem != NULL ) {
282          $elem->setCSSProperty ( CSS_VISIBILITY, $value );
283        }
284      }
285    }
286    // ** Show elements in all pages but last
287    if ( isset ( $POST_PROCESSING_DIRECTIVES["all_but_last_page"] ) ) {
288      $value = ( $pageno != $exppag ? VISIBILITY_VISIBLE : VISIBILITY_HIDDEN );
289      foreach ( $POST_PROCESSING_DIRECTIVES["all_but_last_page"] as $k => $id ) {
290        $elem = $params["document"]->get_element_by_id ( trim ( $id ) );
291        if ( $elem != NULL ) {
292          $elem->setCSSProperty ( CSS_VISIBILITY, $value );
293        }
294      }
295    }
296    // ** Show elements only in even pages
297    if ( isset ( $POST_PROCESSING_DIRECTIVES["only_even_page"] ) ) {
298      $value = ( $pageno % 2 == 0 ? VISIBILITY_VISIBLE : VISIBILITY_HIDDEN );
299      foreach ( $POST_PROCESSING_DIRECTIVES["only_even_page"] as $k => $id ) {
300        $elem = $params["document"]->get_element_by_id ( trim ( $id ) );
301        if ( $elem != NULL ) {
302          $elem->setCSSProperty ( CSS_VISIBILITY, $value );
303        }
304      }
305    }
306    // ** Show elements only in odd pages
307    if ( isset ( $POST_PROCESSING_DIRECTIVES["only_odd_page"] ) ) {
308      $value = ( $pageno % 2 != 0 ? VISIBILITY_VISIBLE : VISIBILITY_HIDDEN );
309      foreach ( $POST_PROCESSING_DIRECTIVES["only_odd_page"] as $k => $id ) {
310        $elem = $params["document"]->get_element_by_id ( trim ( $id ) );
311        if ( $elem != NULL ) {
312          $elem->setCSSProperty ( CSS_VISIBILITY, $value );
313        }
314      }
315    }
316  }
317
318  // ** Generic functions
319
320  function get_var ( $name, $array, $maxlength = 255, $default = null ) {
321    if ( ! isset ( $array[$name] ) ) { return $default; }
322    $data = $array[$name];
323    if ( is_array ( $data ) ) {
324      if ( get_magic_quotes_gpc() ) {
325        foreach ( $data as $key => $value ) {
326          $data[$key] = stripslashes ( $data[$key] );
327        }
328      }
329    } else {
330      if ( get_magic_quotes_gpc() ) {
331        $data = stripslashes ( $data );
332      }
333      $data = substr ( $data, 0, $maxlength );
334    }
335    return $data;
336  }
337
338
339?>