phpexcel save pdf using tcpdf having pdf width issue - php

i am using php excel tcpdf render to save file as pdf. pdf can save successfully but pdf only saving as A4 size and it cuts my columns. i have many columns so i need all columns on pdf. pdf width should be auto so it can generate pdf as per number of columns given. please have a look below is my code i tried. also i included image i taken from pdf generated. you can see my pdf width is fix so my content is cut. any suggestion
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
$rendererLibrary = 'tcpdf';
$rendererLibraryPath = '' . $rendererLibrary;
//echo $rendererLibraryPath;
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("PDF Test Document")
->setSubject("PDF Test Document")
->setDescription("Test document for PDF, generated using PHP classes.")
->setKeywords("pdf php")
->setCategory("Test result file");
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');
$objPHPExcel->getActiveSheet()->setShowGridLines(false);
//If used external excel file
$inputFile="xlsx_file/subcategory.xlsx";
$inputFileType = PHPExcel_IOFactory::identify($inputFile);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFile);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
{
$worksheet->setShowGridLines(true);
$worksheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A2_PAPER );
$worksheet->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A2_PAPER );
$worksheet->getPageSetup()->setFitToPage(true);
$worksheet->getPageSetup()->setFitToWidth(1);
$worksheet->getPageSetup()->setFitToHeight(0);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
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'
);
}
$rand = rand(1234, 9898);
$presentDate = date('YmdHis');
$fileName = "report_" . $rand . "_" . $presentDate . ".pdf";
// Redirect output to a client’s web browser (PDF)
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="'.$fileName.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save('php://output');
exit;

Try setting the portrait visualization:
$objPHPExcel->getActiveSheet()
->getPageSetup()
->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);

I was having the same problem and I had to deactivate the autoSize for the columns and set the width manually, like this:
$objPHPExcel ->getActiveSheet()->getColumnDimension('A')->setAutoSize(false);
$objPHPExcel ->getActiveSheet()->getColumnDimension('B')->setAutoSize(false);
$objPHPExcel ->getActiveSheet()->getColumnDimension('C')->setAutoSize(false);
$objPHPExcel ->getActiveSheet()->getColumnDimension('D')->setAutoSize(false);
$objPHPExcel ->getActiveSheet()->getColumnDimension('E')->setAutoSize(false);
$objPHPExcel ->getActiveSheet()->getColumnDimension('F')->setAutoSize(false);
$objPHPExcel ->getActiveSheet()->getColumnDimension('G')->setAutoSize(false);
$objPHPExcel ->getActiveSheet()->getColumnDimension('A')->setWidth(20);
$objPHPExcel ->getActiveSheet()->getColumnDimension('B')->setWidth(20);
$objPHPExcel ->getActiveSheet()->getColumnDimension('C')->setWidth(14);
$objPHPExcel ->getActiveSheet()->getColumnDimension('D')->setWidth(15);
$objPHPExcel ->getActiveSheet()->getColumnDimension('E')->setWidth(14);
$objPHPExcel ->getActiveSheet()->getColumnDimension('F')->setWidth(18);
$objPHPExcel ->getActiveSheet()->getColumnDimension('G')->setWidth(9);
After doing that, my pdf was printed like this:
The answer might be a little late, but I hope it helps you in the future.

Related

How to fix 'Unable to load PDF Rendering library' Export Excel to PDF using PHPExcel TCPDF

