0

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.

NASSER
  • 5,900
  • 7
  • 38
  • 57
ammarr
  • 23
  • 3
  • You can only send one response per HTTP request. There's no way to transfer multiple PDFs at once. (Short of wrapping them in a ZIP container prior outputting.) – mario Aug 31 '15 at 12:35
  • Thank you for your response. I understand that only one response per http can be sent, so how can I achieve downloading the files inside a loop, is there an alternative to using "header("Content-Type: application/force-download");" inside the loop to download or save file to the system? – ammarr Aug 31 '15 at 12:42
  • A javascript redirect to the file path will do the trick but keep this in mind that we can't force a download via JavaScript. We can just open the file link. Browser will start downloading the file if any PDF reader isn't installed on it else the file will be open. Echo "" instead of header – Rehmat Aug 31 '15 at 12:44
  • Of course you can squeeze all PDFs into one download. But no client would be able to separate those files again. It would just look like one file with lots of trailing garbage appended. – mario Aug 31 '15 at 12:46
  • Thank you for the help & information. I am still not sure how to modify my current code to achieve this. – ammarr Aug 31 '15 at 13:25

0 Answers0