foreach中的多个文件下载导致"无法修改标题信息"错误

时间:2015-08-31 12:27:18

标签: php loops foreach download http-headers

我使用pdftk lib从excel-sheet数据生成pdf,使用PHPExcel lib存储在数组中。

我在foreach循环中运行pdftk代码,为每行数据生成pdf。

但它每次运行时只生成1个pdf,而不是阵列中的每一行。

php调试日志显示以下内容: -

  

" PHP警告:无法修改标题信息 - 已在/ Users / Ammarr / Sites / excel中发送的文件(在/Users/Ammarr/Sites/excel-pdf/index.php:54中开始输出)第67行"

上的pdf / pdftk-php / pdftk-php.php

由pdftk-php make_pdf()函数引起。

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\"");

使用pdftk类生成pdf的代码是: -

$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 );
}

如何绕过修改标题的问题并在每个循环上下载生成的pdf。

我是php编程的新手,非常感谢Ninjas的一些帮助。

0 个答案:

没有答案