dompdf.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * Command line utility to use dompdf.
  4. * Can also be used with HTTP GET parameters
  5. *
  6. * @package dompdf
  7. * @link http://dompdf.github.com/
  8. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  9. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  10. */
  11. /**
  12. * Display command line usage
  13. */
  14. function dompdf_usage() {
  15. $default_paper_size = DOMPDF_DEFAULT_PAPER_SIZE;
  16. echo <<<EOD
  17. Usage: {$_SERVER["argv"][0]} [options] html_file
  18. html_file can be a filename, a url if fopen_wrappers are enabled, or the '-' character to read from standard input.
  19. Options:
  20. -h Show this message
  21. -l List available paper sizes
  22. -p size Paper size; something like 'letter', 'A4', 'legal', etc.
  23. The default is '$default_paper_size'
  24. -o orientation Either 'portrait' or 'landscape'. Default is 'portrait'
  25. -b path Set the 'document root' of the html_file.
  26. Relative urls (for stylesheets) are resolved using this directory.
  27. Default is the directory of html_file.
  28. -f file The output filename. Default is the input [html_file].pdf
  29. -v Verbose: display html parsing warnings and file not found errors.
  30. -d Very verbose: display oodles of debugging output: every frame
  31. in the tree printed to stdout.
  32. -t Comma separated list of debugging types (page-break,reflow,split)
  33. EOD;
  34. exit;
  35. }
  36. /**
  37. * Parses command line options
  38. *
  39. * @return array The command line options
  40. */
  41. function getoptions() {
  42. $opts = array();
  43. if ( $_SERVER["argc"] == 1 )
  44. return $opts;
  45. $i = 1;
  46. while ($i < $_SERVER["argc"]) {
  47. switch ($_SERVER["argv"][$i]) {
  48. case "--help":
  49. case "-h":
  50. $opts["h"] = true;
  51. $i++;
  52. break;
  53. case "-l":
  54. $opts["l"] = true;
  55. $i++;
  56. break;
  57. case "-p":
  58. if ( !isset($_SERVER["argv"][$i+1]) )
  59. die("-p switch requires a size parameter\n");
  60. $opts["p"] = $_SERVER["argv"][$i+1];
  61. $i += 2;
  62. break;
  63. case "-o":
  64. if ( !isset($_SERVER["argv"][$i+1]) )
  65. die("-o switch requires an orientation parameter\n");
  66. $opts["o"] = $_SERVER["argv"][$i+1];
  67. $i += 2;
  68. break;
  69. case "-b":
  70. if ( !isset($_SERVER["argv"][$i+1]) )
  71. die("-b switch requires a path parameter\n");
  72. $opts["b"] = $_SERVER["argv"][$i+1];
  73. $i += 2;
  74. break;
  75. case "-f":
  76. if ( !isset($_SERVER["argv"][$i+1]) )
  77. die("-f switch requires a filename parameter\n");
  78. $opts["f"] = $_SERVER["argv"][$i+1];
  79. $i += 2;
  80. break;
  81. case "-v":
  82. $opts["v"] = true;
  83. $i++;
  84. break;
  85. case "-d":
  86. $opts["d"] = true;
  87. $i++;
  88. break;
  89. case "-t":
  90. if ( !isset($_SERVER['argv'][$i + 1]) )
  91. die("-t switch requires a comma separated list of types\n");
  92. $opts["t"] = $_SERVER['argv'][$i+1];
  93. $i += 2;
  94. break;
  95. default:
  96. $opts["filename"] = $_SERVER["argv"][$i];
  97. $i++;
  98. break;
  99. }
  100. }
  101. return $opts;
  102. }
  103. require_once("dompdf_config.inc.php");
  104. global $_dompdf_show_warnings, $_dompdf_debug, $_DOMPDF_DEBUG_TYPES;
  105. $sapi = php_sapi_name();
  106. $options = array();
  107. switch ( $sapi ) {
  108. case "cli":
  109. $opts = getoptions();
  110. if ( isset($opts["h"]) || (!isset($opts["filename"]) && !isset($opts["l"])) ) {
  111. dompdf_usage();
  112. exit;
  113. }
  114. if ( isset($opts["l"]) ) {
  115. echo "\nUnderstood paper sizes:\n";
  116. foreach (array_keys(CPDF_Adapter::$PAPER_SIZES) as $size)
  117. echo " " . mb_strtoupper($size) . "\n";
  118. exit;
  119. }
  120. $file = $opts["filename"];
  121. if ( isset($opts["p"]) )
  122. $paper = $opts["p"];
  123. else
  124. $paper = DOMPDF_DEFAULT_PAPER_SIZE;
  125. if ( isset($opts["o"]) )
  126. $orientation = $opts["o"];
  127. else
  128. $orientation = "portrait";
  129. if ( isset($opts["b"]) )
  130. $base_path = $opts["b"];
  131. if ( isset($opts["f"]) )
  132. $outfile = $opts["f"];
  133. else {
  134. if ( $file === "-" )
  135. $outfile = "dompdf_out.pdf";
  136. else
  137. $outfile = str_ireplace(array(".html", ".htm", ".php"), "", $file) . ".pdf";
  138. }
  139. if ( isset($opts["v"]) )
  140. $_dompdf_show_warnings = true;
  141. if ( isset($opts["d"]) ) {
  142. $_dompdf_show_warnings = true;
  143. $_dompdf_debug = true;
  144. }
  145. if ( isset($opts['t']) ) {
  146. $arr = split(',',$opts['t']);
  147. $types = array();
  148. foreach ($arr as $type)
  149. $types[ trim($type) ] = 1;
  150. $_DOMPDF_DEBUG_TYPES = $types;
  151. }
  152. $save_file = true;
  153. break;
  154. default:
  155. if ( isset($_GET["input_file"]) )
  156. $file = rawurldecode($_GET["input_file"]);
  157. else
  158. throw new DOMPDF_Exception("An input file is required (i.e. input_file _GET variable).");
  159. if ( isset($_GET["paper"]) )
  160. $paper = rawurldecode($_GET["paper"]);
  161. else
  162. $paper = DOMPDF_DEFAULT_PAPER_SIZE;
  163. if ( isset($_GET["orientation"]) )
  164. $orientation = rawurldecode($_GET["orientation"]);
  165. else
  166. $orientation = "portrait";
  167. if ( isset($_GET["base_path"]) ) {
  168. $base_path = rawurldecode($_GET["base_path"]);
  169. $file = $base_path . $file; # Set the input file
  170. }
  171. if ( isset($_GET["options"]) ) {
  172. $options = $_GET["options"];
  173. }
  174. $file_parts = explode_url($file);
  175. /* Check to see if the input file is local and, if so, that the base path falls within that specified by DOMDPF_CHROOT */
  176. if(($file_parts['protocol'] == '' || $file_parts['protocol'] === 'file://')) {
  177. $file = realpath($file);
  178. if ( strpos($file, DOMPDF_CHROOT) !== 0 ) {
  179. throw new DOMPDF_Exception("Permission denied on $file. The file could not be found under the directory specified by DOMPDF_CHROOT.");
  180. }
  181. }
  182. $outfile = "dompdf_out.pdf"; # Don't allow them to set the output file
  183. $save_file = false; # Don't save the file
  184. break;
  185. }
  186. $dompdf = new DOMPDF();
  187. if ( $file === "-" ) {
  188. $str = "";
  189. while ( !feof(STDIN) )
  190. $str .= fread(STDIN, 4096);
  191. $dompdf->load_html($str);
  192. } else
  193. $dompdf->load_html_file($file);
  194. if ( isset($base_path) ) {
  195. $dompdf->set_base_path($base_path);
  196. }
  197. $dompdf->set_paper($paper, $orientation);
  198. $dompdf->render();
  199. if ( $_dompdf_show_warnings ) {
  200. global $_dompdf_warnings;
  201. foreach ($_dompdf_warnings as $msg)
  202. echo $msg . "\n";
  203. echo $dompdf->get_canvas()->get_cpdf()->messages;
  204. flush();
  205. }
  206. if ( $save_file ) {
  207. // if ( !is_writable($outfile) )
  208. // throw new DOMPDF_Exception("'$outfile' is not writable.");
  209. if ( strtolower(DOMPDF_PDF_BACKEND) === "gd" )
  210. $outfile = str_replace(".pdf", ".png", $outfile);
  211. list($proto, $host, $path, $file) = explode_url($outfile);
  212. if ( $proto != "" ) // i.e. not file://
  213. $outfile = $file; // just save it locally, FIXME? could save it like wget: ./host/basepath/file
  214. $outfile = realpath(dirname($outfile)) . DIRECTORY_SEPARATOR . basename($outfile);
  215. if ( strpos($outfile, DOMPDF_CHROOT) !== 0 )
  216. throw new DOMPDF_Exception("Permission denied.");
  217. file_put_contents($outfile, $dompdf->output( array("compress" => 0) ));
  218. exit(0);
  219. }
  220. if ( !headers_sent() ) {
  221. $dompdf->stream($outfile, $options);
  222. }