Look ma! No WordPress required!
In the documentation for PDF Ink, it is explained how to write a simple script to fire up the PDF Ink PHP library and stamp a PDF file. The library is written in such a way that it can be used without WordPress, and any one of six different PDF-manipulation library sets can be used. This is helpful when one set doesn’t work with a specific PDF, but another set might. As always, when PDF Ink fails for any specific PDF, the solution is to either attempt to repair the PDF — or upgrade to a better library, like SetaPDF-Stamper.
The example in the documentation is for TCPDF/FPDF, so here we will include an example for when using SetaPDF-Stamper. Folder structure: we put an index.php file above the ‘pdf-ink’ folder, and placed an ‘invoice.pdf’ file right there with that index file. The SetaPDF library was placed in the PDF Ink pdf/lib folder.
<?php
/**
* Make sure the SetaPDF library is added to the /lib folder
* and that the Autoload.php file is present
*/
include_once __DIR__ . '/pdf-ink/pdf/lib/SetaPDF/Autoload.php';
// Include the path to pdf-ink/pdf/includes.php file
if ( include_once __DIR__ . '/pdf-ink/pdf/includes.php' ) {
pdfink_stamping_function();
}
function pdfink_stamping_function() {
$settings = [
'setup' => [
'start_page' => 1, // or start on page 2, enter a number
'end_page' => 'last',
'pages' => 'all', // all, odd, even, first, last, or custom
'custom' => '1', // e.g. 1-5, 8, 22-35 or 2-5, 33
'margin_top_bottom' => 0,
'margin_left_right' => 0,
'setapdf_font' => 'helvetica', // other included choices are 'courier' and 'times'
],
'placement' => [
0 => [
'content' => 'hello world',
'color' => '#FF0000', // hex color, default black
'size' => 32, // font size
'opacity' => 0.75, // 0-1 opacity, default 1
'spacing' => 35,
'align' => 'C', // L for left, C for center, R for right
'position' => 'M', // T for top, M for middle, B for bottom
'x' => 0,
'y' => 100,
'rotate' => 5, // 1-359, default 0
'image' => [
'path' => '../../path/to/image.png', // local path to image file
'width' => 400, // image width in pixels
'height' => 300, // image width in pixels
],
],
// A second placement would be index 1, second array here
],
'security' => [
'fallback' => 'off', // 'on' if you want PDF delivered anyway if manip. fails
'encrypt' => 'none', // OR 'rc440' OR 'rc4128' OR 'aes128' OR 'aes256' (FPDF does not have AES encryption)
'permissions' => [], // array() including maybe 'print', 'modify', 'copy', 'annot', 'fill-forms', 'extract', 'assemble', and/or 'print-high'
'pwd' => '', // Blank means it opens without password
'pwd_owner' => 's90fjl1Fl1k^skas#PPl!',
'pwd_owner_original'=> null, // Opens passworded PDF for manipulation
'unlock' => 'off', // DANGER ZONE, do not turn 'on' unless you want PDF 100% unlocked with user password
],
];
$strategy = \LittlePackage\pdfInk\PDF\Strategy\pdfInk_PDF_Strategy_Factory::createStrategy( 'setapdf' );
$handler = new \LittlePackage\pdfInk\PDF\Handler\pdfInk_PDF_Handler();
$handler->set_handler( $strategy );
$handler->parse_pdf( 'invoice.pdf' );
if ( ! is_dir( 'stamped' ) ) {
mkdir( 'stamped' );
}
$handler->write_pdf( 'stamped/invoice.pdf', $settings );
$handler->output_pdf( null, null );
// Redirect to the created file:
header( "Location: stamped/invoice.pdf" );
exit;
}