function genPDF($proj, $local = false) { // create new PDF document $size = array(8.5, 11); $pdf = new TCPDF("l", "in", $size, true, "UTF-8", false); // set document information $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor("EMu Mover"); $pdf->SetTitle("Project Title"); $pdf->SetSubject("Project"); $pdf->SetKeywords("EMu, Project"); $pdf->SetPrintHeader(false); $pdf->SetPrintFooter(false); $pdf->SetMargins(0.5, 0.5, 0.5, false); setSpacing($pdf); // set font $pdf->SetFont("helvetica", "", 10); // add a page $pdf->AddPage(); $pdf->SetAutoPageBreak(TRUE, 0); $html = ""; $css = file_get_contents("report.css"); $style = "<style type=\"text/css\">\n{$css}\n</style>"; $w = $pdf->getPageWidth(); $h = $pdf->getPageHeight(); $m = $pdf->getMargins(); $pdf->setCellPaddings(0.05, 0.1, 0.05, 0); $pdf->setCellHeightRatio(0.4); $w -= $m['left'] + $m['right']; $h -= $m['top'] + $m['bottom']; $sixth = $w / 6; $sixth = floor($sixth * 100.0) / 100.0; $objs = generatePDFcells($proj); addHeaderRow($pdf); //Get row heights $heights = array(); foreach ($objs as $k1 => $obj) { $c = 0; $lh = 0; $y = $pdf->GetY(); foreach ($obj as $k2 => $cell) { $cellWidth = $sixth; $x = $pdf->GetX(); if ($c == 1) { $cellWidth = 3 * $sixth; } $pdf->writeHTMLcell($cellWidth, 0, $x, $y, $style . $cell, 0, 0, 0, 1, '', 1); $c++; $tlh = $pdf->getLastH(); //$objs[$k1][$k2] .= "<p>$tlh</p>"; if ($tlh > $lh) { $lh = $tlh; } } $heights[$k1] = $lh; $pdf->deletePage($pdf->getPage()); $pdf->AddPage(); } addIntro($pdf, $proj); $y = $pdf->GetY(); addHeaderRow($pdf); //Now actually create the PDF foreach ($objs as $k1 => $obj) { $y = $pdf->GetY(); if (isset($heights[$k1])) { if ($y + $heights[$k1] >= $h) { $pdf->AddPage(); addHeaderRow($pdf); } } $c = 0; $lh = 0; $y = $pdf->GetY(); foreach ($obj as $k2 => $cell) { $cellWidth = $sixth; $x = $pdf->GetX(); if ($c == 1) { $cellWidth = 3 * $sixth; } $pdf->writeHTMLcell($cellWidth, $heights[$k1], $x, $y, $style . $cell, 'LTRB', 0, 0, 1, '', 1); $c++; } $pdf->SetY($y + $heights[$k1]); } if ($proj) { $info = getProjectInfo($proj); if ($local) { $fname = $info['title'] . ".pdf"; $pdfpath = sys_get_temp_dir() . "/" . $info['title'] . "--" . date("Y-m-d") . ".pdf"; $pdf->Output($pdfpath, "F"); return $pdfpath; } else { header("Content-type: application/pdf"); $val = $pdf->Output($info['title'] . "-" . date("Y-m-d") . ".pdf", "I"); print $val; } return true; exit; } else { print "{$style}"; var_dump($objs); return false; } }
public function exportAction() { $isAdmin = in_array(\UserManagement\Constants::GROUP_ADMINISTRATOR, identity()->getGroups()); $maxRange = $isAdmin ? 2 : 1; function addHeaderRow($excelActiveSheet, $index, $value = '', $size = 10, $bold = true, $horizontal = null) { if ($horizontal == null) { $horizontal = \PHPExcel_Style_Alignment::HORIZONTAL_CENTER; } $parts = explode(':', $index); $excelActiveSheet->setCellValue($parts[0], $value); if (isset($parts[1])) { $excelActiveSheet->mergeCells($index); } $excelActiveSheet->getStyle($index)->getFont()->setBold($bold); $excelActiveSheet->getStyle($index)->getFont()->setSize($size); $excelActiveSheet->getStyle($index)->getAlignment()->setHorizontal($horizontal); $excelActiveSheet->getStyle($index)->getAlignment()->setWrapText(true); } $brands = $this->brandsAction(); if ($brands instanceof Response || empty($brands['brands'])) { return new Response\HtmlResponse(); } $phpExcel = new \PHPExcel(); $phpExcel->setActiveSheetIndex(0); $sheet = $phpExcel->getActiveSheet(); $rowIndex = '1'; $chars = range('A', 'Z'); $countTypes = count($brands['types']); $count = $countTypes * $maxRange + 1; addHeaderRow($sheet, 'A' . $rowIndex . ':' . $chars[$count - 1] . $rowIndex, $brands['form']->brandId->getValue(), 30); $rowIndex++; foreach ($brands['continents'] as $continentId => $continent) { $cellIndex = 'A'; addHeaderRow($sheet, $cellIndex . $rowIndex . ':' . $chars[$count - 1] . $rowIndex, $continent, 14); $rowIndex++; addHeaderRow($sheet, $cellIndex . $rowIndex, 'Държава', 12); $cellIndex++; addHeaderRow($sheet, $cellIndex . $rowIndex . ':' . $chars[array_search($cellIndex, $chars) + $countTypes - 1] . $rowIndex, 'Статус', 12); if ($isAdmin) { $cellIndex = $chars[array_search($cellIndex, $chars) + $countTypes - 1]; $cellIndex++; addHeaderRow($sheet, $cellIndex . $rowIndex . ':' . $chars[array_search($cellIndex, $chars) + $countTypes - 1] . $rowIndex, 'Предприети действия', 12); } $rowIndex++; $cellIndex = 'A'; addHeaderRow($sheet, $cellIndex . $rowIndex, "Общо държави: " . (isset($brands['countries'][$continentId]) ? count($brands['countries'][$continentId]) : 0) . "\nОбщо население: " . (isset($brands['populations'][$continentId]) ? number_format($brands['populations'][$continentId], 0, ".", " ") : 0), 10, true, \PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $sheet->getStyle($cellIndex . $rowIndex)->getAlignment()->setWrapText(true); $sheet->getColumnDimension($cellIndex)->setAutoSize(true); $cellIndex++; foreach (range(1, $maxRange) as $i) { foreach ($brands['types'] as $type) { addHeaderRow($sheet, $cellIndex . $rowIndex, $type); $sheet->getStyle($cellIndex . $rowIndex)->getAlignment()->setWrapText(true); $sheet->getColumnDimension($cellIndex)->setWidth(20); $cellIndex++; } } $rowIndex++; if (isset($brands['countries'][$continentId])) { foreach ($brands['countries'][$continentId] as $countryId => $country) { $cellIndex = 'A'; $cellValue = $country['ISO3166Code'] . ' ' . $country['name'] . "\nНаселение: " . number_format($country['population'], 0, ".", " "); $totalPrice = 0; $brandEntity = null; foreach ($brands['types'] as $typeId => $type) { if (isset($brands['brands'][$countryId][$typeId])) { $brandEntity = $brands['brands'][$countryId][$typeId]; $totalPrice += $brandEntity->getPrice(); } } if ($brandEntity && $totalPrice > 0) { $cellValue .= "\nОбща цена: " . $brandEntity->getFormatedPrice($totalPrice, $brands['currentCurrency']); } $sheet->setCellValue($cellIndex . $rowIndex, $cellValue); $sheet->getStyle($cellIndex . $rowIndex)->getAlignment()->setWrapText(true); $cellIndex++; foreach ($brands['types'] as $typeId => $type) { $cellValue = ""; if (isset($brands['brands'][$countryId][$typeId])) { $brandEntity = $brands['brands'][$countryId][$typeId]; if (isset($brands['statuses'][$brandEntity['statusId']])) { $cellValue .= $brands['statuses'][$brandEntity['statusId']]; $date = $brandEntity['statusDate']; if ($date) { $date = new \DateTime($date); $cellValue .= "\n" . $date->format('d.m.Y'); } if ($brandEntity->getPrice()) { $cellValue .= "\nЦена: " . $brandEntity->getFormatedPrice(null, $brands['currentCurrency']); } if ($brandEntity->getStatusNote()) { $cellValue .= "\nКоментар: " . $brandEntity->getStatusNote(); } } } $sheet->setCellValue($cellIndex . $rowIndex, $cellValue); $sheet->getStyle($cellIndex . $rowIndex)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $sheet->getStyle($cellIndex . $rowIndex)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_TOP); $sheet->getStyle($cellIndex . $rowIndex)->getAlignment()->setWrapText(true); /* if (isset($brands['brands'][$countryId][$typeId])) { $brandEntity = $brands['brands'][$countryId][$typeId]; $color = (isset($brands['statusesColors'][$brandEntity['statusId']]) ? $brands['statusesColors'][$brandEntity['statusId']] : '#FFFFFF'); if ($color) { if ($color[0] == '#') { $color = array('rgb' => substr($color, 1)); } else { $color = array('argb' => $color); } $sheet->getStyle($cellIndex . $rowIndex)->getFill()->applyFromArray( array( 'type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => $color ) ); } } */ $cellIndex++; } if ($isAdmin) { foreach ($brands['types'] as $typeId => $type) { $cellValue = ""; if (isset($brands['brands'][$countryId][$typeId])) { $cellValue .= $brands['brands'][$countryId][$typeId]['description']; } $sheet->setCellValue($cellIndex . $rowIndex, strip_tags(str_replace(array("<br />", "<br>"), "\n", $cellValue))); $sheet->getStyle($cellIndex . $rowIndex)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_TOP); $sheet->getStyle($cellIndex . $rowIndex)->getAlignment()->setWrapText(true); $cellIndex++; } } $rowIndex++; } } $rowIndex++; } $sheet->setSelectedCell('A1'); $writer = \PHPExcel_IOFactory::createWriter($phpExcel, 'Excel5'); $writer->save('data/brand.xls'); return new FileResponse('data/brand.xls'); }