I want to export my Excel to PDF file using TCPDF in PHPExcel and save it to the path, but I found this error 'Unable to load PDF Rendering library'. Please help, how to fix this?
I'm not sure this is because of rendererLibraryPath
$excel2 = PHPExcel_IOFactory::createReader('Excel2007');
$excel2 = $excel2->load('./template/invoice.xlsx'); // Empty Sheet
$excel2->setActiveSheetIndex(0);
$excel2->getActiveSheet()->setCellValue('C6', $row['nama'])
->setCellValue('C7', $row['idpel'].$row['periode_tagihan'])
->setCellValue('H6', $row['idpel'])
->setCellValue('C6', $row['nama'])
->setCellValue('H7', $row['rekening_bulan'])
->setCellValue('H8', $row['tarif'])
->setCellValue('J8', $row['daya'])
->setCellValue('E12', $row['rpptl'])
->setCellValue('E13', $row['rpbpju'])
->setCellValue('E14', $row['rpppn'])
->setCellValue('E15', $row['rpmat'])
->setCellValue('E16', $row['tagsus'])
->setCellValue('E17', $row['rpbk'])
->setCellValue('E19', $row['rptottag'])
->setCellValue('C22', $terbilang)
->setCellValue('E25', $row['batas_akhir_bayar'])
->setCellValue('H33', date('d F Y'));
//$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007');
//$objWriter->save("./template/cetak/".$row['idpel'].$row['periode_tagihan'].".xlsx");
$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
$rendererLibrary = 'tcpdf';
$rendererLibraryPath = dirname(__FILE__) . '/' . $rendererLibrary;
PHPExcel_Settings::setPdfRenderer($rendererName, $rendererLibraryPath);
$path = "./template/cetak/".$row['idpel'].$row['periode_tagihan'].".pdf";
$objWriter = new PHPExcel_Writer_PDF($excel2);
$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'PDF');
$objWriter->setSheetIndex(0);
$objWriter->save($path);
* Remove TCPDF folder from vendor and install again with php composer.phar *
Hope this link help to find your solution

Post/send values to cells in a Excel file and extract result from specific cell?

I have a Excel file stored on my webserver which i use as a calculator. I was wondering if it's any way possible to post/send values to specific cells and then extract the result from a specific cell? I would prefer to do this by PHP.
Use phpExcel, here's an example to open and modify your existing file:
<?php
error_reporting(E_ALL);
set_time_limit(0);
date_default_timezone_set('Europe/Lisbon');
set_include_path(get_include_path() . PATH_SEPARATOR . './Classes/');
include 'PHPExcel/IOFactory.php';
$fileType = 'Excel5'; //you may have to change this value
$fileName = 'yourfile.xlsm';
// Read the file
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($fileName);
// Change the file
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B1', 'World!');
// Write the file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
$objWriter->save($fileName);
Please refer to the official documentation and list of examples.

How can I convert excel file to pdf using TCPDF?

I need to generate a report with graphs in excel to pdf. I tried using phpexcel but it fails to load the charts. It only generates PDF with tables. Here's my code in phpexcel.
$newpdf = "report-".date('m_d_Y_H_i_s', strtotime('now')).".pdf";
$objPHPExcel1 = new PHPExcel();
$objPHPExcel1 = PHPExcel_IOFactory::load('csv/'.$filename);
$objPHPExcel1->getActiveSheet(0)->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
$objPHPExcel1->getActiveSheet(0)->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
$objPHPExcel1->getActiveSheet(0)->getPageSetup()->setFitToWidth(true);
$objPHPExcel1->getActiveSheet(0)->getPageSetup()->setFitToHeight(true);
$objPHPExcel1->getActiveSheet()->setShowGridLines(false);
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="'.$newpdf.'"');
header('Cache-Control: max-age=0'); //no cache
$objWriter1 = new PHPExcel_Writer_PDF($objPHPExcel1);
$objWriter1->save('php://output');
exit;
I also tried loading the contents from HTML using jchartfx for graphs without success. Any idea?
Thanks in advance.
PHPExcel can only read charts from Excel2007 (OfficeOpenXML .xlsx) files, and will only load them if you explicitly tell it to do so.
if (PHPExcel_IOFactory::identify('csv/'.$filename) == "Excel2007") {
$objReader = PHPExcel_IOFactory::createReader("Excel2007");
$objReader->setIncludeCharts(TRUE);
} else {
die("Can't load charts");
}
$objPHPExcel = $objReader->load('csv/'.$filename);
When writing to a PDF file, you must have the relevant library installed (tcpdf in your case) and tell PHPExcel that you want to use it and the directory where tcpdf is installed.
$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
$rendererLibrary = 'tcPDF5.9';
$rendererLibraryPath = '/php/libraries/PDF/' . $rendererLibrary;
if (!PHPExcel_Settings::setPdfRenderer(
$rendererName,
$rendererLibraryPath
)) {
die('NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
EOL .
'at the top of this script as appropriate for your directory structure'
);
}
You also need jpgraph installed, and again PHPExcel needs to know where it is installed
$rendererName = PHPExcel_Settings::CHART_RENDERER_JPGRAPH;
$rendererLibrary = 'jpgraph3.5.0b1/src';
$rendererLibraryPath = '/php/libraries/Charts/' . $rendererLibrary;
if (!PHPExcel_Settings::setChartRenderer(
$rendererName,
$rendererLibraryPath
)) {
die('NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
PHP_EOL .
'at the top of this script as appropriate for your directory structure'
);
}
Finally, you need to tell the PDF Writer explicitly to generate the charts
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save($outputFileName);

