Skip to content

Instantly share code, notes, and snippets.

@bwente
Created November 27, 2017 15:29
Show Gist options
  • Save bwente/54d2cdcaeb08526b75a3e7a877c1e11c to your computer and use it in GitHub Desktop.
Save bwente/54d2cdcaeb08526b75a3e7a877c1e11c to your computer and use it in GitHub Desktop.
FormItPDF
<?php
$fileIn = $hook->formit->config['fileIn'];
$fileOut = $hook->formit->config['fileOut'];
$fileInPath = $modx->config['base_path'].$fileIn;
$fileOutPath = $modx->config['base_path'].$fileOut;
$formData = $hook->getValues();
// FDF header section
$fdf .= '%FDF-1.2
1 0 obj<</FDF<< /Fields[';
foreach ($formData as $key => $value) {
$fdf .= '<</T(' . $key . ')/V(' . $value . ')>>';
}
// FDF footer section
$fdf .= '] >> >>
endobj
trailer
<</Root 1 0 R>>
%%EOF';
// Creating a temporary file for our FDF file.
$FDFfile = tempnam(sys_get_temp_dir(), gethostname());
file_put_contents($FDFfile, $fdf);
// Merging the FDF file with the raw PDF form
exec("pdftk $fileInPath fill_form $FDFfile output $fileOutPath flatten");
// Removing the FDF file as we don't need it anymore
unlink($FDFfile);
$modx->setPlaceholder('download', $fileOut);
return true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment