Attachments

This script allows to attach files to the PDF

Informations

Author: Olivier
License: FPDF

Description

This script allows to attach files to the PDF. The method to attach a file is:

Attach(string file [, string name [, string desc]])

file: path to the file to attach.
name: the name under which the file will be attached. The default value is taken from file.
desc: an optional description.

The OpenAttachmentPane() method is also provided to force the PDF viewer to open the attachment pane when the document is loaded.

Note: this feature is supported by Adobe Reader but not by all alternative readers.

Source

<?php require('fpdf.php'); class PDF_Attachment extends FPDF { var $files = array(); var $n_files; var $open_attachment_pane = false; 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); } function OpenAttachmentPane() { $this->open_attachment_pane = true; } function _putfiles() { $s = ''; 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); $this->_newobj(); $s .= $this->_textstring(sprintf('%03d', $i)).' '.$this->n.' 0 R '; $this->_out('<<'); $this->_out('/Type /Filespec'); $this->_out('/F '.$this->_textstring($name)); $this->_out('/EF <</F '.($this->n+1).' 0 R>>'); if($desc) $this->_out('/Desc '.$this->_textstring($desc)); $this->_out('>>'); $this->_out('endobj'); $this->_newobj(); $this->_out('<<'); $this->_out('/Type /EmbeddedFile'); $this->_out('/Length '.strlen($fc)); $this->_out('>>'); $this->_putstream($fc); $this->_out('endobj'); } $this->_newobj(); $this->n_files = $this->n; $this->_out('<<'); $this->_out('/Names ['.$s.']'); $this->_out('>>'); $this->_out('endobj'); } function _putresources() { parent::_putresources(); if(!empty($this->files)) $this->_putfiles(); } function _putcatalog() { parent::_putcatalog(); if(!empty($this->files)) $this->_out('/Names <</EmbeddedFiles '.$this->n_files.' 0 R>>'); if($this->open_attachment_pane) $this->_out('/PageMode /UseAttachments'); } } ?>

Example

<?php require('attachment.php'); $pdf = new PDF_Attachment(); $pdf->Attach('attached.txt'); $pdf->OpenAttachmentPane(); $pdf->AddPage(); $pdf->SetFont('Arial', '', 14); $pdf->Write(5, 'This PDF contains an attached file.'); $pdf->Output(); ?>
View the result here.

Download

ZIP | TGZ

Quelle

http://www.fpdf.org/en/script/script95.php, zuletzt abgerufen 02.10.2016 14:57