1<?php
2// $Header: /cvsroot/html2ps/demo/html2ps.php,v 1.10 2007/05/17 13:55:13 Konstantin Exp $
3
4error_reporting(E_ALL);
5ini_set("display_errors","1");
6if (ini_get("pcre.backtrack_limit") < 1000000) { ini_set("pcre.backtrack_limit",1000000); };
7@set_time_limit(10000);
8
9require_once('generic.param.php');
10require_once('../config.inc.php');
11require_once(HTML2PS_DIR.'pipeline.factory.class.php');
12
13ini_set("user_agent", DEFAULT_USER_AGENT);
14
15$g_baseurl = trim(get_var('URL', $_REQUEST));
16
17if ($g_baseurl === "") {
18  die("Please specify URL to process!");
19}
20
21// Add HTTP protocol if none specified
22if (!preg_match("/^https?:/",$g_baseurl)) {
23  $g_baseurl = 'http://'.$g_baseurl;
24}
25
26$g_css_index = 0;
27
28// Title of styleshee to use (empty if no preferences are set)
29$g_stylesheet_title = "";
30
31$GLOBALS['g_config'] = array(
32                             'compress'      => isset($_REQUEST['compress']),
33                             'cssmedia'      => get_var('cssmedia', $_REQUEST, 255, "screen"),
34                             'debugbox'      => isset($_REQUEST['debugbox']),
35                             'debugnoclip'   => isset($_REQUEST['debugnoclip']),
36                             'draw_page_border'        => isset($_REQUEST['pageborder']),
37                             'encoding'      => get_var('encoding', $_REQUEST, 255, ""),
38                             'html2xhtml'    => !isset($_REQUEST['html2xhtml']),
39                             'imagequality_workaround' => isset($_REQUEST['imagequality_workaround']),
40                             'landscape'     => isset($_REQUEST['landscape']),
41                             'margins'       => array(
42                                                      'left'    => (int)get_var('leftmargin',   $_REQUEST, 10, 0),
43                                                      'right'   => (int)get_var('rightmargin',  $_REQUEST, 10, 0),
44                                                      'top'     => (int)get_var('topmargin',    $_REQUEST, 10, 0),
45                                                      'bottom'  => (int)get_var('bottommargin', $_REQUEST, 10, 0),
46                                                      ),
47                             'media'         => get_var('media', $_REQUEST, 255, "A4"),
48                             'method'        => get_var('method', $_REQUEST, 255, "fpdf"),
49                             'mode'          => 'html',
50                             'output'        => get_var('output', $_REQUEST, 255, ""),
51                             'pagewidth'     => (int)get_var('pixels', $_REQUEST, 10, 800),
52                             'pdfversion'    => get_var('pdfversion', $_REQUEST, 255, "1.2"),
53                             'ps2pdf'        => isset($_REQUEST['ps2pdf']),
54                             'pslevel'       => (int)get_var('pslevel', $_REQUEST, 1, 3),
55                             'renderfields'  => isset($_REQUEST['renderfields']),
56                             'renderforms'   => isset($_REQUEST['renderforms']),
57                             'renderimages'  => isset($_REQUEST['renderimages']),
58                             'renderlinks'   => isset($_REQUEST['renderlinks']),
59                             'scalepoints'   => isset($_REQUEST['scalepoints']),
60                             'smartpagebreak' => isset($_REQUEST['smartpagebreak']),
61                             'transparency_workaround' => isset($_REQUEST['transparency_workaround'])
62                             );
63
64$proxy = get_var('proxy', $_REQUEST, 255, '');
65
66// ========== Entry point
67parse_config_file('../html2ps.config');
68
69// validate input data
70if ($GLOBALS['g_config']['pagewidth'] == 0) {
71  die("Please specify non-zero value for the pixel width!");
72};
73
74// begin processing
75
76$g_media = Media::predefined($GLOBALS['g_config']['media']);
77$g_media->set_landscape($GLOBALS['g_config']['landscape']);
78$g_media->set_margins($GLOBALS['g_config']['margins']);
79$g_media->set_pixels($GLOBALS['g_config']['pagewidth']);
80
81// Initialize the coversion pipeline
82$pipeline = new Pipeline();
83$pipeline->configure($GLOBALS['g_config']);
84
85// Configure the fetchers
86if (extension_loaded('curl')) {
87  require_once(HTML2PS_DIR.'fetcher.url.curl.class.php');
88  $pipeline->fetchers = array(new FetcherUrlCurl());
89  if ($proxy != '') {
90    $pipeline->fetchers[0]->set_proxy($proxy);
91  };
92} else {
93  require_once(HTML2PS_DIR.'fetcher.url.class.php');
94  $pipeline->fetchers[] = new FetcherURL();
95};
96
97// Configure the data filters
98$pipeline->data_filters[] = new DataFilterDoctype();
99$pipeline->data_filters[] = new DataFilterUTF8($GLOBALS['g_config']['encoding']);
100if ($GLOBALS['g_config']['html2xhtml']) {
101  $pipeline->data_filters[] = new DataFilterHTML2XHTML();
102} else {
103  $pipeline->data_filters[] = new DataFilterXHTML2XHTML();
104};
105
106$pipeline->parser = new ParserXHTML();
107
108// "PRE" tree filters
109
110$pipeline->pre_tree_filters = array();
111
112$header_html    = get_var('headerhtml', $_REQUEST, 65535, "");
113$footer_html    = get_var('footerhtml', $_REQUEST, 65535, "");
114$filter = new PreTreeFilterHeaderFooter($header_html, $footer_html);
115$pipeline->pre_tree_filters[] = $filter;
116
117if ($GLOBALS['g_config']['renderfields']) {
118  $pipeline->pre_tree_filters[] = new PreTreeFilterHTML2PSFields();
119};
120
121//
122
123if ($GLOBALS['g_config']['method'] === 'ps') {
124  $pipeline->layout_engine = new LayoutEnginePS();
125} else {
126  $pipeline->layout_engine = new LayoutEngineDefault();
127};
128
129$pipeline->post_tree_filters = array();
130
131// Configure the output format
132if ($GLOBALS['g_config']['pslevel'] == 3) {
133  $image_encoder = new PSL3ImageEncoderStream();
134} else {
135  $image_encoder = new PSL2ImageEncoderStream();
136};
137
138switch ($GLOBALS['g_config']['method']) {
139 case 'fastps':
140   if ($GLOBALS['g_config']['pslevel'] == 3) {
141     $pipeline->output_driver = new OutputDriverFastPS($image_encoder);
142   } else {
143     $pipeline->output_driver = new OutputDriverFastPSLevel2($image_encoder);
144   };
145   break;
146 case 'pdflib':
147   $pipeline->output_driver = new OutputDriverPDFLIB16($GLOBALS['g_config']['pdfversion']);
148   break;
149 case 'fpdf':
150   $pipeline->output_driver = new OutputDriverFPDF();
151   break;
152 case 'png':
153   $pipeline->output_driver = new OutputDriverPNG();
154   break;
155 case 'pcl':
156   $pipeline->output_driver = new OutputDriverPCL();
157   break;
158 default:
159   die("Unknown output method");
160};
161
162// Setup watermark
163$watermark_text = trim(get_var('watermarkhtml', $_REQUEST, 65535, ""));
164if ($watermark_text != '') {
165  $pipeline->add_feature('watermark', array('text' => $watermark_text));
166};
167
168if ($GLOBALS['g_config']['debugbox']) {
169  $pipeline->output_driver->set_debug_boxes(true);
170}
171
172if ($GLOBALS['g_config']['draw_page_border']) {
173  $pipeline->output_driver->set_show_page_border(true);
174}
175
176if ($GLOBALS['g_config']['ps2pdf']) {
177  $pipeline->output_filters[] = new OutputFilterPS2PDF($GLOBALS['g_config']['pdfversion']);
178}
179
180if ($GLOBALS['g_config']['compress'] && $GLOBALS['g_config']['method'] == 'fastps') {
181  $pipeline->output_filters[] = new OutputFilterGZip();
182}
183
184if (get_var('process_mode', $_REQUEST) == 'batch') {
185  $filename = "batch";
186} else {
187  $filename = $g_baseurl;
188};
189
190switch ($GLOBALS['g_config']['output']) {
191 case 0:
192   $pipeline->destination = new DestinationBrowser($filename);
193   break;
194 case 1:
195   $pipeline->destination = new DestinationDownload($filename);
196   break;
197 case 2:
198   $pipeline->destination = new DestinationFile($filename, 'File saved as: <a href="%link%">%name%</a>');
199   break;
200};
201
202// Add additional requested features
203if (isset($_REQUEST['toc'])) {
204  $pipeline->add_feature('toc', array('location' => isset($_REQUEST['toc-location']) ? $_REQUEST['toc-location'] : 'after'));
205};
206
207if (isset($_REQUEST['automargins'])) {
208  $pipeline->add_feature('automargins', array());
209};
210
211// Start the conversion
212
213$time = time();
214if (get_var('process_mode', $_REQUEST) == 'batch') {
215  $batch = get_var('batch', $_REQUEST);
216
217  for ($i=0; $i<count($batch); $i++) {
218    if (trim($batch[$i]) != "") {
219      if (!preg_match("/^https?:/",$batch[$i])) {
220        $batch[$i] = "http://".$batch[$i];
221      }
222    };
223  };
224
225  $status = $pipeline->process_batch($batch, $g_media);
226} else {
227  $status = $pipeline->process($g_baseurl, $g_media);
228};
229
230error_log(sprintf("Processing of '%s' completed in %u seconds", $g_baseurl, time() - $time));
231
232if ($status == null) {
233  print($pipeline->error_message());
234  error_log("Error in conversion pipeline");
235  die();
236}
237
238?>