I am using pdftk lib to generate pdf from excel-sheet data which is stored in an array using PHPExcel lib.
I run pdftk code inside a foreach loop to generate pdf for each row of data.
But it generates only 1 pdf on every run and not for every row in array.
php Debug log shows the following :-
"PHP Warning: Cannot modify header information - headers already sent by (output started at /Users/Ammarr/Sites/excel-pdf/index.php:54) in /Users/Ammarr/Sites/excel-pdf/pdftk-php/pdftk-php.php on line 67"
which is caused by pdftk-php make_pdf() function.
public function make_pdf($fdf_data_strings, $fdf_data_names, $fields_hidden, $fields_readonly, $pdf_original, $pdf_filename) {
// Create the fdf file
$fdf = $this->forge_fdf('', $fdf_data_strings, $fdf_data_names, $fields_hidden, $fields_readonly);
// Save the fdf file temporarily - make sure the server has write permissions in the folder you specify in tempnam()
$fdf_fn = tempnam("tmp", "fdf");
$fp = fopen($fdf_fn, 'w');
if($fp) {
fwrite($fp, $fdf);
fclose($fp);
// Send a force download header to the browser with a file MIME type
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"$pdf_filename\"");
// Actually make the PDF by running pdftk - make sure the path to pdftk is correct
// The PDF will be output directly to the browser - apart from the original PDF file, no actual PDF wil be saved on the server.
passthru("/usr/local/bin/pdftk $pdf_original fill_form $fdf_fn output - flatten");
// delete temporary fdf file
unlink( $fdf_fn );
}
else { // error
echo 'Error: unable to write temp fdf file: '. $fdf_fn;
}
} // end of make_pdf()
by :-------
// Send a force download header to the browser with a file MIME type
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"$pdf_filename\"");
My code to generate the pdf using pdftk class is :-
$pdfmaker = new pdftk_php; // Initiate the class
$x = 0;
foreach ($result as $r ) {
$x++;
$fdf_data_strings = array(
'Your_First_Name' => $r['Your_First_Name']
);
$fdf_data_names = array();
$fields_hidden = array(); // Used to hide form fields
$fields_readonly = array();
$pdf_original = dirname(__FILE__) . "/Fillable_PDF_vC5.pdf";
$pdf_filename = $x."-test.pdf";
$pdfmaker->make_pdf( $fdf_data_strings, $fdf_data_names, $fields_hidden, $fields_readonly, $pdf_original, $pdf_filename );
}
How can I get around the problem of modify header and download generated pdf on every loop.
I am new to php programming and will really appreciate some help by programming Ninjas here.