-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAttachmentsTrait.php
More file actions
123 lines (108 loc) · 3.05 KB
/
AttachmentsTrait.php
File metadata and controls
123 lines (108 loc) · 3.05 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
namespace FPDF\Scripts\Attachments;
//http://www.fpdf.org/en/script/script95.php
trait AttachmentsTrait {
protected $files = array();
protected $n_files;
protected $open_attachment_pane = false;
/**
* Add a attachment
*
* @param string $file path to the file to attach.
* @param string $name the name under which the file will be attached, the default value is taken from file.
* @param string $desc an optional description.
* @return void
*/
public function Attach($file, $name='', $desc='')
{
if($name=='')
{
$p = strrpos($file,'/');
if($p===false)
$p = strrpos($file,'\\');
if($p!==false)
$name = substr($file,$p+1);
else
$name = $file;
}
$this->files[] = array('file'=>$file, 'name'=>$name, 'desc'=>$desc);
}
/**
* Force the PDF viewer to open the attachment pane when the document is loaded
*
* @return void
*/
public function OpenAttachmentPane()
{
$this->open_attachment_pane = true;
}
protected function _putfiles()
{
if(empty($this->files)) return;
foreach($this->files as $i=>&$info)
{
$file = $info['file'];
$name = $info['name'];
$desc = $info['desc'];
$fc = file_get_contents($file);
if($fc===false)
$this->Error('Cannot open file: '.$file);
$size = strlen($fc);
$date = @date('YmdHisO', filemtime($file));
$md = 'D:'.substr($date,0,-2)."'".substr($date,-2)."'";;
$this->_newobj();
$info['n'] = $this->n;
$this->_put('<<');
$this->_put('/Type /Filespec');
$this->_put('/F ('.$this->_escape($name).')');
$this->_put('/UF '.$this->_textstring($name));
$this->_put('/EF <</F '.($this->n+1).' 0 R>>');
if($desc)
$this->_put('/Desc '.$this->_textstring($desc));
$this->_put('/AFRelationship /Unspecified');
$this->_put('>>');
$this->_put('endobj');
$this->_newobj();
$this->_put('<<');
$this->_put('/Type /EmbeddedFile');
$this->_put('/Subtype /application#2Foctet-stream');
$this->_put('/Length '.$size);
$this->_put('/Params <</Size '.$size.' /ModDate '.$this->_textstring($md).'>>');
$this->_put('>>');
$this->_putstream($fc);
$this->_put('endobj');
}
unset($info);
$this->_newobj();
$this->n_files = $this->n;
$a = array();
foreach($this->files as $i=>$info)
$a[] = $this->_textstring(sprintf('%03d',$i)).' '.$info['n'].' 0 R';
$this->_put('<<');
$this->_put('/Names ['.implode(' ',$a).']');
$this->_put('>>');
$this->_put('endobj');
}
protected function _putresources()
{
parent::_putresources();
$this->_putfiles();
}
protected function _putfilescatalog()
{
if(empty($this->files)) return;
$this->_put('/Names <</EmbeddedFiles '.$this->n_files.' 0 R>>');
$a = array();
foreach($this->files as $info)
$a[] = $info['n'].' 0 R';
$this->_put('/AF ['.implode(' ',$a).']');
if($this->open_attachment_pane)
$this->_put('/PageMode /UseAttachments');
}
protected function _putcatalog()
{
parent::_putcatalog();
$this->_putfilescatalog();
}
}