Failed to load PDF

Topics: User Forum
Feb 10 at 3:21 PM
Environment:
Windows 7
XAMPP 5.6.14
Php 5.6.14
Apache 2.4.17
phpExcel 1.8.0
mPDF 6.0
Codeigniter 3.0.3

I can successfully read an excel spreadsheet, update some cells and save it to the server. However, creating a pdf appears to work, but when trying to open it I get ‘Failed to load pdf’
Error ;
'Adobe Reader could not open 'test.pdf' because it is either not a supported file type or because the file has been damaged'.

I can create a new spreadsheet and the created pdf file can be opened.
The problem appears to occur when reading an exist .xlsx file.

Working code:
          error_reporting(E_ALL);
          date_default_timezone_set('Europe/London');
    $curdir = $this->config->_config_paths[0];
    require_once($curdir . 'third_party/Classes/PHPExcel/Settings.php');
    $rendererLibrary = 'mpdf';
    $rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
    $rendererLibraryPath = $curdir . 'third_party/mpdf';
    if (!PHPExcel_Settings::setPdfRenderer(
        $rendererName,
        $rendererLibraryPath
    ));
           $objPHPExcel = new PHPExcel();
           $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
                         ->setLastModifiedBy("Maarten Balliauw")
                         ->setTitle("PDF Test Document")
                         ->setSubject("PDF Test Document")
                         ->setDescription("Test document for PDF, generated using PHP classes.")
                         ->setKeywords("pdf php")
                         ->setCategory("Test result file");
                   $objPHPExcel->setActiveSheetIndex(0)
                                 ->setCellValue('A1', 'Hello')
                              ->setCellValue('B2', 'world!')
                         ->setCellValue('C1', 'Hello')
                           ->setCellValue('D2', 'world!');

                    $objPHPExcel->getActiveSheet()->setTitle('Simple');
                 // Set active sheet index to the first sheet, so Excel opens this as the first sheet
                   $objPHPExcel->setActiveSheetIndex(0);
                  // Redirect output to a client’s web browser (Excel2007)
                  header('Content-Type: application/pdf');
                   header('Content-Disposition: attachment;filename="01simple.pdf"');
                 header('Cache-Control: max-age=0');
                 $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
                   $objWriter->save('php://output');
Not working:
    $curdir = $this->config->_config_paths[0];
    require_once($curdir . 'third_party/Classes/PHPExcel/Settings.php');
    $rendererLibrary = 'mpdf';
    $rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
    $rendererLibraryPath = $curdir . 'third_party/mpdf';
    if (!PHPExcel_Settings::setPdfRenderer(
        $rendererName,
        $rendererLibraryPath
    ));
    $file = './assets/files/test2.xlsx';
    //read file from path
    $objPHPExcel = new PHPExcel();
    $objPHPExcel = PHPExcel_IOFactory::load($file);
    //get only the Cell Collection
    $objPHPExcel->setActiveSheetIndex(0);
    $cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();
    $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', 'Hello')
                ->setCellValue('B2', 'world!')
                ->setCellValue('C1', 'Hello')
                ->setCellValue('D2', 'world!');
    $objPHPExcel->setActiveSheetIndex(0);
    // Redirect output to a client’s web browser (Excel2007)
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment;filename="01simple.pdf"');
    header('Cache-Control: max-age=0');
    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
    $objWriter->save('php://output');

Using the following 2 lines will create the file on the server
$writer = \PHPExcel_IOFactory::createWriter($myExcel, 'Excel2007');
$writer->save(str_replace(__FILE__, 'assets/files/output.xlsx', __FILE__));
Feb 10 at 4:40 PM
The problem was related to formatting in the original excel spreadsheet. I will need to investigate further to address my formatting issues.
Marked as answer by kea42 on 2/10/2016 at 8:40 AM