FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Pdf.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred;
4 
5 class Pdf extends Component{
6 
7  const ORIENTATION_PORTRAIT = 'portrait';
8  const ORIENTATION_LANDSCAPE = 'landscape';
9 
10  const PAPER_A0 = 'a0';
11  const PAPER_A1 = 'a1';
12  const PAPER_A2 = 'a2';
13  const PAPER_A3 = 'a3';
14  const PAPER_A4 = 'a4';
15  const PAPER_A5 = 'a5';
16  const PAPER_LETTER = 'letter';
17  const PAPER_LEGAL = 'legal';
18 
19  public $defaultBackEnd = 'domPdf';
20  public $paper = self::PAPER_A4; //!< Default paper size.
21  public $orientation = self::ORIENTATION_PORTRAIT; //!< Default paper orientation.
22 
23  public $webKitHtmlToPdfPath = null; //!< wkHTMLtoPDF command-line, with placeholders voor [options] [source] en [target].
24 
25  protected $_backEnds = null;
26  protected $_domPdf = null;
27 
28  /**
29  * Create a PDF file using DomPdf.
30  * @see create()
31  */
32  public function createDomPdf($html,$filename = null,$paper = null,$orientation = null,$options = null){
33  $this->domPdf->load_html($html);
34  $this->domPdf->set_paper($paper ?: $this->paper,$orientation ?: $this->orientation);
35  $this->domPdf->render();
36  $data = $this->domPdf->output();
37  return $filename ? file_put_contents($filename,$data) : $data;
38  }
39  /**
40  * Create a PDF file using WebKit.
41  * @see create()
42  */
43  public function createWebKit($html,$filename = null,$paper = null,$orientation = null,$options = null){
44  \Rsi\File::unlink($target = $filename ?: \Rsi\File::tempFile('pdf'));
45  shell_exec(strtr($this->webKitHtmlToPdfPath,[
46  '[options]' => '--' . \Rsi\Record::implode(array_merge(['page-size' => $paper,'orientation' => $orientation],$options ?: []),' --',' '),
47  '[source]' => $source = \Rsi\File::tempFile('pdf',$html),
48  '[target]' => $target
49  ]));
50  if(!is_file($target)) return false;
51  if($filename) return filesize($filename);
52  $data = file_get_contents($target);
53  unlink($target);
54  return $data;
55  }
56  /**
57  * Create a PDF file using the default back-end.
58  * @param string $html HTML presentation of content.
59  * @param string $filename Target file (empty = return data).
60  * @param string $paper Paper size (see PAPER_* constants; empty = default).
61  * @param string $orientation Paper orientation (see ORIENTATION_* constants; empty = default).
62  * @param array $options Extra options (back-end specific).
63  * @return mixed Data if no filename given, otherwise filesize (false on error).
64  */
65  public function create($html,$filename = null,$paper = null,$orientation = null,$options = null){
66  return call_user_func([$this,'create' . ucfirst($this->defaultBackEnd)],$html,$filename,$paper,$orientation,$options);
67  }
68 
69  protected function getBackEnds(){
70  if($this->_backEnds === null){
71  $this->_backEnds = [];
72  foreach(get_class_methods($this) as $method) if((strlen($method) > 6) && (substr($method,0,6) == 'create'))
73  $this->_backEnds[] = lcfirst(substr($method,6));
74  }
75  return $this->_backEnds;
76  }
77 
78  protected function getDomPdf(){
79  if(!$this->_domPdf) $this->_domPdf = new \Dompdf\Dompdf();
80  return $this->_domPdf;
81  }
82 
83 }
const PAPER_A5
Definition: Pdf.php:15
create($html, $filename=null, $paper=null, $orientation=null, $options=null)
Create a PDF file using the default back-end.
Definition: Pdf.php:65
createWebKit($html, $filename=null, $paper=null, $orientation=null, $options=null)
Create a PDF file using WebKit.
Definition: Pdf.php:43
const PAPER_A4
Definition: Pdf.php:14
const PAPER_A1
Definition: Pdf.php:11
createDomPdf($html, $filename=null, $paper=null, $orientation=null, $options=null)
Create a PDF file using DomPdf.
Definition: Pdf.php:32
const PAPER_LEGAL
Definition: Pdf.php:17
$paper
Default paper size.
Definition: Pdf.php:20
$_backEnds
Definition: Pdf.php:25
const PAPER_A0
Definition: Pdf.php:10
const ORIENTATION_LANDSCAPE
Definition: Pdf.php:8
getBackEnds()
Definition: Pdf.php:69
const PAPER_LETTER
Definition: Pdf.php:16
getDomPdf()
Definition: Pdf.php:78
$webKitHtmlToPdfPath
wkHTMLtoPDF command-line, with placeholders voor [options] [source] en [target].
Definition: Pdf.php:23
$orientation
Default paper orientation.
Definition: Pdf.php:21
Basic component class.
Definition: Component.php:8
const PAPER_A3
Definition: Pdf.php:13
const ORIENTATION_PORTRAIT
Definition: Pdf.php:7
$defaultBackEnd
Definition: Pdf.php:19
const PAPER_A2
Definition: Pdf.php:12