I’ve recently use pdftk (server) to do so: https://www.pdflabs.com/tools/pdftk-server/
First, install it on a webserver or locally.
Then, here’s a PHP class I adapted from the web (copy it and name it PdfFormToPdftk.php):
<?php
class PdfFormToPdftk {
/*
* Path to raw PDF form
* @var string
*/
private $pdfurl;
/*
* Path to PDFKTK
* @var string
*/
private $pdftkpath;
/*
* Path to temp files
* @var string
*/
private $tmppath;
/*
* Errors
* @var string
*/
private $errors;
/*
* Last command done
* @var string
*/
private $lastcmd;
/*
* Form data
* @var array
*/
private $data;
/*
* Path to filled PDF form
* @var string
*/
private $output;
/*
* Flag for flattening the file
* @var string
*/
private $flatten;
public function __construct($pdfurl, $data, $tmppath, $pdftkpath="/usr/bin/pdftk") {
$this->pdfurl = $pdfurl;
$this->data = $data;
$this->tmppath = $tmppath;
$this->pdftkpath = $pdftkpath;
}
private function tempfile() {
return tempnam($this->tmppath, gethostname());
}
public function fields($pretty = false) {
$tmp = $this->tempfile();
exec("{$this->pdftkpath} {$this->pdfurl} dump_data_fields > {$tmp}");
$con = file_get_contents($tmp);
unlink($tmp);
return $pretty == true ? nl2br($con) : $con;
}
private function makeFdf() {
$fdf="%FDF-1.2
1 0 obj<</FDF<< /Fields[";
foreach ($this->data as $key => $value) {
$fdf .= '<</T(' . $key . ')/V(' . $value . ')>>';
}
$fdf .= "] >> >>
endobj
trailer
<</Root 1 0 R>>
%%EOF";
$fdf_file = $this->tempfile();
file_put_contents($fdf_file, $fdf);
return $fdf_file;
}
public function flatten() {
$this->flatten = ' flatten';
return $this;
}
private function generate() {
$fdf = $this->makeFdf();
$this->output = $this->tempfile();
$cmd = "{$this->pdftkpath} {$this->pdfurl} fill_form {$fdf} output {$this->output}{$this->flatten} 2>&1";
$this->lastcmd = $cmd;
exec($cmd, $outputAndErrors, $returnValue);
$this->errors = $outputAndErrors;
unlink($fdf);
}
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);
}
if (!copy($this->output, $path)) {
echo "failed to copy $path...\n";
}
unlink($this->output);
$this->output = $path;
return $this;
}
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;
}
}
}
You could use it like this in some script:
<?php
require_once 'PdfFormToPdftk.php';
$datas = ['firstname' => 'Foo', 'lastname' => 'Bar'];
$output_file="yourfile.pdf";
$template_pdf_file="templatefile.pdf"; //where your virgin pdf template contains at least 'firstname' and 'lastname' as editable fields. You might want to use Adobe Acrobat Pro and save it as a Adobe Static PDF Form
$path_to_pdftk_server="/opt/pdflabs/pdftk/bin/pdftk"; // type 'which pdftk' in your console to find yours
$pdf = new PdfFormToPdftk($template_pdf_file, $datas, "https://stackoverflow.com/", $path_to_pdftk_server);
$file = $pdf->save($output_file);
9
solved Form to PHP to PDF: Conversion Assistance