| Server IP : 139.59.63.204 / Your IP : 216.73.217.62 Web Server : Apache/2.4.58 (Ubuntu) System : Linux ubuntu-s-1vcpu-1gb-blr1-01 6.8.0-110-generic #110-Ubuntu SMP PREEMPT_DYNAMIC Thu Mar 19 15:09:20 UTC 2026 x86_64 User : root ( 0) PHP Version : 8.3.6 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/abyogasms.com/vendor/tecnickcom/tc-lib-pdf/examples/ |
Upload File : |
<?php
/**
* E058_embedded_files_portfolio.php
*
* @since 2026-05-01
* @category Library
* @package Pdf
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2002-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
* @link https://github.com/tecnickcom/tc-lib-pdf
*
* This file is part of tc-lib-pdf software library.
*/
// NOTE: run make deps fonts in the project root to generate the dependencies and example fonts.
// autoloader when using Composer
require __DIR__ . '/../vendor/autoload.php';
// define fonts directory
\define('K_PATH_FONTS', \realpath(__DIR__ . '/../vendor/tecnickcom/tc-lib-pdf-font/target/fonts'));
/**
* Demonstrate PDF as a document container (portfolio / package).
*
* Multiple file types are embedded inside the PDF using:
* addEmbeddedFile() – embed a file from disk
* addContentAsEmbeddedFile() – embed content generated in-memory (CSV, XML, JSON)
*
* Each embedded file is then linked to an annotation on the document page so
* that clicking the annotation in a viewer opens or exports the attachment.
*
* Embedded files:
* invoice.csv – tabular invoice data
* report.xml – XML report
* config.json – JSON configuration
*/
// main TCPDF object
$pdf = new \Com\Tecnick\Pdf\Tcpdf(
unit: 'mm',
isunicode: true,
subsetfont: false,
compress: true,
mode: '',
objEncrypt: null,
);
$pdf->setCreator('tc-lib-pdf');
$pdf->setAuthor('Nicola Asuni');
$pdf->setSubject('tc-lib-pdf example: 058');
$pdf->setTitle('Embedded Files Portfolio');
$pdf->setKeywords('TCPDF tc-lib-pdf example embedded files portfolio attachment csv xml json');
$pdf->setPDFFilename('E058_embedded_files_portfolio.pdf');
$pdf->setViewerPreferences(['DisplayDocTitle' => true]);
$pdf->enableDefaultPageContent();
$bfont = $pdf->font->insert($pdf->pon, 'helvetica', '', 10);
$bfontB = $pdf->font->insert($pdf->pon, 'helvetica', 'B', 12);
// -----------------------------------------------------------------------
// Build the in-memory attachment content
// -----------------------------------------------------------------------
$csvContent = <<<CSV
item_no,description,qty,unit_price,total
1,"Widget A",10,9.99,99.90
2,"Widget B",5,24.50,122.50
3,"Service Fee",1,150.00,150.00
,,,,
,,"Subtotal",,372.40
,,"Tax (10%)",,37.24
,,"Total",,409.64
CSV;
$xmlContent = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<report generated="2026-05-01" version="1.0">
<summary>
<title>Monthly Sales Report</title>
<period>April 2026</period>
<currency>USD</currency>
</summary>
<items>
<item id="1"><name>Widget A</name><qty>10</qty><revenue>99.90</revenue></item>
<item id="2"><name>Widget B</name><qty>5</qty><revenue>122.50</revenue></item>
<item id="3"><name>Service Fee</name><qty>1</qty><revenue>150.00</revenue></item>
</items>
<totals>
<subtotal>372.40</subtotal>
<tax>37.24</tax>
<total>409.64</total>
</totals>
</report>
XML;
$jsonContent = \json_encode(
[
'application' => 'tc-lib-pdf demo',
'version' => '1.0',
'settings' => [
'compress' => true,
'unicode' => true,
'subset_fonts' => false,
'pdf_version' => '1.7',
],
'output' => [
'mode' => 'render',
'filename' => 'E058_embedded_files_portfolio.pdf',
],
],
\JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES,
);
if ($jsonContent === false) {
throw new \RuntimeException('Failed to encode JSON content');
}
// Embed content generated in-memory
$pdf->addContentAsEmbeddedFile(
file: 'invoice.csv',
content: $csvContent,
mime: 'text/csv',
afrel: 'Data',
desc: 'Invoice line items',
);
$pdf->addContentAsEmbeddedFile(
file: 'report.xml',
content: $xmlContent,
mime: 'application/xml',
afrel: 'Data',
desc: 'Monthly sales report',
);
$pdf->addContentAsEmbeddedFile(
file: 'config.json',
content: $jsonContent,
mime: 'application/json',
afrel: 'Source',
desc: 'Library configuration',
);
// -----------------------------------------------------------------------
// Page 1 – Portfolio overview
// -----------------------------------------------------------------------
$page1 = $pdf->addPage();
$pdf->page->addContent($bfontB['out']);
$pdf->page->addContent($pdf->getTextCell(
txt: 'Embedded Files Portfolio',
posx: 15,
posy: 15,
width: 180,
height: 0,
offset: 0,
linespace: 1,
valign: 'T',
halign: 'C',
));
$pdf->page->addContent($bfont['out']);
$pdf->font->insert($pdf->pon, 'helvetica', '', 10);
$html = <<<HTML
<p>This PDF acts as a self-contained package by embedding three data files in different formats.
Click the annotation icons on this page (or use your PDF viewer's attachment panel) to
access the files.</p>
<p><b>Embedded attachments</b></p>
<table border="1" cellpadding="4" cellspacing="0">
<tr><th>Filename</th><th>MIME type</th><th>AFRelationship</th><th>Description</th></tr>
<tr><td>invoice.csv</td><td>text/csv</td><td>Data</td><td>Invoice line items</td></tr>
<tr><td>report.xml</td><td>application/xml</td><td>Data</td><td>Monthly sales report</td></tr>
<tr><td>config.json</td><td>application/json</td><td>Source</td><td>Library configuration</td></tr>
</table>
<p><b>API used</b></p>
<table border="1" cellpadding="4" cellspacing="0">
<tr><th>Method</th><th>Use case</th></tr>
<tr><td>addEmbeddedFile()</td><td>Embed a file read from disk.</td></tr>
<tr><td>addContentAsEmbeddedFile()</td><td>Embed content generated in memory.</td></tr>
<tr><td>setAnnotation() with Fileattachment subtype</td><td>Visible annotation linking to an embedded file.</td></tr>
</table>
<p>The <em>AFRelationship</em> value categorises each attachment for PDF processors:
<em>Source</em> = the source that produced this PDF; <em>Data</em> = data associated with the PDF.</p>
HTML;
$pdf->addHTMLCell(html: $html, posx: 15, posy: 30, width: 180);
// -----------------------------------------------------------------------
// Attachment annotations – link visible icons to each embedded file
// Annotation type 'FileAttachment', opt 'fs' = file key, 'name' = icon name
// -----------------------------------------------------------------------
$annotations = [
['file' => 'invoice.csv', 'label' => 'invoice.csv', 'x' => 15, 'y' => 175],
['file' => 'report.xml', 'label' => 'report.xml', 'x' => 60, 'y' => 175],
['file' => 'config.json', 'label' => 'config.json', 'x' => 105, 'y' => 175],
];
foreach ($annotations as $ann) {
$annotId = $pdf->setAnnotation(
posx: (float) $ann['x'],
posy: (float) $ann['y'],
width: 40.0,
height: 10.0,
txt: (string) $ann['label'],
opt: [
'subtype' => 'FileAttachment',
'fs' => $ann['file'],
'name' => 'PushPin',
'f' => 4,
],
);
$pdf->page->addAnnotRef($annotId, $page1['pid']);
}
// -----------------------------------------------------------------------
// Page 2 – Content preview
// -----------------------------------------------------------------------
$page2 = $pdf->addPage();
$pdf->page->addContent($bfontB['out']);
$pdf->page->addContent($pdf->getTextCell(
txt: 'Attachment Content Preview',
posx: 15,
posy: 15,
width: 180,
height: 0,
offset: 0,
linespace: 1,
valign: 'T',
halign: 'C',
));
$pdf->page->addContent($bfont['out']);
$pdf->font->insert($pdf->pon, 'helvetica', '', 10);
$previewHtml = <<<HTML
<p><b>invoice.csv (first few lines)</b></p>
<pre>item_no,description,qty,unit_price,total
1,"Widget A",10,9.99,99.90
2,"Widget B",5,24.50,122.50
3,"Service Fee",1,150.00,150.00</pre>
<p><b>report.xml (excerpt)</b></p>
<pre><report generated="2026-05-01" version="1.0">
<summary><title>Monthly Sales Report</title></summary>
<totals>
<subtotal>372.40</subtotal>
<total>409.64</total>
</totals>
</report></pre>
<p><b>config.json (excerpt)</b></p>
<pre>{
"application": "tc-lib-pdf demo",
"version": "1.0",
"settings": {
"compress": true,
"unicode": true
}
}</pre>
HTML;
$pdf->addHTMLCell(html: $previewHtml, posx: 15, posy: 30, width: 180);
// -----------------------------------------------------------------------
// Output
// -----------------------------------------------------------------------
$rawpdf = $pdf->getOutPDFString();
$pdf->renderPDF(rawpdf: $rawpdf);