{ "require": { "monolog/monolog": "1.11.0", #处理日志 "doctrine/orm": "2.3.6", #db orm "imagine/imagine": "0.6.2", #图片处理 "chrisboulton/php-resque": "1.2", #队列 "ezyang/htmlpurifier": "4.6.0", #html过滤 "predis/predis": "1.0.0", #redis操作类 "knplabs/knp-snappy": "0.3.1", #根据html或url生成pdf或image "phpoffice/phpexcel": "1.8.0", #生成和读取excel "mpdf/mpdf": "5.7.3", #生成pdf "tecnick.com/tcpdf": "6.0.099", #生成pdf "dompdf/dompdf": "0.6.1" #生成pdf } }
<?php namespace test; error_reporting(E_ALL); require __DIR__.'/../vendor/autoload.php'; //test log use Monolog\Logger; use Monolog\Handler\StreamHandler; #普通的文件日志 use Monolog\Handler\RotatingFileHandler; #一天写一个文件 use Knp\Snappy\Pdf;#生成pdf use Knp\Snappy\Image;#生成pdf use Imagine\Gd\Imagine;#用于图片处理 class test { private $snappyPdfPath = '/usr/local/wkhtmltopdf/bin/wkhtmltopdf.exe'; private $snappyImgPath = '/usr/local/wkhtmltopdf/bin/wkhtmltoimage.exe'; public function write_log() { // create a log channel $log = new Logger('test_log'); //$log->pushHandler(new StreamHandler(__DIR__.'/log/app.log', Logger::DEBUG)); $log->pushHandler(new RotatingFileHandler(__DIR__.'/log/app.log', 2, Logger::DEBUG)); // add records to the log $log->addWarning('this is test log', array('txxxxxxxxxx'=>'bbbbbbbbbbb', 'ss' => '11')); $log->addError('this is test log2'); } public function image() { $imageine = new Imagine(); $size = new \Imagine\Image\Box(5000, 1000); //大小 $mode = \Imagine\Image\ImageInterface::THUMBNAIL_INSET; //自适应 //$mode = \Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND; //会砍掉一部份 //生成缩略图 $imageine->open(__DIR__.'/image/test.png')->thumbnail($size, $mode)->save(__DIR__.'/image/thumb/test_thumb.png'); } /** * disable_functions里的配置不能包括escapeshellarg,proc_open * 同时需要在http://wkhtmltopdf.org/downloads.html下载好对应的包安装好 */ public function html2pdf() { $snappy = new Pdf(); $snappy->setBinary($this->snappyPdfPath); //根据thml生成 //$snappy->generateFromHtml('<h1>Bill</h1><p>You owe me money, dude.</p>', '/tmp/bill-123.pdf'); //直接输出下载 /*header('Content-Type: application/pdf'); header('Content-Disposition: attachment; filename="file.pdf"'); echo $snappy->getOutput('http://www.xx.com/xx.html');*/ //保存到服务器 $snappy->generate('http://www.xx.com/xx.html', __DIR__.'/pdf/test.pdf'); } /** * 同上面的pdf */ public function html2images() { $snappy = new Image(); $snappy->setBinary($this->snappyImgPath); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="file.jpg"'); echo $snappy->getOutput('http://www.xx.com/xx.html'); } /** * 写excel,需要扩展xml、xmlwriter、zip、gd2 * http://phpexcel.codeplex.com/ * https://github.com/PHPOffice/PHPExcel/wiki/User%20Documentation * doc: http://www.cnblogs.com/freespider/p/3284828.html * http://blog.sina.com.cn/s/blog_44b3f96d0101cczo.html */ public function writeExcel() { $objPHPExcel = new \PHPExcel(); //单一处理 /*$objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', '这个是A1') ->setCellValue('B2', '这个是B2!') ->setCellValue('C1', '这个是C1') ->setCellValue('D2', '这个是D2'); $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save(__DIR__.'/excel/test.xlsx');*/ //批量写入数据 $arrData = array( array('测试数据1', '测试数据2'), array('测试数据1', '测试数据12'), array('测试数据11', '测试数据112'), array('测试数据1', '测试数据1112'), array('测试数据1', '测试数据1112'), ); $objPHPExcel->setActiveSheetIndex(0); foreach ($arrData as $key => $value) { $objPHPExcel->getActiveSheet()->setCellValue('A' . ($key+1), $value[0]); $objPHPExcel->getActiveSheet()->setCellValue('B' . ($key+1), $value[1]); } $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save(__DIR__.'/excel/test1.xlsx'); } /** * 读excel * * result: * A1 - 测试数据1 * B1 - 测试数据2 * A2 - 测试数据1 * B2 - 测试数据12 * A3 - 测试数据11 * B3 - 测试数据112 * A4 - 测试数据1 * B4 - 测试数据1112 * A5 - 测试数据1 * B5 - 测试数据1112 */ public function readExcel() { header("Content-type:text/html;charset=utf-8"); $objReader = \PHPExcel_IOFactory:: createReader('Excel2007' ); $objPHPExcel = $objReader->load( __DIR__.'/excel/test2.xlsx' ); foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) { //遍历工作表 echo 'Worksheet - ' , $worksheet->getTitle() , '<br>'; foreach ($worksheet->getRowIterator() as $row) { //遍历行 echo ' Row number - ' , $row->getRowIndex() , '<br/>'; $cellIterator = $row->getCellIterator(); //得到所有列 $cellIterator->setIterateOnlyExistingCells( false); // Loop all cells, even if it is not set foreach ($cellIterator as $cell) { //遍历列 if (!is_null($cell)) { //如果列不给空就得到它的坐标和计算的值 echo $cell->getCoordinate() , ' - ' , $cell->getCalculatedValue() , '<br>'; } } } } } /** * excel的内容写到pdf里 * * 需要安装: * Library Version used for testing Downloadable from PHPExcel Internal Constant * tcPDF 5.9 http://www.tcpdf.org/ PDF_RENDERER_TCPDF * mPDF 5.4 http://www.mpdf1.com/mpdf/ PDF_RENDERER_MPDF * domPDF 0.6.0 beta 3 http://code.google.com/p/dompdf/ PDF_RENDERER_DOMPDF */ public function excelToPdf() { $objPHPExcel = new \PHPExcel(); //批量写入数据 $arrData = array( array('Downloadable', 'Downloadable'), array('Downloadable', 'Downloadable2'), array('Downloadable1', 'Downloadable12'), array('Downloadable', 'Downloadable112'), array('Downloadable', 'Downloadable就'), ); $objPHPExcel->setActiveSheetIndex(0); foreach ($arrData as $key => $value) { $objPHPExcel->getActiveSheet()->setCellValue('A' . ($key+1), $value[0]); $objPHPExcel->getActiveSheet()->setCellValue('B' . ($key+1), $value[1]); } $rendererName = \PHPExcel_Settings::PDF_RENDERER_DOMPDF; $rendererLibraryPath = __DIR__.'/../vendor/dompdf/dompdf'; \PHPExcel_Settings::setPdfRenderer( $rendererName, $rendererLibraryPath ); $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF'); $objWriter->setIncludeCharts(TRUE); $objWriter->save(__DIR__.'/pdf/excel.pdf'); } } $testObj = new test(); //phpinfo(); //写日志 //$testObj->write_log(); //图片处理 //$testObj->image(); //生成pdf //$testObj->html2pdf(); //生成图 //$testObj->html2images(); //写excel //$testObj->writeExcel(); //读excel //$testObj->readExcel(); //excel生成pdf文件 $testObj->excelToPdf(); echo 'done';