如何在PHPExcel中设置流内容?

时间:2015-04-18 09:38:11

标签: php phpexcel dompdf

我正在使用PHP和PHPExcel从数据库中获取图像文件名并执行流阅读器操作。我使用setImageResource函数设置了我的图像,但它没有按预期工作。这是我的代码:

<?php
    require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
    require_once '../classes/PHPExcel/IOFactory.php';
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    date_default_timezone_set('Asia/Calcutta');

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

    $rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
    $rendererLibrary = 'dompdf.php';
    $rendererLibraryPath = dirname(__FILE__) . '/../libs/classes/dompdf/';

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

    $objPHPExcel->setActiveSheetIndex(0);
    $objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Tes');
    $objPHPExcel->getActiveSheet()->SetCellValue('B1', 'Image...');
    $objPHPExcel->getActiveSheet()->setTitle('Simple');
    $img = imagecreatefrompng('C:/Users/Downloads/logo.png');
    $objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
    $objDrawing->setName('Test image');
    $objDrawing->setDescription('Test image');
    $objDrawing->setImageResource($img);
    $objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG);
    $objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
    $objDrawing->setHeight(150);
    $objDrawing->setCoordinates('A4');
    $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());



    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'
        );
    } 

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
    $objWriter->save('test.pdf');
    exit;

1 个答案:

答案 0 :(得分:4)

哎呀,如果你坚持使用流内容并将其传递给你的工作表,你的PDF将无法呈现你的图像,那么第一件事PHPExcel_Worksheet_MemoryDrawing()无法解决你的问题。但是如果你想渲染图像,也可以使用`PHPExcel_Worksheet_Drawing()'。你可以这样欺骗它:

  1. 您可以从stream_get_contentsfile_get_contents
  2. 检索您的图片文件
  3. 然后将其重新创建到文件中。
  4. 最后一次将图像文件路径传递给$objDrawing->setPath($path_to_your_image);
  5. 因此,如果您想要将图像呈现给工作表,则必须使用PHPExcel_Worksheet_Drawing(),为此您必须指向文件路径以呈现该图像。

    我认为这是PHPExcelDomPDF合作的问题。因此,请在此处稍加讨论并编辑objDrawing至此:

    $objDrawing = new PHPExcel_Worksheet_Drawing();
    $objDrawing->setPath('logo.png');
    

    对于我制作的完整样本,试试这个:

    include 'PHPExcel.php';
    require_once("dompdf/dompdf_config.inc.php");
    
    
    $rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
    $rendererLibrary = 'dompdf.php';
    $rendererLibraryPath = dirname(__FILE__). '/dompdf';
    
    
    $objPHPExcel = new PHPExcel();
    $objPHPExcel->setActiveSheetIndex(0);
    $objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Tes');
    $objPHPExcel->getActiveSheet()->SetCellValue('B1', 'Image...');
    $objPHPExcel->getActiveSheet()->setTitle('Simple');
    
    $objPHPExcel->getActiveSheet()
    ->getColumnDimension('A')
    ->setWidth(50);
    
    $objPHPExcel->getActiveSheet()
    ->getRowDimension('2')
    ->setRowHeight(200);
    
    // $img = imagecreatefrompng('logo.png');
    $data = file_get_contents("logo.png");
    // $img = imagecreatefromstring($data);
    $fp = fopen("logo1.png","w");
    fwrite($fp,$data);
    fclose($fp);
    
    $objDrawing = new PHPExcel_Worksheet_Drawing();
    $objDrawing->setPath('logo1.png');
    
    // $objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
    $objDrawing->setName('Test image');
    $objDrawing->setDescription('Test image');
    // $objDrawing->setImageResource($img);
    // $objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG);
    // $objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT);
    // $objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
    $objDrawing->setHeight(150);
    $objDrawing->setCoordinates('A2');
    $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
    
    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'
        );
    }
    
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
    
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
    $objWriter->save('test.pdf');
    
    echo date('H:i:s') . " Done writing file.\r\n";
    

    很高兴为您提供帮助,希望这会帮助您:)