3 * DOMPDF - PHP5 HTML to PDF renderer
5 * File: $RCSfile: dompdf.php,v $
6 * Created on: 2004-06-22
8 * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca>
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.
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.
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
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.
30 * The latest version of DOMPDF might be available at:
31 * http://www.digitaljunkies.ca/dompdf
33 * dompdf.php is a simple script to drive DOMPDF. It can be executed from
34 * a browser or from the command line.
36 * @link http://www.digitaljunkies.ca/dompdf
37 * @copyright 2004 Benj Carson
38 * @author Benj Carson <benjcarson@digitaljunkies.ca>
43 /* $Id: dompdf.php,v 1.17 2006/07/07 21:31:02 benjcarson Exp $ */
46 * Display command line usage:
48 * Usage: ./dompdf.php [options] html_file
50 * html_file can be a filename, a url if fopen_wrappers are enabled, or the '-'
51 * character to read from standard input.
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
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.
70 function dompdf_usage() {
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".
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";
91 function getoptions() {
95 if ( $_SERVER["argc"] == 1 )
99 while ($i < $_SERVER["argc"]) {
101 switch ($_SERVER["argv"][$i]) {
115 if ( !isset($_SERVER["argv"][$i+
1]) )
116 die("-p switch requires a size parameter\n");
117 $opts["p"] = $_SERVER["argv"][$i+
1];
122 if ( !isset($_SERVER["argv"][$i+
1]) )
123 die("-o switch requires an orientation parameter\n");
124 $opts["o"] = $_SERVER["argv"][$i+
1];
129 if ( !isset($_SERVER["argv"][$i+
1]) )
130 die("-b switch requires an path parameter\n");
131 $opts["b"] = $_SERVER["argv"][$i+
1];
136 if ( !isset($_SERVER["argv"][$i+
1]) )
137 die("-f switch requires an filename parameter\n");
138 $opts["f"] = $_SERVER["argv"][$i+
1];
153 $opts["filename"] = $_SERVER["argv"][$i];
162 require_once("dompdf_config.inc.php");
163 global $_dompdf_show_warnings;
164 global $_dompdf_debug;
166 $old_limit = ini_set("memory_limit", "80M");
168 $sapi = php_sapi_name();
174 $opts = getoptions();
176 if ( isset($opts["h"]) ||
(!isset($opts["filename"]) && !isset($opts["l"])) ) {
181 if ( isset($opts["l"]) ) {
182 echo "\nUnderstood paper sizes:\n";
184 foreach (array_keys(CPDF_Adapter
::$PAPER_SIZES) as $size)
185 echo " " . mb_strtoupper($size) . "\n";
188 $file = $opts["filename"];
190 if ( isset($opts["p"]) )
193 $paper = DOMPDF_DEFAULT_PAPER_SIZE
;
195 if ( isset($opts["o"]) )
196 $orientation = $opts["o"];
198 $orientation = "portrait";
200 if ( isset($opts["b"]) )
201 $base_path = $opts["b"];
203 if ( isset($opts["f"]) )
204 $outfile = $opts["f"];
207 $outfile = "dompdf_out.pdf";
209 $outfile = str_ireplace(array(".html", ".htm", ".php"), "", $file) . ".pdf";
212 if ( isset($opts["v"]) )
213 $_dompdf_show_warnings = true;
215 if ( isset($opts["d"]) ) {
216 $_dompdf_show_warnings = true;
217 $_dompdf_debug = true;
226 if ( isset($_GET["input_file"]) )
227 $file = rawurldecode($_GET["input_file"]);
229 throw new DOMPDF_Exception("An input file is required (i.e. input_file _GET variable).");
231 if ( isset($_GET["paper"]) )
232 $paper = rawurldecode($_GET["paper"]);
234 $paper = DOMPDF_DEFAULT_PAPER_SIZE
;
236 if ( isset($_GET["orientation"]) )
237 $orientation = rawurldecode($_GET["orientation"]);
239 $orientation = "portrait";
241 if ( isset($_GET["base_path"]) )
242 $base_path = rawurldecode($_GET["base_path"]);
244 if ( isset($_GET["output_file"]) )
245 $outfile = rawurldecode($_GET["output_file"]);
247 $outfile = "dompdf_out.pdf";
249 if ( isset($_GET["save_file"]) )
257 $dompdf = new DOMPDF();
259 if ( $file == "-" ) {
261 while ( !feof(STDIN
) )
262 $str .= fread(STDIN
, 4096);
264 $dompdf->load_html($str);
267 $dompdf->load_html_file($file);
269 if ( isset($base_path) ) {
270 $dompdf->set_base_path($base_path);
273 $dompdf->set_paper($paper, $orientation);
277 if ( $_dompdf_show_warnings ) {
278 foreach ($_dompdf_warnings as $msg)
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);
289 file_put_contents($outfile, $dompdf->output());
293 if ( !headers_sent() ) {
294 $dompdf->stream($outfile);