1 | <?php
|
---|
2 | /**
|
---|
3 | * DOMPDF - PHP5 HTML to PDF renderer
|
---|
4 | *
|
---|
5 | * File: $RCSfile: dompdf.php,v $
|
---|
6 | * Created on: 2004-06-22
|
---|
7 | *
|
---|
8 | * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca>
|
---|
9 | *
|
---|
10 | * This library is free software; you can redistribute it and/or
|
---|
11 | * modify it under the terms of the GNU Lesser General Public
|
---|
12 | * License as published by the Free Software Foundation; either
|
---|
13 | * version 2.1 of the License, or (at your option) any later version.
|
---|
14 | *
|
---|
15 | * This library is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
18 | * Lesser General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU Lesser General Public License
|
---|
21 | * along with this library in the file LICENSE.LGPL; if not, write to the
|
---|
22 | * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
23 | * 02111-1307 USA
|
---|
24 | *
|
---|
25 | * Alternatively, you may distribute this software under the terms of the
|
---|
26 | * PHP License, version 3.0 or later. A copy of this license should have
|
---|
27 | * been distributed with this file in the file LICENSE.PHP . If this is not
|
---|
28 | * the case, you can obtain a copy at http://www.php.net/license/3_0.txt.
|
---|
29 | *
|
---|
30 | * The latest version of DOMPDF might be available at:
|
---|
31 | * http://www.digitaljunkies.ca/dompdf
|
---|
32 | *
|
---|
33 | * dompdf.php is a simple script to drive DOMPDF. It can be executed from
|
---|
34 | * a browser or from the command line.
|
---|
35 | *
|
---|
36 | * @link http://www.digitaljunkies.ca/dompdf
|
---|
37 | * @copyright 2004 Benj Carson
|
---|
38 | * @author Benj Carson <benjcarson@digitaljunkies.ca>
|
---|
39 | * @package dompdf
|
---|
40 | * @version 0.5.1
|
---|
41 | */
|
---|
42 |
|
---|
43 | /* $Id: dompdf.php 186 2009-10-19 22:42:06Z eclecticgeek@gmail.com $ */
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Display command line usage:
|
---|
47 | *
|
---|
48 | * Usage: ./dompdf.php [options] html_file
|
---|
49 | *
|
---|
50 | * html_file can be a filename, a url if fopen_wrappers are enabled, or the '-'
|
---|
51 | * character to read from standard input.
|
---|
52 | *
|
---|
53 | * Options:
|
---|
54 | * -h Show this message
|
---|
55 | * -l list available paper sizes
|
---|
56 | * -p size paper size; something like 'letter', 'A4', 'legal', etc. The default is
|
---|
57 | * 'letter'
|
---|
58 | * -o orientation either 'portrait' or 'landscape'. Default is 'portrait'.
|
---|
59 | * -b path set the 'document root' of the html_file. Relative urls (for
|
---|
60 | * stylesheets) are resolved using this directory. Default is the
|
---|
61 | * directory of html_file.
|
---|
62 | * -f file the output filename. Default is the input [html_file].pdf.
|
---|
63 | * -v verbose: display html parsing warnings and file not found errors.
|
---|
64 | * -d very verbose: display oodles of debugging output: every frame in the
|
---|
65 | * tree is printed to stdout.
|
---|
66 | * -t comma separated list of debugging types (page-break,reflow,split)
|
---|
67 | * -r write the render time to the log file
|
---|
68 | *
|
---|
69 | *
|
---|
70 | */
|
---|
71 |
|
---|
72 | function dompdf_usage() {
|
---|
73 | echo
|
---|
74 | "\nUsage: {$_SERVER["argv"][0]} [options] html_file\n\n".
|
---|
75 | "html_file can be a filename, a url if fopen_wrappers are enabled, or the '-' \n".
|
---|
76 | "character to read from standard input.\n\n".
|
---|
77 | "Options:\n".
|
---|
78 | " -h\t\tShow this message\n".
|
---|
79 | " -l\t\tlist available paper sizes\n".
|
---|
80 | " -p size\tpaper size; something like 'letter', 'A4', 'legal', etc. The default is\n".
|
---|
81 | " \t\t'" . DOMPDF_DEFAULT_PAPER_SIZE . "'\n".
|
---|
82 | " -o orientation\teither 'portrait' or 'landscape'. Default is 'portrait'.\n".
|
---|
83 | " -b path\tset the 'document root' of the html_file. Relative urls (for \n".
|
---|
84 | " \tstylesheets) are resolved using this directory. Default is the \n".
|
---|
85 | " \tdirectory of html_file.\n".
|
---|
86 | " -f file\tthe output filename. Default is the input [html_file].pdf.\n".
|
---|
87 | " -v \tverbose: display html parsing warnings and file not found errors.\n".
|
---|
88 | " -d \tvery verbose: display oodles of debugging output: every frame\n".
|
---|
89 | " \tin the tree printed to stdout.\n".
|
---|
90 | " -t comma separated list of debugging types (page-break,reflow,split)\n\n";
|
---|
91 |
|
---|
92 |
|
---|
93 | }
|
---|
94 |
|
---|
95 | function getoptions() {
|
---|
96 |
|
---|
97 | $opts = array();
|
---|
98 |
|
---|
99 | if ( $_SERVER["argc"] == 1 )
|
---|
100 | return $opts;
|
---|
101 |
|
---|
102 | $i = 1;
|
---|
103 | while ($i < $_SERVER["argc"]) {
|
---|
104 |
|
---|
105 | switch ($_SERVER["argv"][$i]) {
|
---|
106 |
|
---|
107 | case "--help":
|
---|
108 | case "-h":
|
---|
109 | $opts["h"] = true;
|
---|
110 | $i++;
|
---|
111 | break;
|
---|
112 |
|
---|
113 | case "-l":
|
---|
114 | $opts["l"] = true;
|
---|
115 | $i++;
|
---|
116 | break;
|
---|
117 |
|
---|
118 | case "-p":
|
---|
119 | if ( !isset($_SERVER["argv"][$i+1]) )
|
---|
120 | die("-p switch requires a size parameter\n");
|
---|
121 | $opts["p"] = $_SERVER["argv"][$i+1];
|
---|
122 | $i += 2;
|
---|
123 | break;
|
---|
124 |
|
---|
125 | case "-o":
|
---|
126 | if ( !isset($_SERVER["argv"][$i+1]) )
|
---|
127 | die("-o switch requires an orientation parameter\n");
|
---|
128 | $opts["o"] = $_SERVER["argv"][$i+1];
|
---|
129 | $i += 2;
|
---|
130 | break;
|
---|
131 |
|
---|
132 | case "-b":
|
---|
133 | if ( !isset($_SERVER["argv"][$i+1]) )
|
---|
134 | die("-b switch requires a path parameter\n");
|
---|
135 | $opts["b"] = $_SERVER["argv"][$i+1];
|
---|
136 | $i += 2;
|
---|
137 | break;
|
---|
138 |
|
---|
139 | case "-f":
|
---|
140 | if ( !isset($_SERVER["argv"][$i+1]) )
|
---|
141 | die("-f switch requires a filename parameter\n");
|
---|
142 | $opts["f"] = $_SERVER["argv"][$i+1];
|
---|
143 | $i += 2;
|
---|
144 | break;
|
---|
145 |
|
---|
146 | case "-v":
|
---|
147 | $opts["v"] = true;
|
---|
148 | $i++;
|
---|
149 | break;
|
---|
150 |
|
---|
151 | case "-d":
|
---|
152 | $opts["d"] = true;
|
---|
153 | $i++;
|
---|
154 | break;
|
---|
155 |
|
---|
156 | case "-t":
|
---|
157 | if ( !isset($_SERVER['argv'][$i + 1]) )
|
---|
158 | die("-t switch requires a comma separated list of types\n");
|
---|
159 | $opts["t"] = $_SERVER['argv'][$i+1];
|
---|
160 | $i += 2;
|
---|
161 | break;
|
---|
162 |
|
---|
163 | default:
|
---|
164 | $opts["filename"] = $_SERVER["argv"][$i];
|
---|
165 | $i++;
|
---|
166 | break;
|
---|
167 | }
|
---|
168 |
|
---|
169 | }
|
---|
170 | return $opts;
|
---|
171 | }
|
---|
172 |
|
---|
173 | require_once("dompdf_config.inc.php");
|
---|
174 | global $_dompdf_show_warnings;
|
---|
175 | global $_dompdf_debug;
|
---|
176 | global $_DOMPDF_DEBUG_TYPES;
|
---|
177 |
|
---|
178 | $sapi = php_sapi_name();
|
---|
179 |
|
---|
180 | switch ( $sapi ) {
|
---|
181 |
|
---|
182 | case "cli":
|
---|
183 |
|
---|
184 | $opts = getoptions();
|
---|
185 |
|
---|
186 | if ( isset($opts["h"]) || (!isset($opts["filename"]) && !isset($opts["l"])) ) {
|
---|
187 | dompdf_usage();
|
---|
188 | exit;
|
---|
189 | }
|
---|
190 |
|
---|
191 | if ( isset($opts["l"]) ) {
|
---|
192 | echo "\nUnderstood paper sizes:\n";
|
---|
193 |
|
---|
194 | foreach (array_keys(CPDF_Adapter::$PAPER_SIZES) as $size)
|
---|
195 | echo " " . mb_strtoupper($size) . "\n";
|
---|
196 | exit;
|
---|
197 | }
|
---|
198 | $file = $opts["filename"];
|
---|
199 |
|
---|
200 | if ( isset($opts["p"]) )
|
---|
201 | $paper = $opts["p"];
|
---|
202 | else
|
---|
203 | $paper = DOMPDF_DEFAULT_PAPER_SIZE;
|
---|
204 |
|
---|
205 | if ( isset($opts["o"]) )
|
---|
206 | $orientation = $opts["o"];
|
---|
207 | else
|
---|
208 | $orientation = "portrait";
|
---|
209 |
|
---|
210 | if ( isset($opts["b"]) )
|
---|
211 | $base_path = $opts["b"];
|
---|
212 |
|
---|
213 | if ( isset($opts["f"]) )
|
---|
214 | $outfile = $opts["f"];
|
---|
215 | else {
|
---|
216 | if ( $file == "-" )
|
---|
217 | $outfile = "dompdf_out.pdf";
|
---|
218 | else
|
---|
219 | $outfile = str_ireplace(array(".html", ".htm", ".php"), "", $file) . ".pdf";
|
---|
220 | }
|
---|
221 |
|
---|
222 | if ( isset($opts["v"]) )
|
---|
223 | $_dompdf_show_warnings = true;
|
---|
224 |
|
---|
225 | if ( isset($opts["d"]) ) {
|
---|
226 | $_dompdf_show_warnings = true;
|
---|
227 | $_dompdf_debug = true;
|
---|
228 | }
|
---|
229 |
|
---|
230 | if ( isset($opts['t']) ) {
|
---|
231 | $arr = preg_split('/,/',$opts['t']);
|
---|
232 | $types = array();
|
---|
233 | foreach ($arr as $type)
|
---|
234 | $types[ trim($type) ] = 1;
|
---|
235 | $_DOMPDF_DEBUG_TYPES = $types;
|
---|
236 | }
|
---|
237 |
|
---|
238 | $save_file = true;
|
---|
239 |
|
---|
240 | break;
|
---|
241 |
|
---|
242 | default:
|
---|
243 |
|
---|
244 | if ( isset($_GET["input_file"]) )
|
---|
245 | $file = basename(rawurldecode($_GET["input_file"]));
|
---|
246 | else
|
---|
247 | throw new DOMPDF_Exception("An input file is required (i.e. input_file _GET variable).");
|
---|
248 |
|
---|
249 | if ( isset($_GET["paper"]) )
|
---|
250 | $paper = rawurldecode($_GET["paper"]);
|
---|
251 | else
|
---|
252 | $paper = DOMPDF_DEFAULT_PAPER_SIZE;
|
---|
253 |
|
---|
254 | if ( isset($_GET["orientation"]) )
|
---|
255 | $orientation = rawurldecode($_GET["orientation"]);
|
---|
256 | else
|
---|
257 | $orientation = "portrait";
|
---|
258 |
|
---|
259 | if ( isset($_GET["base_path"]) )
|
---|
260 | $base_path = rawurldecode($_GET["base_path"]);
|
---|
261 |
|
---|
262 |
|
---|
263 | $outfile = "dompdf_out.pdf"; # Don't allow them to set the output file
|
---|
264 | $save_file = false; # Don't save the file
|
---|
265 | $file = $base_path . $file; # Set the input file
|
---|
266 |
|
---|
267 | /* Check to see if the input file and base path = www/test */
|
---|
268 | if(!$base_path=="www/test/")
|
---|
269 | throw new DOMPDF_Exception("Access to dompdf.php via non-cli SAPI has been deprecated due to security concerns. Please use the dompdf class directly.");
|
---|
270 |
|
---|
271 | break;
|
---|
272 | }
|
---|
273 |
|
---|
274 | $dompdf = new DOMPDF();
|
---|
275 |
|
---|
276 | if ( $file == "-" ) {
|
---|
277 | $str = "";
|
---|
278 | while ( !feof(STDIN) )
|
---|
279 | $str .= fread(STDIN, 4096);
|
---|
280 |
|
---|
281 | $dompdf->load_html($str);
|
---|
282 |
|
---|
283 | } else
|
---|
284 | $dompdf->load_html_file($file);
|
---|
285 |
|
---|
286 | if ( isset($base_path) ) {
|
---|
287 | $dompdf->set_base_path($base_path);
|
---|
288 | }
|
---|
289 |
|
---|
290 | $dompdf->set_paper($paper, $orientation);
|
---|
291 |
|
---|
292 | $dompdf->render();
|
---|
293 |
|
---|
294 | if ( $_dompdf_show_warnings ) {
|
---|
295 | global $_dompdf_warnings;
|
---|
296 | foreach ($_dompdf_warnings as $msg)
|
---|
297 | echo $msg . "\n";
|
---|
298 | echo $dompdf->get_canvas()->get_cpdf()->messages;
|
---|
299 | flush();
|
---|
300 | }
|
---|
301 |
|
---|
302 | if ( $save_file ) {
|
---|
303 | // if ( !is_writable($outfile) )
|
---|
304 | // throw new DOMPDF_Exception("'$outfile' is not writable.");
|
---|
305 | if ( strtolower(DOMPDF_PDF_BACKEND) == "gd" )
|
---|
306 | $outfile = str_replace(".pdf", ".png", $outfile);
|
---|
307 |
|
---|
308 | list($proto, $host, $path, $file) = explode_url($outfile);
|
---|
309 | if ( $proto != "" ) // i.e. not file://
|
---|
310 | $outfile = $file; // just save it locally, FIXME? could save it like wget: ./host/basepath/file
|
---|
311 |
|
---|
312 | $outfile = dompdf_realpath($outfile);
|
---|
313 |
|
---|
314 | if ( strpos($outfile, DOMPDF_CHROOT) !== 0 )
|
---|
315 | throw new DOMPDF_Exception("Permission denied.");
|
---|
316 |
|
---|
317 | file_put_contents($outfile, $dompdf->output( array("compress" => 0) ));
|
---|
318 | exit(0);
|
---|
319 | }
|
---|
320 |
|
---|
321 | if ( !headers_sent() ) {
|
---|
322 | $dompdf->stream($outfile);
|
---|
323 | }
|
---|
324 | ?> |
---|