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,v 1.17 2006/07/07 21:31:02 benjcarson Exp $ */ |
---|
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 | * |
---|
67 | * |
---|
68 | */ |
---|
69 | |
---|
70 | function dompdf_usage() { |
---|
71 | echo |
---|
72 | "\nUsage: {$_SERVER["argv"][0]} [options] html_file\n\n". |
---|
73 | "html_file can be a filename, a url if fopen_wrappers are enabled, or the '-' \n". |
---|
74 | "character to read from standard input.\n\n". |
---|
75 | "Options:\n". |
---|
76 | " -h\t\tShow this message\n". |
---|
77 | " -l\t\tlist available paper sizes\n". |
---|
78 | " -p size\tpaper size; something like 'letter', 'A4', 'legal', etc. The default is\n". |
---|
79 | " \t\t'" . DOMPDF_DEFAULT_PAPER_SIZE . "'\n". |
---|
80 | " -o orientation\teither 'portrait' or 'landscape'. Default is 'portrait'.\n". |
---|
81 | " -b path\tset the 'document root' of the html_file. Relative urls (for \n". |
---|
82 | " \tstylesheets) are resolved using this directory. Default is the \n". |
---|
83 | " \tdirectory of html_file.\n". |
---|
84 | " -f file\tthe output filename. Default is the input [html_file].pdf.\n". |
---|
85 | " -v \tverbose: display html parsing warnings and file not found errors.\n". |
---|
86 | " -d \tvery verbose: display oodles of debugging output: every frame\n". |
---|
87 | " \tin the tree printed to stdout.\n\n"; |
---|
88 | |
---|
89 | } |
---|
90 | |
---|
91 | function getoptions() { |
---|
92 | |
---|
93 | $opts = array(); |
---|
94 | |
---|
95 | if ( $_SERVER["argc"] == 1 ) |
---|
96 | return $opts; |
---|
97 | |
---|
98 | $i = 1; |
---|
99 | while ($i < $_SERVER["argc"]) { |
---|
100 | |
---|
101 | switch ($_SERVER["argv"][$i]) { |
---|
102 | |
---|
103 | case "--help": |
---|
104 | case "-h": |
---|
105 | $opts["h"] = true; |
---|
106 | $i++; |
---|
107 | break; |
---|
108 | |
---|
109 | case "-l": |
---|
110 | $opts["l"] = true; |
---|
111 | $i++; |
---|
112 | break; |
---|
113 | |
---|
114 | case "-p": |
---|
115 | if ( !isset($_SERVER["argv"][$i+1]) ) |
---|
116 | die("-p switch requires a size parameter\n"); |
---|
117 | $opts["p"] = $_SERVER["argv"][$i+1]; |
---|
118 | $i += 2; |
---|
119 | break; |
---|
120 | |
---|
121 | case "-o": |
---|
122 | if ( !isset($_SERVER["argv"][$i+1]) ) |
---|
123 | die("-o switch requires an orientation parameter\n"); |
---|
124 | $opts["o"] = $_SERVER["argv"][$i+1]; |
---|
125 | $i += 2; |
---|
126 | break; |
---|
127 | |
---|
128 | case "-b": |
---|
129 | if ( !isset($_SERVER["argv"][$i+1]) ) |
---|
130 | die("-b switch requires an path parameter\n"); |
---|
131 | $opts["b"] = $_SERVER["argv"][$i+1]; |
---|
132 | $i += 2; |
---|
133 | break; |
---|
134 | |
---|
135 | case "-f": |
---|
136 | if ( !isset($_SERVER["argv"][$i+1]) ) |
---|
137 | die("-f switch requires an filename parameter\n"); |
---|
138 | $opts["f"] = $_SERVER["argv"][$i+1]; |
---|
139 | $i += 2; |
---|
140 | break; |
---|
141 | |
---|
142 | case "-v": |
---|
143 | $opts["v"] = true; |
---|
144 | $i++; |
---|
145 | break; |
---|
146 | |
---|
147 | case "-d": |
---|
148 | $opts["d"] = true; |
---|
149 | $i++; |
---|
150 | break; |
---|
151 | |
---|
152 | default: |
---|
153 | $opts["filename"] = $_SERVER["argv"][$i]; |
---|
154 | $i++; |
---|
155 | break; |
---|
156 | } |
---|
157 | |
---|
158 | } |
---|
159 | return $opts; |
---|
160 | } |
---|
161 | |
---|
162 | require_once("dompdf_config.inc.php"); |
---|
163 | global $_dompdf_show_warnings; |
---|
164 | global $_dompdf_debug; |
---|
165 | |
---|
166 | $old_limit = ini_set("memory_limit", "80M"); |
---|
167 | |
---|
168 | $sapi = php_sapi_name(); |
---|
169 | |
---|
170 | switch ( $sapi ) { |
---|
171 | |
---|
172 | case "cli": |
---|
173 | |
---|
174 | $opts = getoptions(); |
---|
175 | |
---|
176 | if ( isset($opts["h"]) || (!isset($opts["filename"]) && !isset($opts["l"])) ) { |
---|
177 | dompdf_usage(); |
---|
178 | exit; |
---|
179 | } |
---|
180 | |
---|
181 | if ( isset($opts["l"]) ) { |
---|
182 | echo "\nUnderstood paper sizes:\n"; |
---|
183 | |
---|
184 | foreach (array_keys(CPDF_Adapter::$PAPER_SIZES) as $size) |
---|
185 | echo " " . mb_strtoupper($size) . "\n"; |
---|
186 | exit; |
---|
187 | } |
---|
188 | $file = $opts["filename"]; |
---|
189 | |
---|
190 | if ( isset($opts["p"]) ) |
---|
191 | $paper = $opts["p"]; |
---|
192 | else |
---|
193 | $paper = DOMPDF_DEFAULT_PAPER_SIZE; |
---|
194 | |
---|
195 | if ( isset($opts["o"]) ) |
---|
196 | $orientation = $opts["o"]; |
---|
197 | else |
---|
198 | $orientation = "portrait"; |
---|
199 | |
---|
200 | if ( isset($opts["b"]) ) |
---|
201 | $base_path = $opts["b"]; |
---|
202 | |
---|
203 | if ( isset($opts["f"]) ) |
---|
204 | $outfile = $opts["f"]; |
---|
205 | else { |
---|
206 | if ( $file == "-" ) |
---|
207 | $outfile = "dompdf_out.pdf"; |
---|
208 | else |
---|
209 | $outfile = str_ireplace(array(".html", ".htm", ".php"), "", $file) . ".pdf"; |
---|
210 | } |
---|
211 | |
---|
212 | if ( isset($opts["v"]) ) |
---|
213 | $_dompdf_show_warnings = true; |
---|
214 | |
---|
215 | if ( isset($opts["d"]) ) { |
---|
216 | $_dompdf_show_warnings = true; |
---|
217 | $_dompdf_debug = true; |
---|
218 | } |
---|
219 | |
---|
220 | $save_file = true; |
---|
221 | |
---|
222 | break; |
---|
223 | |
---|
224 | default: |
---|
225 | |
---|
226 | if ( isset($_GET["input_file"]) ) |
---|
227 | $file = rawurldecode($_GET["input_file"]); |
---|
228 | else |
---|
229 | throw new DOMPDF_Exception("An input file is required (i.e. input_file _GET variable)."); |
---|
230 | |
---|
231 | if ( isset($_GET["paper"]) ) |
---|
232 | $paper = rawurldecode($_GET["paper"]); |
---|
233 | else |
---|
234 | $paper = DOMPDF_DEFAULT_PAPER_SIZE; |
---|
235 | |
---|
236 | if ( isset($_GET["orientation"]) ) |
---|
237 | $orientation = rawurldecode($_GET["orientation"]); |
---|
238 | else |
---|
239 | $orientation = "portrait"; |
---|
240 | |
---|
241 | if ( isset($_GET["base_path"]) ) |
---|
242 | $base_path = rawurldecode($_GET["base_path"]); |
---|
243 | |
---|
244 | if ( isset($_GET["output_file"]) ) |
---|
245 | $outfile = rawurldecode($_GET["output_file"]); |
---|
246 | else |
---|
247 | $outfile = "dompdf_out.pdf"; |
---|
248 | |
---|
249 | if ( isset($_GET["save_file"]) ) |
---|
250 | $save_file = true; |
---|
251 | else |
---|
252 | $save_file = false; |
---|
253 | |
---|
254 | break; |
---|
255 | } |
---|
256 | |
---|
257 | $dompdf = new DOMPDF(); |
---|
258 | |
---|
259 | if ( $file == "-" ) { |
---|
260 | $str = ""; |
---|
261 | while ( !feof(STDIN) ) |
---|
262 | $str .= fread(STDIN, 4096); |
---|
263 | |
---|
264 | $dompdf->load_html($str); |
---|
265 | |
---|
266 | } else |
---|
267 | $dompdf->load_html_file($file); |
---|
268 | |
---|
269 | if ( isset($base_path) ) { |
---|
270 | $dompdf->set_base_path($base_path); |
---|
271 | } |
---|
272 | |
---|
273 | $dompdf->set_paper($paper, $orientation); |
---|
274 | |
---|
275 | $dompdf->render(); |
---|
276 | |
---|
277 | if ( $_dompdf_show_warnings ) { |
---|
278 | foreach ($_dompdf_warnings as $msg) |
---|
279 | echo $msg . "\n"; |
---|
280 | flush(); |
---|
281 | } |
---|
282 | |
---|
283 | if ( $save_file ) { |
---|
284 | // if ( !is_writable($outfile) ) |
---|
285 | // throw new DOMPDF_Exception("'$outfile' is not writable."); |
---|
286 | if ( strtolower(DOMPDF_PDF_BACKEND) == "gd" ) |
---|
287 | $outfile = str_replace(".pdf", ".png", $outfile); |
---|
288 | |
---|
289 | file_put_contents($outfile, $dompdf->output()); |
---|
290 | exit(0); |
---|
291 | } |
---|
292 | |
---|
293 | if ( !headers_sent() ) { |
---|
294 | $dompdf->stream($outfile); |
---|
295 | } |
---|
296 | ?> |
---|