dompdf.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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.dompdf.com/
  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.dompdf.com/
  37. * @copyright 2004 Benj Carson
  38. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  39. * @package dompdf
  40. */
  41. /* $Id: dompdf.php 216 2010-03-11 22:49:18Z ryan.masten $ */
  42. /**
  43. * Display command line usage:
  44. *
  45. * Usage: ./dompdf.php [options] html_file
  46. *
  47. * html_file can be a filename, a url if fopen_wrappers are enabled, or the '-'
  48. * character to read from standard input.
  49. *
  50. * Options:
  51. * -h Show this message
  52. * -l list available paper sizes
  53. * -p size paper size; something like 'letter', 'A4', 'legal', etc. The default is
  54. * 'letter'
  55. * -o orientation either 'portrait' or 'landscape'. Default is 'portrait'.
  56. * -b path set the 'document root' of the html_file. Relative urls (for
  57. * stylesheets) are resolved using this directory. Default is the
  58. * directory of html_file.
  59. * -f file the output filename. Default is the input [html_file].pdf.
  60. * -v verbose: display html parsing warnings and file not found errors.
  61. * -d very verbose: display oodles of debugging output: every frame in the
  62. * tree is printed to stdout.
  63. * -t comma separated list of debugging types (page-break,reflow,split)
  64. * -r write the render time to the log file
  65. *
  66. *
  67. */
  68. function dompdf_usage() {
  69. echo
  70. "\nUsage: {$_SERVER["argv"][0]} [options] html_file\n\n".
  71. "html_file can be a filename, a url if fopen_wrappers are enabled, or the '-' \n".
  72. "character to read from standard input.\n\n".
  73. "Options:\n".
  74. " -h\t\tShow this message\n".
  75. " -l\t\tlist available paper sizes\n".
  76. " -p size\tpaper size; something like 'letter', 'A4', 'legal', etc. The default is\n".
  77. " \t\t'" . DOMPDF_DEFAULT_PAPER_SIZE . "'\n".
  78. " -o orientation\teither 'portrait' or 'landscape'. Default is 'portrait'.\n".
  79. " -b path\tset the 'document root' of the html_file. Relative urls (for \n".
  80. " \tstylesheets) are resolved using this directory. Default is the \n".
  81. " \tdirectory of html_file.\n".
  82. " -f file\tthe output filename. Default is the input [html_file].pdf.\n".
  83. " -v \tverbose: display html parsing warnings and file not found errors.\n".
  84. " -d \tvery verbose: display oodles of debugging output: every frame\n".
  85. " \tin the tree printed to stdout.\n".
  86. " -t comma separated list of debugging types (page-break,reflow,split)\n\n";
  87. }
  88. function getoptions() {
  89. $opts = array();
  90. if ( $_SERVER["argc"] == 1 )
  91. return $opts;
  92. $i = 1;
  93. while ($i < $_SERVER["argc"]) {
  94. switch ($_SERVER["argv"][$i]) {
  95. case "--help":
  96. case "-h":
  97. $opts["h"] = true;
  98. $i++;
  99. break;
  100. case "-l":
  101. $opts["l"] = true;
  102. $i++;
  103. break;
  104. case "-p":
  105. if ( !isset($_SERVER["argv"][$i+1]) )
  106. die("-p switch requires a size parameter\n");
  107. $opts["p"] = $_SERVER["argv"][$i+1];
  108. $i += 2;
  109. break;
  110. case "-o":
  111. if ( !isset($_SERVER["argv"][$i+1]) )
  112. die("-o switch requires an orientation parameter\n");
  113. $opts["o"] = $_SERVER["argv"][$i+1];
  114. $i += 2;
  115. break;
  116. case "-b":
  117. if ( !isset($_SERVER["argv"][$i+1]) )
  118. die("-b switch requires a path parameter\n");
  119. $opts["b"] = $_SERVER["argv"][$i+1];
  120. $i += 2;
  121. break;
  122. case "-f":
  123. if ( !isset($_SERVER["argv"][$i+1]) )
  124. die("-f switch requires a filename parameter\n");
  125. $opts["f"] = $_SERVER["argv"][$i+1];
  126. $i += 2;
  127. break;
  128. case "-v":
  129. $opts["v"] = true;
  130. $i++;
  131. break;
  132. case "-d":
  133. $opts["d"] = true;
  134. $i++;
  135. break;
  136. case "-t":
  137. if ( !isset($_SERVER['argv'][$i + 1]) )
  138. die("-t switch requires a comma separated list of types\n");
  139. $opts["t"] = $_SERVER['argv'][$i+1];
  140. $i += 2;
  141. break;
  142. default:
  143. $opts["filename"] = $_SERVER["argv"][$i];
  144. $i++;
  145. break;
  146. }
  147. }
  148. return $opts;
  149. }
  150. require_once("dompdf_config.inc.php");
  151. global $_dompdf_show_warnings;
  152. global $_dompdf_debug;
  153. global $_DOMPDF_DEBUG_TYPES;
  154. $sapi = php_sapi_name();
  155. switch ( $sapi ) {
  156. case "cli":
  157. $opts = getoptions();
  158. if ( isset($opts["h"]) || (!isset($opts["filename"]) && !isset($opts["l"])) ) {
  159. dompdf_usage();
  160. exit;
  161. }
  162. if ( isset($opts["l"]) ) {
  163. echo "\nUnderstood paper sizes:\n";
  164. foreach (array_keys(CPDF_Adapter::$PAPER_SIZES) as $size)
  165. echo " " . mb_strtoupper($size) . "\n";
  166. exit;
  167. }
  168. $file = $opts["filename"];
  169. if ( isset($opts["p"]) )
  170. $paper = $opts["p"];
  171. else
  172. $paper = DOMPDF_DEFAULT_PAPER_SIZE;
  173. if ( isset($opts["o"]) )
  174. $orientation = $opts["o"];
  175. else
  176. $orientation = "portrait";
  177. if ( isset($opts["b"]) )
  178. $base_path = $opts["b"];
  179. if ( isset($opts["f"]) )
  180. $outfile = $opts["f"];
  181. else {
  182. if ( $file === "-" )
  183. $outfile = "dompdf_out.pdf";
  184. else
  185. $outfile = str_ireplace(array(".html", ".htm", ".php"), "", $file) . ".pdf";
  186. }
  187. if ( isset($opts["v"]) )
  188. $_dompdf_show_warnings = true;
  189. if ( isset($opts["d"]) ) {
  190. $_dompdf_show_warnings = true;
  191. $_dompdf_debug = true;
  192. }
  193. if ( isset($opts['t']) ) {
  194. $arr = split(',',$opts['t']);
  195. $types = array();
  196. foreach ($arr as $type)
  197. $types[ trim($type) ] = 1;
  198. $_DOMPDF_DEBUG_TYPES = $types;
  199. }
  200. $save_file = true;
  201. break;
  202. default:
  203. if ( isset($_GET["input_file"]) )
  204. $file = basename(rawurldecode($_GET["input_file"]));
  205. else
  206. throw new DOMPDF_Exception("An input file is required (i.e. input_file _GET variable).");
  207. if ( isset($_GET["paper"]) )
  208. $paper = rawurldecode($_GET["paper"]);
  209. else
  210. $paper = DOMPDF_DEFAULT_PAPER_SIZE;
  211. if ( isset($_GET["orientation"]) )
  212. $orientation = rawurldecode($_GET["orientation"]);
  213. else
  214. $orientation = "portrait";
  215. if ( isset($_GET["base_path"]) )
  216. $base_path = rawurldecode($_GET["base_path"]);
  217. $outfile = "dompdf_out.pdf"; # Don't allow them to set the output file
  218. $save_file = false; # Don't save the file
  219. $file = $base_path . $file; # Set the input file
  220. /* Check to see if the input file and base path = www/test */
  221. if($base_path !== "www/test/")
  222. 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.");
  223. break;
  224. }
  225. $dompdf = new DOMPDF();
  226. if ( $file === "-" ) {
  227. $str = "";
  228. while ( !feof(STDIN) )
  229. $str .= fread(STDIN, 4096);
  230. $dompdf->load_html($str);
  231. } else
  232. $dompdf->load_html_file($file);
  233. if ( isset($base_path) ) {
  234. $dompdf->set_base_path($base_path);
  235. }
  236. $dompdf->set_paper($paper, $orientation);
  237. $dompdf->render();
  238. if ( $_dompdf_show_warnings ) {
  239. global $_dompdf_warnings;
  240. foreach ($_dompdf_warnings as $msg)
  241. echo $msg . "\n";
  242. echo $dompdf->get_canvas()->get_cpdf()->messages;
  243. flush();
  244. }
  245. if ( $save_file ) {
  246. // if ( !is_writable($outfile) )
  247. // throw new DOMPDF_Exception("'$outfile' is not writable.");
  248. if ( strtolower(DOMPDF_PDF_BACKEND) === "gd" )
  249. $outfile = str_replace(".pdf", ".png", $outfile);
  250. list($proto, $host, $path, $file) = explode_url($outfile);
  251. if ( $proto != "" ) // i.e. not file://
  252. $outfile = $file; // just save it locally, FIXME? could save it like wget: ./host/basepath/file
  253. $outfile = realpath(dirname($outfile)) . DIRECTORY_SEPARATOR . basename($outfile);
  254. if ( strpos($outfile, DOMPDF_CHROOT) !== 0 )
  255. throw new DOMPDF_Exception("Permission denied.");
  256. file_put_contents($outfile, $dompdf->output( array("compress" => 0) ));
  257. exit(0);
  258. }
  259. if ( !headers_sent() ) {
  260. $dompdf->stream($outfile);
  261. }