Perl Requires Chilkat v9.5.0.97+
Chilkat2 Perl
Add File Attachments to a PDF
See more PDF Signatures Examples
Demonstrates how to attach files to a PDF. This is also known as embedding a file within a PDF.Chilkat Perl Downloads
use strict;
use warnings;
use Chilkat2;
# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
my $pdf = Chilkat2::Pdf->new;
# We'll be adding attachments to this PDF file..
# Note: We are loading the PDF file into memory. The PDF file is not kept open at this point.
my $success = $pdf->LoadFile('qa_data/pdf/helloWorld.pdf');
if (!$success) {
print $pdf->LastErrorText, "\n";
exit;
}
# Build JSON to provide information about the files to be embedded in the PDF.
my $json = Chilkat2::JsonObject->new;
$json->EmitCompact(0);
my $i = 0;
# We can attach multiple files in one operation.
# Here we specify the local relative file path of the 1st file to be embedded.
$json->I($i);
$json->UpdateString('files[i].description', 'Hello World');
$json->UpdateString('files[i].localFilePath', 'qa_data/xml/helloWorld.xml');
$i = $i + 1;
# Specify the 2nd file to be attached.
$json->I($i);
$json->UpdateString('files[i].description', 'Image of starfish');
$json->UpdateString('files[i].localFilePath', 'qa_data/jpg/starfish.jpg');
# You can explicitly specify the subType (i.e. MIME type) of the file.
# Otherwise Chilkat will use the default MIME type based on the filename extension.
# If no default MIME type exists, then "application/octet-stream" is used.
$json->UpdateString('files[i].subType', 'image/jpeg');
$i = $i + 1;
# You can alternatively provide the file data from base64 encoded data rather than from a local file.
$json->I($i);
$json->UpdateString('files[i].description', 'Hello World from Data');
$json->UpdateString('files[i].fileData', 'SGVsbG8gV29ybGQsIEhlbGxvIFdvcmxkLCBIZWxsbyBXb3JsZCwgSGVsbG8gV29ybGQsIEhlbGxvIFdvcmxk');
$json->UpdateString('files[i].embeddedFilename', 'abc.txt');
$i = $i + 1;
# By default, the filename used within the PDF is the filename part of the local file path.
# You can change it by specifying the embeddedFilename.
$json->I($i);
$json->UpdateString('files[i].description', 'Invoice');
$json->UpdateString('files[i].localFilePath', 'qa_data/xml/invoice.xml');
$json->UpdateString('files[i].embeddedFilename', 'invoice_1234.xml');
$json->UpdateString('files[i].subType', 'application/xml');
# The above JSON provides instructions for attaching 4 files to the PDF.
# Let's attach the files..
# It is perfectly acceptable to write over the PDF file that was loaded,
# but in this example we'll write the PDF with attachments to a new PDF file.
$success = $pdf->AddEmbeddedFiles($json, 'qa_output/helloWorld_withAttachments.pdf');
if (!$success) {
print $pdf->LastErrorText, "\n";
exit;
}
# You can alternatively write the PDF file with attachments to a Chilkat BinData object..
my $bd = Chilkat2::BinData->new;
$success = $pdf->AddEmbeddedFilesBd($json, $bd);
if (!$success) {
print $pdf->LastErrorText, "\n";
exit;
}
# Then you can do what you want with the BinData, which contains the binary image of the PDF with attachments.
print "Success.\n";