123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?php
-
- namespace Barryvdh\DomPDF;
-
- use Exception;
- use Illuminate\Config\Repository as ConfigRepository;
- use Illuminate\Filesystem\Filesystem;
- use Illuminate\Http\Response;
-
-
- class PDF
- {
-
-
- protected $dompdf;
-
-
- protected $config;
-
-
- protected $files;
-
-
- protected $view;
-
- protected $rendered = false;
- protected $orientation;
- protected $paper;
- protected $showWarnings;
- protected $public_path;
-
-
-
- public function __construct(ConfigRepository $config, Filesystem $files, $view, $publicPath)
- {
- $this->config = $config;
- $this->files = $files;
- $this->view = $view;
- $this->public_path = $publicPath;
-
- $this->showWarnings = $this->config->get('laravel-dompdf::show_warnings', false);
-
-
- if ($this->config->has('laravel-dompdf::paper')) {
- $this->paper = $this->config->get('laravel-dompdf::paper');
- } else {
- $this->paper = 'portrait';
- }
-
- $this->orientation = $this->config->get('laravel-dompdf::orientation') ?: 'portrait';
-
- $this->dompdf = new \DOMPDF();
- $this->dompdf->set_base_path(realpath($publicPath));
- }
-
-
-
- public function getDomPDF()
- {
- return $this->dompdf;
- }
-
-
-
- public function setPaper($paper, $orientation = null)
- {
- $this->paper = $paper;
- if ($orientation) {
- $this->orientation = $orientation;
- }
- return $this;
- }
-
-
-
- public function setOrientation($orientation)
- {
- $this->orientation = $orientation;
- return $this;
- }
-
-
-
- public function setWarnings($warnings)
- {
- $this->showWarnings = $warnings;
- return $this;
- }
-
-
-
- public function loadHTML($string, $encoding = null)
- {
- $string = $this->convertEntities($string);
- $this->dompdf->load_html($string, $encoding);
- $this->rendered = false;
- return $this;
- }
-
-
-
- public function loadFile($file)
- {
- $this->dompdf->load_html_file($file);
- $this->rendered = false;
- return $this;
- }
-
-
-
- public function loadView($view, $data = array(), $mergeData = array(), $encoding = null)
- {
- $html = $this->view->make($view, $data, $mergeData)->render();
- return $this->loadHTML($html, $encoding);
- }
-
-
-
- public function output()
- {
- if (!$this->rendered) {
- $this->render();
- }
- return $this->dompdf->output();
- }
-
-
-
- public function save($filename)
- {
- $this->files->put($filename, $this->output());
- return $this;
- }
-
-
-
- public function download($filename = 'document.pdf')
- {
- $output = $this->output();
- return new Response($output, 200, array(
- 'Content-Type' => 'application/pdf',
- 'Content-Disposition' => 'attachment; filename="' . $filename . '"'
- ));
- }
-
-
-
- public function stream($filename = 'document.pdf')
- {
- $output = $this->output();
- return new Response($output, 200, array(
- 'Content-Type' => 'application/pdf',
- 'Content-Disposition' => 'inline; filename="' . $filename . '"',
- ));
- }
-
-
-
- protected function render()
- {
- if (!$this->dompdf) {
- throw new Exception('DOMPDF not created yet');
- }
-
- $this->dompdf->set_paper($this->paper, $this->orientation);
-
- $this->dompdf->render();
-
- if ($this->showWarnings) {
- global $_dompdf_warnings;
- if (count($_dompdf_warnings)) {
- $warnings = '';
- foreach ($_dompdf_warnings as $msg) {
- $warnings .= $msg . "\n";
- }
-
- if (!empty($warnings)) {
- throw new Exception($warnings);
- }
- }
- }
- $this->rendered = true;
- }
-
-
- protected function convertEntities($subject)
- {
- $entities = array(
- '€' => '€',
- );
-
- foreach ($entities as $search => $replace) {
- $subject = str_replace($search, $replace, $subject);
- }
- return $subject;
- }
- }
|