PDF Formatting not working

Topics: Developer Forum
Oct 30, 2012 at 9:27 AM

Here is my code.

 

No errors, PDF is created but not formatted with my settings at all.

 

Really frustrating.

 

Plese help.

 


$fileLocation = $file_directory."certificates/"; $fileName = "Payment Certificate - ".$_GET["invoice"].".xlsx";
error_reporting(E_ALL); ini_set("display_errors", TRUE); ini_set("display_startup_errors", TRUE);
define("EOL",(PHP_SAPI == "cli") ? PHP_EOL : "<br />");
require_once "include/PHPExcel.php"; require_once "include/PHPExcel/IOFactory.php";
// NB -- SPECIFY RENDERING LIBRARY $rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF; $rendererLibrary = 'DomPDF'; $rendererLibraryPath = 'include/PHPExcel/Writer/PDF/'; if (!PHPExcel_Settings::setPdfRenderer( $rendererName, $rendererLibraryPath )) { die('Please set the RENDER NAME and RENDER LIBRARY PATH (serveFile.php LINE: 34)'); }
// LOAD EXCEL FILE $objPHPExcel = PHPExcel_IOFactory::load(preg_replace("/ /","_",$fileLocation.$fileName));
$sheets = $objPHPExcel->getSheetNames();
$objPageSetup = new PHPExcel_Worksheet_PageSetup(); $objPageSetup->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4); $objPageSetup->setFitToPage(true); $objPageSetup->setFitToHeight(1); $objPageSetup->setFitToWidth(1); $objPageSetup->setHorizontalCentered(true); $objPageSetup->setVerticalCentered(true);
foreach ($sheets as $key=>$value) { $objPHPExcel->setActiveSheetIndex(array_search($value, $sheets))->setShowGridlines(false); switch ($value) { case "Tax Invoice": { $objPageSetup->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE); $objPageSetup->setPrintArea("B2:C50"); break; } case "Payment Certificate": { $objPageSetup->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE); $objPageSetup->setPrintArea("B2:L88"); break; } case "Time Based": case "Travel Time": case "Motor Vehicle": case "Car Hire": case "Flights": case "Accommodation": case "Other": case "Toll Gate": case "Prints & Copies": { $objPageSetup->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT); break; } default: break; } $objPHPExcel->getActiveSheet()->setPageSetup($objPageSetup); }
// SAVE PDF FILE $objWriter = new PHPExcel_Writer_PDF($objPHPExcel); $objWriter->setSheetIndex(2); //$objWriter->writeAllSheets(); $objWriter->save(preg_replace("/.xlsx/",".pdf",preg_replace("/ /","_",$fileLocation.$fileName))); echo date('H:i:s') . "- Done writing files -";

Coordinator
Oct 30, 2012 at 1:32 PM

Not all page settings work with PDF output, they're primarily intended to work with the Excel output. This includes setting print areas, alignment within the page, headers/footers, etc. The only settings that should work are paper size and orientation.