Here is my version of the PdfForm.php file. I have named it 130uPdfForm.php as that is the form I plan to do first. If it looks like this file will be needed in the exact same fashion for every pdf form i plan to complete I may decide to change the name back to PdfForm.php, but we’ll see who this goes
<?php
class PdfForm
{
// this will reflect the actual PDF URL
/*
* Path to raw PDF form
* @var string
*/
private $pdfurl;
// this will reflect the data request
/*
* Form data
* @var array
*/
private $data;
// this will reflect where the final filled .pdf is stored on the server
/*
* Path to filled PDF form
* @var string
*/
private $output;
// simply flattens the file
/*
* Flag for flattening the file
* @var string
*/
private $flatten;
/**
* Class Conctructor
*
* @param string $pdfurl
* @param string $data
*/
public function __construct($pdfurl, $data)
{
$this->pdfurl = $pdfurl;
$this->data = $data;
}
/**
* Generate a filled PDF file
*
*/
private function generate()
{
$fdf = $this->makeFdf($this->data);
$this->output = $this->tmpfile();
exec("pdftk {$this->pdfurl} fill_form {$fdf} output {$this->output}{$this->flatten}");
unlink($fdf);
}
/**
* Extract fields information
*
* @param boolean $pretty
* @return string
*/
public function fields($pretty = false)
{
$tmp = $this->tmpfile();
exec("pdftk {$this->pdfurl} dump_data_fields > {$tmp}");
$con = file_get_contents($tmp);
unlink($tmp);
return $pretty == true ? nl2br($con) : $con;
}
/**
* Generate FDF file
* @param array $data
* @return string
*/
public function makeFdf($data)
{
$fdf = '%FDF-1.2
1 0 obj<</FDF<< /Fields[';
foreach ($data as $key => $value) {
$fdf .= '<</T(' . $key . ')/V(' . $value . ')>>';
}
$fdf .= "] >> >>
endobj
trailer
<</Root 1 0 R>>
%%EOF";
$fdf_file = $this->tmpfile();
file_put_contents($fdf_file, $fdf);
return $fdf_file;
}
/**
* Set the flatten flag
*
* @return pdfWriter
*/
public function flatten()
{
$this->flatten = ' flatten';
return $this;
}
/**
* Save the file
*
* @param string $path
*/
public function save($path = null)
{
if (is_null($path)) {
return $this;
}
if (!$this->output) {
$this->generate();
}
$dest = pathinfo($path, PATHINFO_DIRNAME);
if (!file_exists($dest)) {
mkdir($dest, 0775, true);
}
copy($this->output, $path);
unlink($this->output);
$this->output = $path;
return $this;
}
/**
* Force-download the filled PDF file
*
*/
public function download()
{
if (!$this->output) {
$this->generate();
}
$filepath = $this->output;
if (file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=' . uniqid(gethostname()) . '.pdf');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
readfile($filepath);
exit;
}
}
/**
* Create a temporary file and return the name
*
* @return string
*/
private function tmpfile()
{
return tempnam(sys_get_temp_dir(), gethostname());
}
}
Here my version of the Wrapper code that they said was needed to fill the PDF form
<?php
// I understand there is a MySQL server connection that needs to be made here too
require 'connect.php';
// this is the file needed to create the fdf and new pdf form
require 'PdfForm.php';
// I understand that all of these fields will need to be made var as they will reflect the data to be drawn from the finaldetails table and
// then used in the $data to fill the FDF and PDF form
var $reference_code = 'reference_code',
var $VehVin6 = 'VehVin6',
var $VehVin = 'VehVin',
var $VehPlt = 'VehPlt',
var $VehYr = 'VehYr',
var $VehMk = 'VehMk',
var $VehMod = 'VehMod',
var $VehBdy = 'VehBdy',
var $VehClr = 'VehClr',
var $VehMil = 'VehMil',
var $VehWght = 'VehWght',
var $VehCry = 'VehCry',
var $ID# = 'ID#',
var $IDOrgn = 'IDOrgn',
var $PassOrgn = 'PassOrgn',
var $Own1st = 'Own1st',
var $OwnMid = 'OwnMid',
var $OwnLst = 'OwnLst',
var $OwnEnty = 'OwnEnty',
var $OwnCnty = 'OwnCnty',
var $OwnAdd = 'OwnAdd',
var $OwnCty = 'OwnCty',
var $OwnSt = 'OwnSt',
var $OwnZp = 'OwnZp',
var $PreOwn = 'PreOwn',
var $PreCty = 'PreCty',
var $PreSt = 'PreSt',
var $Dealer = 'Dealer#',
var $LienDt = 'LienDt',
var $LienHld = 'LienHld',
var $LienAdd = 'LienAdd',
var $LienCty = 'LienCty',
var $LienSt = 'LienSt',
var $LienZp = 'LienZp',
var $Sales = 'Sales$',
var $Tax = 'Tax$'
// then used in the $data to fill the FDF and PDF form, of course, form_name will need to be changed
$data = [
'form_name' => '$VehVin6',
'form_name' => '$VehVin',
'form_name' => '$VehPlt',
'form_name' => '$VehYr',
'form_name' => '$VehMk',
'form_name' => '$VehMod',
'form_name' => '$VehBdy',
'form_name' => '$VehClr',
'form_name' => '$VehMil',
'form_name' => '$VehWght',
'form_name' => '$VehCry',
'form_name' => '$ID#',
'form_name' => '$IDOrgn',
'form_name' => '$PassOrgn',
'form_name' => '$Own1st',
'form_name' => '$OwnMid',
'form_name' => '$OwnLst',
'form_name' => '$OwnEnty',
'form_name' => '$OwnCnty',
'form_name' => '$OwnAdd',
'form_name' => '$OwnCty',
'form_name' => '$OwnSt',
'form_name' => '$OwnZp',
'form_name' => '$PreOwn',
'form_name' => '$PreCty',
'form_name' => '$PreSt',
'form_name' => '$Dealer#',
'form_name' => '$LienDt',
'form_name' => '$LienHld',
'form_name' => '$LienAdd',
'form_name' => '$LienCty',
'form_name' => '$LienSt',
'form_name' => '$LienZp',
'first_name' => '$Sales$',
'form_name' => '$Tax$'
];
// Data can be fetched from different sources like a database table, a JSON object or just an array as
// we did in the above snippet.
//Data to be fetched from database:thetitl1_Livesite754 and table:finaldetails
$pdf = new PdfForm('/130u.pdf', $data);
$pdf->flatten()
->save('/130u-output.pdf')
->download();
Not sure if what I’m doing is right, but I’m trying to follow what I’ve gleaned so far. Please forgive me if my coding make you cringe…LOL