No Description

dompdf.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. $dompdf = new DOMPDF();
  108. switch ( $sapi ) {
  109. case "cli":
  110. $opts = getoptions();
  111. if ( isset($opts["h"]) || (!isset($opts["filename"]) && !isset($opts["l"])) ) {
  112. dompdf_usage();
  113. exit;
  114. }
  115. if ( isset($opts["l"]) ) {
  116. echo "\nUnderstood paper sizes:\n";
  117. foreach (array_keys(CPDF_Adapter::$PAPER_SIZES) as $size)
  118. echo " " . mb_strtoupper($size) . "\n";
  119. exit;
  120. }
  121. $file = $opts["filename"];
  122. if ( isset($opts["p"]) )
  123. $paper = $opts["p"];
  124. else
  125. $paper = DOMPDF_DEFAULT_PAPER_SIZE;
  126. if ( isset($opts["o"]) )
  127. $orientation = $opts["o"];
  128. else
  129. $orientation = "portrait";
  130. if ( isset($opts["b"]) )
  131. $base_path = $opts["b"];
  132. if ( isset($opts["f"]) )
  133. $outfile = $opts["f"];
  134. else {
  135. if ( $file === "-" )
  136. $outfile = "dompdf_out.pdf";
  137. else
  138. $outfile = str_ireplace(array(".html", ".htm"), "", $file) . ".pdf";
  139. }
  140. if ( isset($opts["v"]) )
  141. $_dompdf_show_warnings = true;
  142. if ( isset($opts["d"]) ) {
  143. $_dompdf_show_warnings = true;
  144. $_dompdf_debug = true;
  145. }
  146. if ( isset($opts['t']) ) {
  147. $arr = split(',',$opts['t']);
  148. $types = array();
  149. foreach ($arr as $type)
  150. $types[ trim($type) ] = 1;
  151. $_DOMPDF_DEBUG_TYPES = $types;
  152. }
  153. $save_file = true;
  154. break;
  155. default:
  156. $dompdf->set_option('enable_php', false);
  157. if ( isset($_GET["input_file"]) )
  158. $file = rawurldecode($_GET["input_file"]);
  159. else
  160. throw new DOMPDF_Exception("An input file is required (i.e. input_file _GET variable).");
  161. if ( isset($_GET["paper"]) )
  162. $paper = rawurldecode($_GET["paper"]);
  163. else
  164. $paper = DOMPDF_DEFAULT_PAPER_SIZE;
  165. if ( isset($_GET["orientation"]) )
  166. $orientation = rawurldecode($_GET["orientation"]);
  167. else
  168. $orientation = "portrait";
  169. if ( isset($_GET["base_path"]) ) {
  170. $base_path = rawurldecode($_GET["base_path"]);
  171. $file = $base_path . $file; # Set the input file
  172. }
  173. if ( isset($_GET["options"]) ) {
  174. $options = $_GET["options"];
  175. }
  176. $file_parts = explode_url($file);
  177. $outfile = "dompdf_out.pdf"; # Don't allow them to set the output file
  178. $save_file = false; # Don't save the file
  179. break;
  180. }
  181. if ( $file === "-" ) {
  182. $str = "";
  183. while ( !feof(STDIN) )
  184. $str .= fread(STDIN, 4096);
  185. $dompdf->load_html($str);
  186. } else
  187. $dompdf->load_html_file($file);
  188. if ( isset($base_path) ) {
  189. $dompdf->set_base_path($base_path);
  190. }
  191. $dompdf->set_paper($paper, $orientation);
  192. $dompdf->render();
  193. if ( $_dompdf_show_warnings ) {
  194. global $_dompdf_warnings;
  195. foreach ($_dompdf_warnings as $msg)
  196. echo $msg . "\n";
  197. echo $dompdf->get_canvas()->get_cpdf()->messages;
  198. flush();
  199. }
  200. if ( $save_file ) {
  201. // if ( !is_writable($outfile) )
  202. // throw new DOMPDF_Exception("'$outfile' is not writable.");
  203. if ( strtolower(DOMPDF_PDF_BACKEND) === "gd" )
  204. $outfile = str_replace(".pdf", ".png", $outfile);
  205. list($proto, $host, $path, $file) = explode_url($outfile);
  206. if ( $proto != "" ) // i.e. not file://
  207. $outfile = $file; // just save it locally, FIXME? could save it like wget: ./host/basepath/file
  208. $outfile = realpath(dirname($outfile)) . DIRECTORY_SEPARATOR . basename($outfile);
  209. if ( strpos($outfile, DOMPDF_CHROOT) !== 0 )
  210. throw new DOMPDF_Exception("Permission denied.");
  211. file_put_contents($outfile, $dompdf->output( array("compress" => 0) ));
  212. exit(0);
  213. }
  214. if ( !headers_sent() ) {
  215. $dompdf->stream($outfile, $options);
  216. }