2013-04-14 44 views
0

我想用下面的代碼創建一個使用PHPexcel和MPDF庫的PDF文檔,但出現錯誤 - 「Adobe Reader無法打開'01simple-6.pdf'因爲它不是受支持的文件類型或文件已被損壞(例如,它是作爲電子郵件附件發送的,未正確解碼)「。我究竟做錯了什麼?用PHPexcel + Mpdf創建PDF文檔時出錯

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', TRUE); 
ini_set('display_startup_errors', TRUE); 
date_default_timezone_set('Europe/London'); 

if (PHP_SAPI == 'cli') 
    die('This example should only be run from a Web Browser'); 

/** Include PHPExcel */ 
require_once '../Classes/PHPExcel.php'; 
$rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF; 
$rendererLibrary = 'mpdf.php'; 
$rendererLibraryPath = dirname(__FILE__).'/../../MPDF56/' . $rendererLibrary; 

// Create new PHPExcel object 
$objPHPExcel = new PHPExcel(); 

// Set document properties 
$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"); 


// Add some data 
$objPHPExcel->setActiveSheetIndex(0) 
      ->setCellValue('A1', 'Hello') 
      ->setCellValue('B2', 'world!') 
      ->setCellValue('C1', 'Hello') 
      ->setCellValue('D2', 'world!'); 

// Miscellaneous glyphs, UTF-8 
$objPHPExcel->setActiveSheetIndex(0) 
      ->setCellValue('A4', 'Miscellaneous glyphs') 
      ->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç'); 

// Rename worksheet 
$objPHPExcel->getActiveSheet()->setTitle('Simple'); 
$objPHPExcel->getActiveSheet()->setShowGridLines(false); 

// Set active sheet index to the first sheet, so Excel opens this as the first sheet 
$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' 
    ); 
} 

// Redirect output to a client’s web browser (PDF) 
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'); 
exit; 
+1

用你喜歡的文本編輯器打開文件,找出不屬於那裏的東西。 –

+0

正如Alvaro所說 - 特別是在文件 –

+0

中檢查前導或尾隨空格,領先的BOM標記或任何明顯的明文PHP錯誤消息嗨,我試圖使用此庫,但不知道在哪裏存儲此代碼。我應該創建一個文件嗎?我在哪裏放? – Limon

回答

2

嘗試關閉錯誤報告。我有同樣的錯誤,然後我看到在文件輸出之前輸出的一些通知和警告。

所以我關閉了錯誤報告,它開始工作。