can't read and write xlsx file php

this is my code i want to attach this file and send it and the values are numerical variables that in excel file i use them to drow a chart
it dosen't work at all ,
my boss is mad at me , Help
let me explain more . i have to attach an excel file {which contains 4 numbers that are a test result and draw a chart } I've done the test i have the result, i have done sending with attachment but i can't make the file .
require_once '../Classes/PHPExcel.php';
$fileType = 'Excel2007';
$fileName = 'Result.xlsx';
// Read the file
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($fileName);
// Change the file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
$objSheet = $objPHPExcel->setActiveSheetIndex(0);
objSheet->getCell('A2')->setValue($SumY );
objSheet->getCell('B2')->setValue($SumR );
objSheet->getCell('C2')->setValue($SumB );
objSheet->getCell('D2')->setValue($SumG );
// Write the file
$objWriter->save('Result.xlsx');
Tell PHPExcel that you want to include charts when reading and writing
Assuming that the chart is defined in your template
require_once '../Classes/PHPExcel.php';
$fileType = 'Excel2007';
$fileName = 'Result.xlsx';
// Read the file (including chart template)
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objReader->setIncludeCharts(TRUE);
$objPHPExcel = $objReader->load($fileName);
// Change the file
$objSheet = $objPHPExcel->setActiveSheetIndex(0);
$objSheet->getCell('A2')->setValue($SumY );
$objSheet->getCell('B2')->setValue($SumR );
$objSheet->getCell('C2')->setValue($SumB );
$objSheet->getCell('D2')->setValue($SumG );
// Write the file (including chart)
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
$objWriter->setIncludeCharts(TRUE);
$objWriter->save('Result.xlsx');
If the chart isn't defined in your template, then you need to create it in your code

PHPExcel converting multiple sheet of xlsx to csv

im using this few lines to convert an xlsx, which contain 4sheets to convert to .csv . but it only convert the first sheet of the xlsx file. how can i make it to convert every sheets in the xlsx. heres the code,
error_reporting(E_ALL);
date_default_timezone_set($this->vendor_timezone);
/** PHPExcel_IOFactory */
require_once sfConfig::get('sf_root_dir').'/lib/PHPExcel/IOFactory.php';
$file=sfConfig::get("sf_upload_dir").DIRECTORY_SEPARATOR."temp".DIRECTORY_SEPARATOR."1344500254_MyExcel.xlsx";
// Check prerequisites
//print sfConfig::get("sf_upload_dir").DIRECTORY_SEPARATOR."temp".DIRECTORY_SEPARATOR; exit;
if (!file_exists($file)) {
exit($file."Please run 06largescale.php first.\n");
}
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($file);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->save(str_replace('.xlsx', '.csv',$file));
return "success";
Please check the tutorial here which has step by step code to achieve this
<?php
require_once 'PHPExcel/PHPExcel/IOFactory.php';
$excel = PHPExcel_IOFactory::load("test123.xlsx");
$writer = PHPExcel_IOFactory::createWriter($excel, 'CSV');
$writer->setDelimiter(";");
$writer->setEnclosure("");
$writer->save("test123.csv");
?>
To convert all sheets you must iterate through all worksheets and set it as the active sheet and then write that to a file.
$inFile = 'fileWithMultipleWorksheet.xlsx';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($inFile);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$index = 0;
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$objPHPExcel->setActiveSheetIndex($index);
// write out each worksheet to it's name with CSV extension
$outFile = str_replace(array("-"," "), "_", $worksheet->getTitle()) .".csv";
$objWriter->setSheetIndex($index);
$objWriter->save($outFile);
$index++;
}
Write each sheet in turn to a different file, and then concatenate those files into one: PHPExcel does not provide an option to write multiple sheets to a single CSV file.

Categories