PDF - not supported file type or file has been damaged

Topics: Developer Forum
Apr 11, 2013 at 1:39 PM
I have an application that exports an Excel spreadsheet with no problem, but when I try to get it to export in pdf I get the error message that the file is either not supported or has been damaged. I've checked other discussions and have not seen any solution or any solutions I've seen, when tried haven't worked. I did see in one discussion a mention that pdf export was no longer supported in PHPExcel. Is that true? I have version 1.7.6.

I have tried different approaches to get this pdf export to work. For properties I have used this method:
protected function set_pdf_properties(){
        $title = ucfirst($this->region) . "-" . strtoupper($this->type) . " Video Upstream Capacity Report";
        $this->vce->getProperties()->setCreator("David Newey")
                                     ->setLastModifiedBy("David Newey")
                                     ->setTitle($title)
                                     ->setSubject($title)
                                     ->setDescription("PDF document for ". $this->last_we)
                                     ->setKeywords("PDF, php capacity " . $this->last_we)
                                     ->setCategory(strtoupper($this->type) . " Capacity " . $this->last_we);    
        $this->vce->setActiveSheetIndex(0);                              
        }

//NOTE: earlier in this file the PHPExcel Object is set to $this->vce as in:
//$this->vce = new PHPExcel();
I set the headers with this method:
protected function set_pdf_headers(){
        header('Content-Type: application/pdf');
        header('Content-Disposition: attachment;filename="' . ucfirst($this->region) . "-" . $this->type . '_up_cap_report.pdf"');
        header('Cache-Control: max-age=0');    
    }
And I attempted to write the file with variations of the following method - note the commented lines are the variations I used.
public function write_pdf(){
        //$this->vce->objWriter = PHPExcel_IOFactory::createWriter($this->vce, 'PDF');
        $this->vce->objWriter = new PHPExcel_Writer_PDF($this->vce);
        $this->vce->objWriter->setPreCalculateFormulas(false);      
        $this->vce->objWriter->save('Up_Cap_Report.pdf');
        //$this->vce->objWriter->save('php://output');
        }
Is there anything in my above code that looks incorrect. I think the other code for the creation of the output is fine, since it exports in excel, so I think the problem is in my setting of the properties, headers and write method.
Coordinator
Apr 12, 2013 at 4:24 PM
denewey wrote:
I have an application that exports an Excel spreadsheet with no problem, but when I try to get it to export in pdf I get the error message that the file is either not supported or has been damaged. I've checked other discussions and have not seen any solution or any solutions I've seen, when tried haven't worked. I did see in one discussion a mention that pdf export was no longer supported in PHPExcel. Is that true? I have version 1.7.6.
Not strictly accurate. We've dropped the distribution of tcPDF within the PHPExcel distribution; but we haven't dropped PDF export.
tcPDF isn't a good renderer (not to mention it took up nearly 20x as much space in the distribution as PHPExcel did), but none of the alternatives had a license that we could work with, so we rewrote the PDF export in such a way that you can choose between using tcPDF, mPDF or DomPDF as the rendering engine; but you have to install the appropriate library yourself and tell PHPExcel which you wish to use, and where it's installed before doing an export to PDF.
Is there anything in my above code that looks incorrect. I think the other code for the creation of the output is fine, since it exports in excel, so I think the problem is in my setting of the properties, headers and write method.
Nothing obvious; but as always when you get a corrupted file being generated, open the file in a text editor and look for leading/trailing whitespace characters, BOM header, or any obvious plaintext PHP error messages
Apr 12, 2013 at 5:41 PM
Thank you. Is there somewhere I can find a discussion about which of the three choices would be best to use? And where would I find documentation on hooking one of these rendering engines to PHPExcel?

- David