Welcome Guest, Not a member yet? Register   Sign In
PHPExcel with mPDF function.
#1

[eluser]Unknown[/eluser]
I try to implemetn the PHPExcel with mPDF library.
Right now I am in the point that can let the user download the pdf file but once download completed, I could not be able to open the PDF file at all.

Code:
$this->load->library('mpdf/mpdf');
require_once(dirname(__FILE__) . '/../libraries/PHPExcel/Settings.php');

$rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
$rendererLibraryPath = dirname(__FILE__) . '/../libraries/mpdf';

$objPHPExcel = new PHPExcel();

$objPHPExcel->getProperties()->setCreator("TEST PDF")
  ->setLastModifiedBy("TEST")
  ->setTitle("TEST PDF")
  ->setSubject("TEST PDF")
  ->setDescription("TEST PDF")
  ->setKeywords("TEST PDF")
  ->setCategory("TEST PDF");

$objPHPExcel->setActiveSheetIndex(0);

// Field names in the first row
$fields = $query->list_fields();
$col = 0;
foreach ($fields as $field)
{
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
$col++;
}

// Fetching the table data
$row = 2;
foreach($query->result() as $data)
{
$col = 0;
foreach ($fields as $field)
{
  $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field);
  $col++;
}

$row++;
}

$objPHPExcel->setActiveSheetIndex(0);

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' . '<br/>' .
  $rendererName . '<br/>' .
  $rendererLibraryPath . '<br/>'
);
}

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

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

When I use this code, it offer the user to download the PDF file, but could not be able to open.

The error message stated,

Adobe Reader could not open 'test.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).

Have anyone run into this problem before?

- I include the mpdf and phpexcel in libraries folder.

Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB