1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZLoremIpsumBundle\Faker\Provider; |
4
|
|
|
|
5
|
|
|
use TCPDF; |
6
|
|
|
|
7
|
|
|
class PdfFile extends Base |
8
|
|
|
{ |
9
|
|
|
public function pdfFile($dir = null, $pages=5, $title = '', $author = '', $subject = '', $keywords = '') |
10
|
|
|
{ |
11
|
|
|
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); |
12
|
|
|
|
13
|
|
|
// set document information |
14
|
|
|
$pdf->SetCreator(PDF_CREATOR); |
15
|
|
|
$pdf->SetAuthor($this->getAuthor($author)); |
16
|
|
|
$pdf->SetTitle($this->getTitle($title)); |
17
|
|
|
$pdf->SetSubject($this->getSubject($subject)); |
18
|
|
|
$pdf->SetKeywords($this->getKeywords($keywords)); |
19
|
|
|
|
20
|
|
|
// remove default header/footer |
21
|
|
|
$pdf->setPrintHeader(false); |
22
|
|
|
$pdf->setPrintFooter(false); |
23
|
|
|
|
24
|
|
|
// set default monospaced font |
25
|
|
|
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); |
26
|
|
|
|
27
|
|
|
// set margins |
28
|
|
|
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); |
29
|
|
|
|
30
|
|
|
// set auto page breaks |
31
|
|
|
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); |
32
|
|
|
|
33
|
|
|
// set image scale factor |
34
|
|
|
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); |
35
|
|
|
|
36
|
|
|
// set some language-dependent strings (optional) |
37
|
|
|
/*if (@file_exists(dirname(__FILE__).'/lang/eng.php')) { |
38
|
|
|
require_once(dirname(__FILE__).'/lang/eng.php'); |
39
|
|
|
$pdf->setLanguageArray($l); |
40
|
|
|
}*/ |
41
|
|
|
|
42
|
|
|
// set default font subsetting mode |
43
|
|
|
$pdf->setFontSubsetting(true); |
44
|
|
|
|
45
|
|
|
// Set font |
46
|
|
|
// dejavusans is a UTF-8 Unicode font, if you only need to |
47
|
|
|
// print standard ASCII chars, you can use core fonts like |
48
|
|
|
// helvetica or times to reduce file size. |
49
|
|
|
$pdf->SetFont('dejavusans', '', 14, '', true); |
50
|
|
|
|
51
|
|
|
$pages = mt_rand(1, $pages); |
52
|
|
|
|
53
|
|
|
for ($i = 0; $i < $pages; $i++) { |
54
|
|
|
$pdf->AddPage(); |
55
|
|
|
$html = $this->getPageHtml($i); |
56
|
|
|
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// Close and save PDF document |
60
|
|
|
$fileName = $this->getFilename($dir); |
61
|
|
|
|
62
|
|
|
$pdf->Output($fileName, 'F'); |
63
|
|
|
|
64
|
|
|
return $fileName; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function getAuthor($author = '') |
68
|
|
|
{ |
69
|
|
|
if ($author !== '') { |
70
|
|
|
return $author; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->generator->name; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function getTitle($title = '') |
77
|
|
|
{ |
78
|
|
|
if ($title !== '') { |
79
|
|
|
return $title; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->generator->sentence; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected function getSubject($subject = '') |
86
|
|
|
{ |
87
|
|
|
if ($subject !== '') { |
88
|
|
|
return $subject; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->generator->paragraph; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function getKeywords($keywords = '') |
95
|
|
|
{ |
96
|
|
|
if ($keywords !== '') { |
97
|
|
|
return $keywords; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return implode(', ', $this->generator->words); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function getPageHtml($pageNum) |
104
|
|
|
{ |
105
|
|
|
return $this->generator->randomHtml(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function getFileName($dir = null) |
109
|
|
|
{ |
110
|
|
|
$dir = is_null($dir) ? sys_get_temp_dir() : $dir; // GNU/Linux / OS X / Windows compatible |
111
|
|
|
// Validate directory path |
112
|
|
|
if (!is_dir($dir) || !is_writable($dir)) { |
113
|
|
|
throw new \InvalidArgumentException(sprintf('Cannot write to directory "%s"', $dir)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// Generate a random filename. Use the server address so that a file |
117
|
|
|
// generated at the same time on a different server won't have a collision. |
118
|
|
|
$name = md5(uniqid(empty($_SERVER['SERVER_ADDR']) ? '' : $_SERVER['SERVER_ADDR'], true)); |
119
|
|
|
$filename = $name .'.pdf'; |
120
|
|
|
$filepath = $dir . DIRECTORY_SEPARATOR . $filename; |
121
|
|
|
|
122
|
|
|
return $filepath; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|