溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHPExcel如何實(shí)現(xiàn)mpdf導(dǎo)出

發(fā)布時(shí)間:2021-08-31 13:41:58 來(lái)源:億速云 閱讀:107 作者:小新 欄目:開(kāi)發(fā)技術(shù)

小編給大家分享一下PHPExcel如何實(shí)現(xiàn)mpdf導(dǎo)出,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

phpexcel常用處理

##導(dǎo)入類庫(kù)
require 'PHPExcel/Classes/PHPExcel.php';
require 'PHPExcel/Classes/PHPExcel/Writer/Excel5.php'; //非07格式的寫(xiě)出類
 
##基礎(chǔ)屬性設(shè)定
$objPHPExcel = \PHPExcel_IOFactory::load('a.xls'); //讀入指定excel文件
$objPHPExcel->setActiveSheetIndex(0); //指定活動(dòng)工作表
$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setName('宋體');
$objPHPExcel->getProperties()->setTitle('xxx');
 
##單元格編輯
$objPHPExcel->getActiveSheet()->setCellValue('A3', 'xxx'); //設(shè)定A3單元格值為xxx
 
##單元格繪圖
$objDrawing = new \PHPExcel_Worksheet_Drawing();
$objDrawing->setPath('a.jpg'); //指定圖片路徑。若要遠(yuǎn)程圖片需PHPExcel/Classes/PHPExcel/Worksheet/Drawing.php:106處file_exists換成file_get_contents
$objDrawing->setCoordinates('A4'); //指定在A4單元格繪圖
$objDrawing->setName('Photo');
$objDrawing->setDescription('Photo');
$objDrawing->setHeight(120);
$objDrawing->setWidth(100);
$objDrawing->setOffsetX(7);
$objDrawing->setOffsetY(7);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
 
##excel文件瀏覽器下載導(dǎo)出
$filename='a.xls';
$encoded_filename = rawurlencode($filename);
$ua = $_SERVER["HTTP_USER_AGENT"];
header('Content-type: application/vnd.ms-excel');
if (preg_match("/MSIE/", $ua) || preg_match("/Trident\/7.0/", $ua) || preg_match("/Edge/", $ua)) {
  header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} else if (preg_match("/Firefox/", $ua)) {
  header("Content-Disposition: attachment; filename*=\"utf8''" . $filename . '"');
} else {
  header('Content-Disposition: attachment; filename="' . $filename . '"');
}
header("Pragma:no-cache");
header("Expires:0");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
 
##excel文件html顯示(可用于調(diào)試)
$objWriter = new \PHPExcel_Writer_HTML($objPHPExcel);
$objWriter->save('php://output');

利用mpdf庫(kù)從phpexcel導(dǎo)出pdf文件

$filename='a.pdf';
$encoded_filename = rawurlencode($filename);
$rendererName = \PHPExcel_Settings::PDF_RENDERER_MPDF; //指定通過(guò)mpdf類庫(kù)導(dǎo)出pdf文件
$rendererLibraryPath = 'PHPExcel/MPDF57'; //指定你下載的mpdf類庫(kù)路徑
if (!\PHPExcel_Settings::setPdfRenderer(
  $rendererName,
  $rendererLibraryPath
)) {
  die(
    'Please set the $rendererName and $rendererLibraryPath values' .
    PHP_EOL .
    ' as appropriate for your directory structure'
  );
}
header('Content-type: application/pdf');
if (preg_match("/MSIE/", $ua) || preg_match("/Trident\/7.0/", $ua) || preg_match("/Edge/", $ua)) {
  header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} else if (preg_match("/Firefox/", $ua)) {
  header("Content-Disposition: attachment; filename*=\"utf8''" . $file_name . '"');
} else {
  header('Content-Disposition: attachment; filename="' . $file_name . '"');
}
header("Pragma:no-cache");
header("Expires:0");
$objWriter = new \PHPExcel_Writer_PDF($objPHPExcel);
$objWriter->setPreCalculateFormulas(false);
$objWriter->save('php://output');
 
 
##############################
##pdf導(dǎo)出失敗的一些錯(cuò)誤解決方法
##############################
 
##1 pdf中文亂碼問(wèn)題
PHPExcel/Classes/PHPExcel/Writer/PDF/mPDF.php:105處加兩行設(shè)定:
$pdf->useAdobeCJK = true;
$pdf->SetAutoFont(AUTOFONT_ALL);
 
##2 類庫(kù)里面多處preg_replace調(diào)用使用了元字符e,而部分低版本php不支持正則表達(dá)式e元字符
e元字符的不當(dāng)使用并導(dǎo)致pdf報(bào)錯(cuò)的觸發(fā)點(diǎn)在類庫(kù)里面大概有五六處吧,
由于e元字符是一個(gè)shell下的子進(jìn)程php調(diào)用,所以報(bào)錯(cuò)信息不會(huì)反饋到當(dāng)前php進(jìn)程中,故即便你配置了錯(cuò)誤打印到屏幕, 頁(yè)面也不會(huì)顯示報(bào)錯(cuò)信息, 必須查看php報(bào)錯(cuò)日志
查看php報(bào)錯(cuò)日志,把提示的preg_replace中元字符e的調(diào)用替換為preg_replace_callback形式的調(diào)用
 
##3 部分版本phpexcel類庫(kù)有單元格樣式判斷錯(cuò)誤
lib/PHPExcel/Classes/PHPExcel/Writer/HTML.php:1236處加個(gè)if判斷
if (!$this->_useInlineCss) {
  $cssClass .= ' style' . $pSheet->getCell($endCellCoord)->getXfIndex();

以上是“PHPExcel如何實(shí)現(xiàn)mpdf導(dǎo)出”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI