function render(App_Grid_Filter $filter, $exportLayout, $page, $fileName) { $pdf = new mypdf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->caption = $this->caption; // set document information $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor('Nicola Asuni'); $pdf->SetTitle('TCPDF Example 048'); $pdf->SetSubject('TCPDF Tutorial'); $pdf->SetKeywords('TCPDF, PDF, example, test, guide'); // set default header data //$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 048', PDF_HEADER_STRING); // set header and footer fonts //$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); //$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); // set default monospaced font $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); //set margins $pdf->SetMargins(2, 0, 5); //$pdf->SetHeaderMargin(2); //$pdf->SetFooterMargin(PDF_MARGIN_FOOTER); //set auto page breaks $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); //set image scale factor $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); //set some language-dependent strings //$pdf->setLanguageArray($l); // --------------------------------------------------------- // set font $pdf->SetFont('helvetica', 'B', 20); // add a page // ----------------------------------------------------------------------------- // ----------------------------------------------------------------------------- $totalpage = $this->filter()->getTotalPage(); if ($page == 'all') { $startPage = 1; $endPage = $totalpage; } elseif (is_array($page)) { $startPage = (int) $page['0']; $endPage = (int) $page['1']; } elseif (is_numeric($page)) { $startPage = $endPage = (int) $page; } if ($startPage < 0) { $startPage = 1; } if ($endPage == '') { $endPage = $startPage; } if ($endPage > $totalpage) { $endPage = $totalpage; } for ($i = $startPage; $i <= $endPage; $i++) { $pdf->AddPage($exportLayout); $pdf->SetFont('freeserif', '', 7); $pdf->SetX(5); $pdf->SetY(10); $filter->setPage($i); $list = $filter->getList(); $tbl = $this->renderSimpleTable($filter, $list); $pdf->writeHTML($tbl, true, true, false, false, ''); } //Close and output PDF document if ('' == $fileName) { $fileName = uniqid(); } $filePath = App_Env::getPathOfUserTemporaryFolder() . "/" . $fileName . '.pdf'; $pdf->Output($filePath, 'F'); $pdf->Output(null, 'I'); //============================================================+ // END OF FILE //============================================================+ }
function render($RecordSet, $titleArray, $fields, $fileName) { echo <<<STYLE <style> table.excel { \tborder-style:ridge; \tborder-width:1; \tborder-collapse:collapse; \tfont-family:sans-serif; \tfont-size:12px; } table.excel thead th, table.excel tbody th { \tbackground:#CCCCCC; \tborder-style:ridge; \tborder-width:1; \ttext-align: center; \tvertical-align:bottom; } table.excel tbody th { \ttext-align:center; \twidth:20px; } table.excel tbody td { \tvertical-align:bottom; } table.excel tbody td { padding: 0 3px; \tborder: 1px solid #EEEEEE; } table.excel tr:hover { \tcolor: #09F; \tbackground-color: #D6D6D6; } </style> STYLE; try { error_reporting(E_ALL ^ E_NOTICE); ini_set('display_errors', 0); if ('' == $fileName) { $fileName = uniqid(); } $filePath = App_Env::getPathOfUserTemporaryFolder() . "/" . $fileName . '.xls'; $workbook = new Excel_Writer($filePath); $workbook->setLandscape(); $workbook->setTitle($this->filter()->getCaptionText()); // query $i = 0; $dataSet = array(); foreach ($RecordSet as $rows) { //array_unique ( $row ); $j = 0; foreach ($fields as $filed) { $dataSet[$i][$j++] = $this->filter()->fieldTranform($rows, $filed); } ++$i; } // set data $workbook->setData($dataSet); $lastcol = $j - 1; $columnConfig = $this->_columnConfig; $widths = array(); $tableWidth = $this->filter()->getTableWidth(); if (false != strpos($tableWidth, "%")) { $tableWidth = "100"; } //echo $tableWidth; $counter_blank_cell = 0; $total_cell_width = 0; foreach ($fields as $filed) { $width = $columnConfig->fields[$filed]->width; if (false != strpos($width, "%")) { $width = str_replace("%", "", $width); $w = $width * $tableWidth / 100; } else { $w = str_replace("px", "", $width); } if ($w == '') { $counter_blank_cell += 1; } $total_cell_width += $w; $widths[] = $w; } if ($counter_blank_cell == 0) { $counter_blank_cell = 1; } for ($i = 0; $i < count($widths); $i++) { if ($widths[$i] == '') { $widths[$i] = ($tableWidth - $total_cell_width) / $counter_blank_cell; } } $format = array('right' => 1, 'left' => 1, 'border' => 1); $i = 0; foreach ($fields as $filed) { $align = in_array($columnConfig->fields[$filed]->align, App_Model_Config::$alignList) ? $columnConfig->fields[$filed]->align : 'left'; $workbook->setColumn($i, $i, $widths[$i], array_merge($format, array('align' => $align))); ++$i; } //$workbook->setColumn(0,0,15 ,array_merge($format,array( 'align'=>'center')) ); //$workbook->setColumn(1,1,35,array_merge($format,array('align'=>'left'))); //$workbook->setColumn(2,4,20,array_merge($format,array('align'=>'center','NumFormat'=>'_-* #,##0.00_-'))); //$workbook->setColumn(5,$lastcol,10,array_merge($format,array('align'=>'right','NumFormat'=>'_-* #,##0.00_-'))); //set header for ($i = 0; $i < count($titleArray); $i++) { $titleArray[$i] = ucfirst(App_Util::splitByCaps($this->_translate($titleArray[$i]))); } $header[0] = $titleArray; //$header[0] = array('เธฃเธซเธฑเธชเธงเธดเธ—เธขเธฒเธฅเธฑเธข','เธงเธดเธ—เธขเธฒเธฅเธฑเธขเธ�เธธเธกเธ�เธ�',"เธงเธ�เน€เธ�เธดเธ�เธ—เธตเน�เน�เธ”เน�เธฃเธฑเธ� \n เธ�เธฑเธ”เธชเธฃเธฃ","เธฃเธงเธกเน€เธ�เธดเธ� \nเธ�เธ�เธ�เธฃเธฐเธกเธฒเธ“เธ—เธตเน� \nเน�เธ”เน�เธฃเธฑเธ�เน�เธญเธ�","เธฃเน�เธญเธขเธฅเธฐ\n เธ•เน�เธญ \nเธงเธ�เน€เธ�เธดเธ�เธ—เธตเน� เน�เธ”เน�เธฃเธฑเธ�เธ�เธฑเธ”เธชเธฃเธฃ",'เธ�เธณเธ�เธงเธ�เน€เธ�เธดเธ�เธ—เธตเน�เน�เธ”เน�เธฃเธฑเธ�เน�เธญเธ�'); //$header[1] = array('','','','',''); //for($i=1;$i<=$lastcol-4;$i++){ /// $header[0][] =''; // $header[1][] = 'เธ�เธฃเธฑเน�เธ�เธ—เธตเน� '.$i ; //} //array_pop( $header[0]); $workbook->setHeader($header); //$first_row , $first_col , $last_row , $last_col $firstRow = $workbook->getFirstRow(); ///echo $lastcol; //$workbook->mergeCells($firstRow,0,$firstRow+1,0); //$workbook->mergeCells($firstRow,1,$firstRow+1,1); //$workbook->mergeCells($firstRow,2,$firstRow+1,2); //$workbook->mergeCells($firstRow,3,$firstRow+1,3); //$workbook->mergeCells($firstRow,4,$firstRow+1,4); //$workbook->mergeCells($firstRow,5,$firstRow,$lastcol); //addLastRow; //$workbook->setFormula ('lastrow',0,0,'' ); //$workbook->setFormula ('lastrow',1,1,'เธฃเธงเธกเธ—เธธเธ�เธงเธดเธ—เธขเธฒเธฅเธฑเธข' ); //$workbook->setFormula ('lastrow',2,3,'SUMCOLUMN'); //$workbook->setFormula ('lastrow',4,4,'=(D[row]/C[row])*100'); //$workbook->setFormula ('lastrow',5,$lastcol,'SUMCOLUMN'); echo $workbook->render(); } catch (Exception $e) { echo $e->getCode(), 'error : ', $e->getMessage(); echo $e->getTraceAsString(); } }
function render($RecordSet, $titleArray, $fields, $exportFileName) { /* Data to be inserted into excel in an array of arrays * Each array is the avg monthly temp of a different city */ //$data = $list; //$csvTitle = $this->_filetitle; /* We know the keys of each sub-array are the same, so * extract them from the first sub-array and set them * to be our column titles */ //$titleArray = $this->_headers; /* Set your desired delimiter. You can make this a true * .csv and set $delimiter = ","; but I find that tabs * work better as commas can also be present in your data. * Note that you must use the .tsv or .xls file extension for Excel * to correctly interpret tabs. Otherwise if you are using commas * for your delimiter, use .csv for your file extension. */ $delimiter = ","; //Set target filename - see above comment on file extension. //Send headers ..header("Content-Type: text/plain"); header('Content-type: text/html; charset=utf-8'); //print the title to the first cell //print $csvTitle . "\r\n"; // $fields = array (); //foreach ( $grid->ExtFields as $obt ) { // $titleArray [] = lcfirst ( $obt ['field'] ); //} //Separate each column title name with the delimiter $titleString = implode($delimiter, $titleArray); $preview = "<table border='1' width='100%'>"; $preview .= "<tr>"; foreach ($titleArray as $title) { $preview .= "<td>{$title}</td>"; } $preview .= "</tr>"; ob_start(); echo $titleString . "\r\n"; //Loop through each subarray, which are our data sets //$RecordSet = $grid->query (); $dataRowString = array(); foreach ($RecordSet as $row) { $subArray = array(); $preview .= "<tr>"; foreach ($fields as $filed) { $text = $row->{$filed}; $preview .= "<td>{$text}</td>"; $text = iconv("UTF-8", "TIS-620", $text); $subArray[] = "\"{$text}\""; } $preview .= "</tr>"; $dataRowString = implode($delimiter, $subArray); echo $dataRowString . "\r\n"; } $preview .= "</table>"; $contents = ob_get_contents(); ob_clean(); if ('' == $exportFileName) { $exportFileName = uniqid(); } $filePath = App_Env::getPathOfUserTemporaryFolder() . "/" . $exportFileName . '.csv'; $objFopen = fopen($filePath, 'w'); fwrite($objFopen, $contents); fclose($objFopen); echo $preview; }
function printAction() { $this->_helper->layout()->disableLayout(); $data = $this->_setupPdfData(); $filename = App_Env::getPathOfUserTemporaryFolder() . '/print-' . uniqid() . '.pdf'; $pdf = new $this->_pdfClass($filename, $data); $pdf->Output($filePath, 'F'); $pdf->Output(null, 'I'); $this->view->downloadfile = $filename; $this->render('pdf', null, true); }