No Description

PDF.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace Barryvdh\DomPDF;
  3. use Exception;
  4. use Illuminate\Config\Repository as ConfigRepository;
  5. use Illuminate\Filesystem\Filesystem;
  6. use Illuminate\Http\Response;
  7. /**
  8. * A Laravel wrapper for DOMPDF
  9. *
  10. * @package laravel-dompdf
  11. * @author Barry vd. Heuvel
  12. */
  13. class PDF
  14. {
  15. /** @var \DOMPDF */
  16. protected $dompdf;
  17. /** @var \Illuminate\Config\Repository */
  18. protected $config;
  19. /** @var \Illuminate\Filesystem\Filesystem */
  20. protected $files;
  21. /** @var \Illuminate\View\Factory */
  22. protected $view;
  23. protected $rendered = false;
  24. protected $orientation;
  25. protected $paper;
  26. protected $showWarnings;
  27. protected $public_path;
  28. /**
  29. *
  30. * @param \Illuminate\Config\Repository $config
  31. * @param \Illuminate\Filesystem\Filesystem $files
  32. * @param \Illuminate\View\Factory $view
  33. * @param string $publicPath
  34. */
  35. public function __construct(ConfigRepository $config, Filesystem $files, /* Illuminate\View\Factory */ $view, $publicPath)
  36. {
  37. $this->config = $config;
  38. $this->files = $files;
  39. $this->view = $view;
  40. $this->public_path = $publicPath;
  41. $this->showWarnings = $this->config->get('laravel-dompdf::show_warnings', false);
  42. //To prevent old configs from not working..
  43. if ($this->config->has('laravel-dompdf::paper')) {
  44. $this->paper = $this->config->get('laravel-dompdf::paper');
  45. } else {
  46. $this->paper = 'portrait';
  47. }
  48. $this->orientation = $this->config->get('laravel-dompdf::orientation') ?: 'portrait';
  49. $this->dompdf = new \DOMPDF();
  50. $this->dompdf->set_base_path(realpath($publicPath));
  51. }
  52. /**
  53. * Get the DomPDF instance
  54. *
  55. * @return \DOMPDF
  56. */
  57. public function getDomPDF()
  58. {
  59. return $this->dompdf;
  60. }
  61. /**
  62. * Set the paper size (default A4)
  63. *
  64. * @param string $paper
  65. * @param string $orientation
  66. * @return $this
  67. */
  68. public function setPaper($paper, $orientation = null)
  69. {
  70. $this->paper = $paper;
  71. if ($orientation) {
  72. $this->orientation = $orientation;
  73. }
  74. return $this;
  75. }
  76. /**
  77. * Set the orientation (default portrait)
  78. *
  79. * @param string $orientation
  80. * @return static
  81. */
  82. public function setOrientation($orientation)
  83. {
  84. $this->orientation = $orientation;
  85. return $this;
  86. }
  87. /**
  88. * Show or hide warnings
  89. *
  90. * @param bool $warnings
  91. * @return $this
  92. */
  93. public function setWarnings($warnings)
  94. {
  95. $this->showWarnings = $warnings;
  96. return $this;
  97. }
  98. /**
  99. * Load a HTML string
  100. *
  101. * @param string $string
  102. * @param string $encoding Not used yet
  103. * @return static
  104. */
  105. public function loadHTML($string, $encoding = null)
  106. {
  107. $string = $this->convertEntities($string);
  108. $this->dompdf->load_html($string, $encoding);
  109. $this->rendered = false;
  110. return $this;
  111. }
  112. /**
  113. * Load a HTML file
  114. *
  115. * @param string $file
  116. * @return static
  117. */
  118. public function loadFile($file)
  119. {
  120. $this->dompdf->load_html_file($file);
  121. $this->rendered = false;
  122. return $this;
  123. }
  124. /**
  125. * Load a View and convert to HTML
  126. *
  127. * @param string $view
  128. * @param array $data
  129. * @param array $mergeData
  130. * @param string $encoding Not used yet
  131. * @return static
  132. */
  133. public function loadView($view, $data = array(), $mergeData = array(), $encoding = null)
  134. {
  135. $html = $this->view->make($view, $data, $mergeData)->render();
  136. return $this->loadHTML($html, $encoding);
  137. }
  138. /**
  139. * Output the PDF as a string.
  140. *
  141. * @return string The rendered PDF as string
  142. */
  143. public function output()
  144. {
  145. if (!$this->rendered) {
  146. $this->render();
  147. }
  148. return $this->dompdf->output();
  149. }
  150. /**
  151. * Save the PDF to a file
  152. *
  153. * @param $filename
  154. * @return static
  155. */
  156. public function save($filename)
  157. {
  158. $this->files->put($filename, $this->output());
  159. return $this;
  160. }
  161. /**
  162. * Make the PDF downloadable by the user
  163. *
  164. * @param string $filename
  165. * @return \Illuminate\Http\Response
  166. */
  167. public function download($filename = 'document.pdf')
  168. {
  169. $output = $this->output();
  170. return new Response($output, 200, array(
  171. 'Content-Type' => 'application/pdf',
  172. 'Content-Disposition' => 'attachment; filename="' . $filename . '"'
  173. ));
  174. }
  175. /**
  176. * Return a response with the PDF to show in the browser
  177. *
  178. * @param string $filename
  179. * @return \Illuminate\Http\Response
  180. */
  181. public function stream($filename = 'document.pdf')
  182. {
  183. $output = $this->output();
  184. return new Response($output, 200, array(
  185. 'Content-Type' => 'application/pdf',
  186. 'Content-Disposition' => 'inline; filename="' . $filename . '"',
  187. ));
  188. }
  189. /**
  190. * Render the PDF
  191. */
  192. protected function render()
  193. {
  194. if (!$this->dompdf) {
  195. throw new Exception('DOMPDF not created yet');
  196. }
  197. $this->dompdf->set_paper($this->paper, $this->orientation);
  198. $this->dompdf->render();
  199. if ($this->showWarnings) {
  200. global $_dompdf_warnings;
  201. if (count($_dompdf_warnings)) {
  202. $warnings = '';
  203. foreach ($_dompdf_warnings as $msg) {
  204. $warnings .= $msg . "\n";
  205. }
  206. // $warnings .= $this->dompdf->get_canvas()->get_cpdf()->messages;
  207. if (!empty($warnings)) {
  208. throw new Exception($warnings);
  209. }
  210. }
  211. }
  212. $this->rendered = true;
  213. }
  214. protected function convertEntities($subject)
  215. {
  216. $entities = array(
  217. '€' => '&#0128;',
  218. );
  219. foreach ($entities as $search => $replace) {
  220. $subject = str_replace($search, $replace, $subject);
  221. }
  222. return $subject;
  223. }
  224. }