-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from erikn69/patch-8
Support Attachments
- Loading branch information
Showing
15 changed files
with
285 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# AttachmentsTrait | ||
![GitHub license](https://img.shields.io/badge/license-FPDF-green) | ||
[![Author](https://img.shields.io/badge/author-Olivier-blue)](mailto:[email protected]?subject=Bookmarks) | ||
|
||
This script allows to attach files to the PDF. | ||
|
||
## Usage | ||
The method to attach a file is: | ||
|
||
```php | ||
/** | ||
* 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 | ||
*/ | ||
AttachmentsTrait::Attach(string file [, string name [, string desc]]); | ||
``` | ||
|
||
The `OpenAttachmentPane()` method is also provided to force the PDF viewer to open the attachment pane when the document is loaded. | ||
|
||
## Example | ||
|
||
```php | ||
<?php | ||
declare(strict_types=1); | ||
|
||
require dirname(dirname(__DIR__)) . '/fpdf/fpdf.php'; | ||
require __DIR__ . '/AttachmentsTrait.php'; | ||
|
||
use FPDF\Scripts\Attachments\AttachmentsTrait; | ||
|
||
$pdf = new class extends FPDF { | ||
use AttachmentsTrait; | ||
}; | ||
|
||
$pdf->Attach('attached.txt'); | ||
$pdf->OpenAttachmentPane(); | ||
$pdf->AddPage(); | ||
$pdf->SetFont('Arial','',14); | ||
$pdf->Write(5,'This PDF contains an attached file.'); | ||
|
||
$pdf->Output('F', __DIR__ . '/example.pdf'); | ||
``` | ||
[Result](ex.pdf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Attached file. |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
require dirname(dirname(__DIR__)) . '/fpdf/fpdf.php'; | ||
require __DIR__ . '/AttachmentsTrait.php'; | ||
|
||
use FPDF\Scripts\Attachments\AttachmentsTrait; | ||
|
||
$pdf = new class extends FPDF { | ||
use AttachmentsTrait; | ||
}; | ||
|
||
$pdf->Attach('attached.txt'); | ||
$pdf->OpenAttachmentPane(); | ||
$pdf->AddPage(); | ||
$pdf->SetFont('Arial','',14); | ||
$pdf->Write(5,'This PDF contains an attached file.'); | ||
|
||
$pdf->Output('F', __DIR__ . '/example.pdf'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
<title>Attachments</title> | ||
<style type="text/css"> | ||
body {font-family:"Times New Roman",serif} | ||
h1 {font:bold 135% Arial,sans-serif; color:#4000A0; margin-bottom:0.9em} | ||
h2 {font:bold 95% Arial,sans-serif; color:#900000; margin-top:1.5em; margin-bottom:1em} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>Attachments</h1> | ||
<h2>Informations</h2> | ||
Author: <a href="mailto:[email protected]?subject=Attachments">Olivier</a><br> | ||
License: FPDF | ||
<h2>Description</h2> | ||
This script allows to attach files to the PDF. The method to attach a file is:<br> | ||
<br> | ||
<tt>Attach(<b>string</b> file [, <b>string</b> name [, <b>string</b> desc]])</tt><br> | ||
<br> | ||
<tt><u>file</u></tt>: path to the file to attach.<br> | ||
<tt><u>name</u></tt>: the name under which the file will be attached. The default value is taken from <tt>file</tt>.<br> | ||
<tt><u>desc</u></tt>: an optional description.<br> | ||
<br> | ||
The <code>OpenAttachmentPane()</code> method is also provided to force the PDF viewer to open the attachment | ||
pane when the document is loaded.<br> | ||
<br> | ||
<strong>Note:</strong> this feature is supported by Adobe Reader but not by all alternative readers. | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Fawno\FPDF\Tests\Scripts; | ||
|
||
use FPDF; | ||
use FPDF\Scripts\Attachments\AttachmentsTrait; | ||
use Fawno\FPDF\Tests\TestCase; | ||
|
||
class AttachmentsTraitTest extends TestCase { | ||
public function testAttachmentsTrait () { | ||
$pdf = new class extends FPDF { | ||
use AttachmentsTrait; | ||
}; | ||
|
||
$pdf->Attach(__DIR__ . '/../../scripts/Attachments/attached.txt'); | ||
$pdf->OpenAttachmentPane(); | ||
$pdf->AddPage(); | ||
$pdf->SetFont('Arial','',14); | ||
$pdf->Write(5,'This PDF contains an attached file.'); | ||
|
||
$this->assertFileCanBeCreated($pdf); | ||
|
||
$this->assertPdfIsOk($pdf); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters