dt614037527 2015-10-04 11:47
浏览 444

使用PHPExcel将xlsx转换为pdf并下载该pdf。 在localhost上工作正常

This code works fine on localhost but on live server it shows

File Not Found

Check the file name for capitalization or other typing errors. Check to see if the file was moved, renamed or deleted.

Here is my code

excel.php

<?php

error_reporting(1);
ini_set('display_errors', 0);
ini_set('display_startup_errors', TRUE);

$target = 'Myfile.xlsx';
include 'phpexcel/Classes/PHPExcel/IOFactory.php';
$inputFileType = PHPExcel_IOFactory::identify($target);

function datefix_excel($excel) {

    $dif=(41885-$excel)*86400;
    $seconds=1409737670-$dif;
    $date=date("d/m/Y",$seconds);
    return $date; }

//echo 'File ',pathinfo($inputFileName,PATHINFO_BASENAME),' has been identified as an ',$inputFileType,' file<br />';

//echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with the identified reader type<br />';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($target);

$i = 0;
$found = false;
try
{



    //
    foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) 
    {

    //
    //$objWorksheet = $objPHPExcel->getActiveSheet();
    //now do whatever you want with the active sheet

    $worksheet->setShowGridLines(false);
    $worksheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A2_PAPER );
    $worksheet->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A2_PAPER );
    $worksheet->getPageSetup()->setFitToPage(true);
    $worksheet->getPageSetup()->setFitToWidth(1);
    $worksheet->getPageSetup()->setFitToHeight(0);

    $worksheet->getPageSetup()->setScale(40);
    $worksheet->getStyle('F1:F4')->getAlignment()->setWrapText(false);
    $worksheet->getStyle('D6:D8')->getAlignment()->setWrapText(false);


    $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
    $count = 0;
    $found == false;

    //

$worksheetTitle     = $worksheet->getTitle();
$highestRow         = $worksheet->getHighestRow(); // e.g. 10
$highestColumn      = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$nrColumns = ord($highestColumn) - 64;
for ($row = 1; $row <= $highestRow; ++ $row) {
    for ($col = 0; $col < $highestColumnIndex; ++ $col) {
        $cell = $worksheet->getCellByColumnAndRow($col, $row);
        $val = $cell->getValue();

            if($val==$_REQUEST['roll'])
            {
                //echo "Roll Number found: ".$_REQUEST['roll']." <br/>";
                $found = true;
               // $objPHPExcel->getActiveSheet()->setCellValue('C23',$val);
               $objPHPExcel->getActiveSheet()->setCellValue('C23',datefix_excel($objPHPExcel->getActiveSheet()->getCell('C23')->getValue()));

                $rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
                $rendererLibrary = 'dompdf';
                $rendererLibraryPath = './' . $rendererLibrary;
                //require_once (realpath(dirname(dirname(dirname(__FILE__))))."/libraries/pdf.php");
                //echo $rendererName.' and '.$rendererLibraryPath;
                if (!PHPExcel_Settings::setPdfRenderer($rendererName,$rendererLibraryPath)) {
                    die('NOTICE: Please set the $rendererName and $rendererLibraryPath values' .EOL .'at the top of this script as appropriate for your directory structure');
                }
                header("HTTP/1.1 200 OK");
                header("Pragma: public");
                header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                header("Cache-Control: private", false);
                header('Content-Type: application/pdf');
                header('Content-Disposition: attachment;filename="rename.pdf"'); //tell browser what's the file name
                header('Cache-Control: max-age=0'); //no cache
                $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
                $objWriter->setSheetIndex($i);
                //$objWriter->save('test.pdf');
                $objWriter->save('php://output');
                break;


            }
            else{
                continue;
            }


            }
        }

    //

}   
}
catch(Exception $e)
{
    //echo $e;
}


?>
  • 写回答

1条回答 默认 最新

  • doubao12345 2015-10-04 12:56
    关注

    I would check: 1) is current folder writeable? 2) pdf.php is commented out, try enabling it (why? local machine probably has PDF installed, server does not), 3) try saving to file instead of outputting to browser, this will bypass any browser / security issues.

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序