|
| 1 | +<?php |
| 2 | + declare(strict_types=1); |
| 3 | + |
| 4 | + namespace FPDF\Scripts\Attachments; |
| 5 | + //http://www.fpdf.org/en/script/script95.php |
| 6 | + |
| 7 | + trait AttachmentsTrait { |
| 8 | + |
| 9 | + protected $files = array(); |
| 10 | + protected $n_files; |
| 11 | + protected $open_attachment_pane = false; |
| 12 | + |
| 13 | + /** |
| 14 | + * Add a attachment |
| 15 | + * |
| 16 | + * @param string $file path to the file to attach. |
| 17 | + * @param string $name the name under which the file will be attached, the default value is taken from file. |
| 18 | + * @param string $desc an optional description. |
| 19 | + * @return void |
| 20 | + */ |
| 21 | + public function Attach($file, $name='', $desc='') |
| 22 | + { |
| 23 | + if($name=='') |
| 24 | + { |
| 25 | + $p = strrpos($file,'/'); |
| 26 | + if($p===false) |
| 27 | + $p = strrpos($file,'\\'); |
| 28 | + if($p!==false) |
| 29 | + $name = substr($file,$p+1); |
| 30 | + else |
| 31 | + $name = $file; |
| 32 | + } |
| 33 | + $this->files[] = array('file'=>$file, 'name'=>$name, 'desc'=>$desc); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Force the PDF viewer to open the attachment pane when the document is loaded |
| 38 | + * |
| 39 | + * @return void |
| 40 | + */ |
| 41 | + public function OpenAttachmentPane() |
| 42 | + { |
| 43 | + $this->open_attachment_pane = true; |
| 44 | + } |
| 45 | + |
| 46 | + protected function _putfiles() |
| 47 | + { |
| 48 | + if(empty($this->files)) return; |
| 49 | + |
| 50 | + foreach($this->files as $i=>&$info) |
| 51 | + { |
| 52 | + $file = $info['file']; |
| 53 | + $name = $info['name']; |
| 54 | + $desc = $info['desc']; |
| 55 | + |
| 56 | + $fc = file_get_contents($file); |
| 57 | + if($fc===false) |
| 58 | + $this->Error('Cannot open file: '.$file); |
| 59 | + $size = strlen($fc); |
| 60 | + $date = @date('YmdHisO', filemtime($file)); |
| 61 | + $md = 'D:'.substr($date,0,-2)."'".substr($date,-2)."'";; |
| 62 | + |
| 63 | + $this->_newobj(); |
| 64 | + $info['n'] = $this->n; |
| 65 | + $this->_put('<<'); |
| 66 | + $this->_put('/Type /Filespec'); |
| 67 | + $this->_put('/F ('.$this->_escape($name).')'); |
| 68 | + $this->_put('/UF '.$this->_textstring($name)); |
| 69 | + $this->_put('/EF <</F '.($this->n+1).' 0 R>>'); |
| 70 | + if($desc) |
| 71 | + $this->_put('/Desc '.$this->_textstring($desc)); |
| 72 | + $this->_put('/AFRelationship /Unspecified'); |
| 73 | + $this->_put('>>'); |
| 74 | + $this->_put('endobj'); |
| 75 | + |
| 76 | + $this->_newobj(); |
| 77 | + $this->_put('<<'); |
| 78 | + $this->_put('/Type /EmbeddedFile'); |
| 79 | + $this->_put('/Subtype /application#2Foctet-stream'); |
| 80 | + $this->_put('/Length '.$size); |
| 81 | + $this->_put('/Params <</Size '.$size.' /ModDate '.$this->_textstring($md).'>>'); |
| 82 | + $this->_put('>>'); |
| 83 | + $this->_putstream($fc); |
| 84 | + $this->_put('endobj'); |
| 85 | + } |
| 86 | + unset($info); |
| 87 | + |
| 88 | + $this->_newobj(); |
| 89 | + $this->n_files = $this->n; |
| 90 | + $a = array(); |
| 91 | + foreach($this->files as $i=>$info) |
| 92 | + $a[] = $this->_textstring(sprintf('%03d',$i)).' '.$info['n'].' 0 R'; |
| 93 | + $this->_put('<<'); |
| 94 | + $this->_put('/Names ['.implode(' ',$a).']'); |
| 95 | + $this->_put('>>'); |
| 96 | + $this->_put('endobj'); |
| 97 | + } |
| 98 | + |
| 99 | + protected function _putresources() |
| 100 | + { |
| 101 | + parent::_putresources(); |
| 102 | + $this->_putfiles(); |
| 103 | + } |
| 104 | + |
| 105 | + protected function _putfilescatalog() |
| 106 | + { |
| 107 | + if(empty($this->files)) return; |
| 108 | + |
| 109 | + $this->_put('/Names <</EmbeddedFiles '.$this->n_files.' 0 R>>'); |
| 110 | + $a = array(); |
| 111 | + foreach($this->files as $info) |
| 112 | + $a[] = $info['n'].' 0 R'; |
| 113 | + $this->_put('/AF ['.implode(' ',$a).']'); |
| 114 | + if($this->open_attachment_pane) |
| 115 | + $this->_put('/PageMode /UseAttachments'); |
| 116 | + } |
| 117 | + |
| 118 | + protected function _putcatalog() |
| 119 | + { |
| 120 | + parent::_putcatalog(); |
| 121 | + $this->_putfilescatalog(); |
| 122 | + } |
| 123 | + } |
0 commit comments