PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/dompdf/dompdf/dompdf.php

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