forked from yiiext/pdf
/
PdfFile.php
97 lines (90 loc) · 2.26 KB
/
PdfFile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* PdfFile represents an existing pdf file
*
* @author Rinat Silnov
* @version 1.0
*/
class PdfFile extends Pdf
{
/**
* @var string absolute path to the PDF file
*/
protected $file;
/**
* Constructor.
* @param string $file absolute path to the PDF file
* @throws CException if the PDF file does not exist
*/
function __construct($file)
{
if (!file_exists($file))
throw new CException("File $file not exists");
$this->file = $file;
}
/**
* Output content of PDF to the browser.
* @param string $name Name of the file.
* If name is not specified basename of current PDF file will be used.
* @return void
* @see file
*/
function output($name = null)
{
$name = $name ?: str_replace('"', '', basename($this->file));
return parent::output($name);
}
/**
* Return PdfForm object for this pdf file
* @return PdfForm the form instance
*/
function getForm()
{
return new PdfForm($this);
}
/**
* Returns the content of the pdf
* @return string
*/
function getContent()
{
return file_get_contents($this->file);
}
/**
* Add few files to the end of this PDF
* @param variable lengths argument list of PdfFile objects
* @throws CException if argument list contains not PdfFile values
* @return PdfString an instance of PdfString with merged PDF files
* on success, false on failure
*/
function merge()
{
foreach(func_get_args() as $pdf)
if (!($pdf instanceOf PdfFile))
throw new CException('All arguments for PdfFile::merge should be an instance of PdfFile');
$pdftk = Pdftk::getInstance();
$result = $pdftk->cat(array_merge(array($this),func_get_args()));
return $result ? new PdfString($result) : false;
}
/**
* Apply another PDF as background for each page of current PDF
* @param PdfFile $back an instance of PdfFile which will be used as a background
* @return PdfString an instance of PdfString with applied background
* on success, false on failure
*/
function applyBackground(PdfFile $back)
{
$pdftk = Pdftk::getInstance();
$result = $pdftk->background($this, $back);
return $result ? new PdfString($result) : false;
}
/**
* String magic method
* @return string the absolute path to the current pdf file
* @see file
*/
function __toString()
{
return $this->file;
}
}