Chrome使用PHPExcel在内联PDF中“无法加载PDF文档”错误消息

时间:2015-01-02 05:43:08

标签: php google-chrome pdf pdf-generation phpexcel

我在使用PHP浏览pdf文件时遇到问题,并且由PHPExcel Library生成。它在Firefox中正常工作但不在chrome中工作。请帮帮我

以下代码是我在PHP中的操作

    require_once 'PHPExcel.php';
    $rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
    $rendererLibrary = 'dompdf';

     $objPHPExcel = new PHPExcel();

        //  set the paper size and the paper view style
        $objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
        $objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);

        //  make cell content text-align center
        $objPHPExcel->getDefaultStyle()
                    ->getAlignment()
                    ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        //  border for all cells
        $styleArray = array(
                        'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN
                            )
                        )
                    );
        $objPHPExcel->getDefaultStyle()->applyFromArray($styleArray);

        // Set document properties
        $objPHPExcel->getProperties()->setCreator("Occasional Riot")
                                     ->setLastModifiedBy("Occasional Riot")
                                     ->setTitle("Interest Area Document")
                                     ->setSubject("Interest Area List")
                                     ->setDescription("The list of the Interest Area")
                                     ->setKeywords("office 2007 openxml php")
                                     ->setCategory("Interest Area file");

        foreach(range('B','F') as $columnID) {
            $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)
                ->setAutoSize(true);
        }
        // Add some data
        $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A1', 'Social')
                    ->setCellValue('H2', 'Venue');

        $objPHPExcel->setActiveSheetIndex(0)->mergeCells('A1:B1');



        $objPHPExcel->getActiveSheet()->getStyle("A1")->getFont()->setBold(true);


        if (!PHPExcel_Settings::setPdfRenderer(
                $rendererName,
                $rendererLibraryPath
            )) {
            die(
                'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
                '<br />' .
                'at the top of this script as appropriate for your directory structure'
            );
        }

        // Redirect output to a client’s web browser (PDF)
        header('Content-Type: application/pdf');
        header('Content-Disposition: attachment;filename="Interest Area.pdf"');
        header('Cache-Control: max-age=0');

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
        $objWriter->writeAllSheets();
        $objWriter->save('php://output');

        $objWriter->save('interest.pdf');
        readfile("interest.pdf");
        exit;

1 个答案:

答案 0 :(得分:3)

派对迟到了,但是发生了类似的错误。

我不得不用PHP填充PDF并在之后显示,但我总是得到一个&#34;无法加载PDF文档&#34;来自Chrome的错误。

我不知道它是否是同一个问题,但在我的情况下,PHP报告了通知和警告错误,没有什么太重要,但正因为如此,PDF无法加载。隐藏错误解决了我的问题!

TL; DR:在你的error_reporting(E_STRICT);之前,在你的PHP文件(快速和脏)或exit;之上尝试readfile("interest.pdf");,这样你就可以看到错误并修复它们。