Error when i wont to save file as PDF using PHPExcel - php

I'm using the PHPExcel library in my project and its work fine when i wont to save files format (xls/xlsx). But I need to save files as a PDF format and i found some examples but none of them is working for me.
here the code which i use to generate file with pdf format
<?php
error_reporting(E_ALL);
ini_set('include_path', ini_get('include_path').';../Classes/');
include 'PHPExcel/Classes/PHPExcel/IOFactory.php';
$inputFileName = 'file.xls';
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
$objPHPExcel->setActiveSheetIndex(0);
$row = $objPHPExcel->getActiveSheet()->getHighestRow()+1;
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objSheet = $objPHPExcel->getActiveSheet();
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'test test');
$objWriter->save('test100.pdf');
?>
and here what the browser show me
( ! ) Fatal error: Uncaught exception 'PHPExcel_Writer_Exception' with message 'PDF Rendering library has not been defined.' in C:\wamp\www\projet\PHPExcel\Classes\PHPExcel\Writer\PDF.php on line 56
what did i do wrong

PHPExcel doesn't have a PDF Rendering library built-in as part of the code/distribution. Instead, it allows you to use any of 3 different 3rd party PDF libraries (tcPDF, DomPDF or mPDF).
You have to install that PDF Rendering library separately, and then you need to tell PHPExcel which PDF library you have installed before you can save a file as PDF.
This is explained in the PHPExcel documentation and shown in the examples like 21pdf.php

Related

PHPExcel fails on a heavy file import

I am trying to import excel files (xlsx) using PHPExcel library and store the data in a database. Following is my code:
require_once $_SERVER['DOCUMENT_ROOT']."/application/views/subfolder/PHPExcel/Classes/PHPExcel/IOFactory.php";
$file_name = $_FILES['controlname']['tmp_name'];
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$inputFileName = $file_name;
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
$objPHPExcel->setActiveSheetIndex(0);
$sheet = $objPHPExcel->getActiveSheet();
When I import a 1MB+ file having more than 1000 records, the code fails without returning any error with a strange behavior. However when I reupload that file by splitting it into multiple excels such that each excel contains only 1000 records, the same code works successfully.
To test, I tried echoing some message before and after the following line:
$objPHPExcel = $objReader->load($inputFileName);
The message echoed before this line is printed, but the message written after this line does not work.
In order to allow heavy loads I have made settings of ini in MBs as well as GBs, but nothing works.
ini_set("pcre.backtrack_limit", "100000000");
ini_set("max_allowed_packet ", "2G");
ini_set("max_execution_time ", "20000");
ini_set('max_input_time','20000');
ini_set('memory_limit', '2G');
ini_set('upload_max_filesize','20M');
ini_set('post_max_size', '2G');
set_time_limit(0);

how to delete image/ images from excel file using PHPExcel?

Can anybody help to solve this issue, i am new for PHPExcel Libraries,
I want to delete image which is on excel file using PHPExcel.
I did some code here but i am getting error of
Notice: main(): ArrayIterator::next(): Array was modified outside object and internal position
<?php
require_once '../Classes/PHPExcel/IOFactory.php';
require_once '../Classes/PHPExcel.php';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load(files/test.xlsx);
$objPHPExcel->setActiveSheetIndex(1);
$objPHPExcel = $objPHPExcel->getActiveSheet(1)->getDrawingCollection();
foreach ($objPHPExcel as $key => $drawing){
$objPHPExcel->offsetUnset($key);
}
$objReader = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objReader->save('excel-files/new_test.xlsx');
?>

PHPExcel not working with xls file

I am using PHPExcel to read excel file in php,it is working fine with xlsx but when try to read xls file,It shows an error
Fatal error: Call to undefined method PHPExcel_Reader_CSV::setReadDataOnly() in /var/www/....
Here is my code
$file_path='/var/www/html/site/sample.xls';
$inputFileType = PHPExcel_IOFactory::identify( $file_path);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
/** Load $inputFileName to a PHPExcel Object **/
$objPHPExcel = $objReader->load( $file_path);
$total_sheets=$objPHPExcel->getSheetCount();
$allSheetName=$objPHPExcel->getSheetNames();
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
for ($row = 1; $row <= $highestRow;++$row)
{
for ($col = 0; $col <$highestColumnIndex;++$col)
{
$value=$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
$arraydata[$row-1][$col]=trim($value);
}
}
The file that you're loading is identified as a CSV file, even though it has an .xls extension... it is quite a common practise for some developers to save files formatted as csv or with html markup with a .xls extension, but that doesn't make them BIFF-format .xls files.
The setReadDataOnly() method isn't available for the CSV Reader, because a CSV file cannot contain anything other than data.
The most recent versions of PHPExcel provide a stub for setReadDataOnly() in the CSV Reader to prevent an error in this situation, and I'd certainly recommend that you upgrade to the latest code; but if you can't do that, then the simplest fix for you is simply to wrap the call to setReadDataOnly() in an if test:
$inputFileType = PHPExcel_IOFactory::identify( $file_path);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
if ($inputFileType !== 'CSV') {
$objReader->setReadDataOnly(true);
}
For binary Excel files (xls) you may have better luck using the (old) PHP-ExcelReader. It's the most reliable one I've found so far.

PHPExcel function getHighestRow() is NOT working for .xls but working for .xlsx

I am using PHPExcel to read its content based on no.of rows present.
To find no.of rows I am using following function.
$objSheet->getHighestRow();
It is working fine for .xlsx files.
But it is NOT working in .xls.
Fatal error: Call to a member function getHighestRow() on a non-object
So how i can get no.of rows of excel ?
PHP Code:
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
/** PHPExcel_IOFactory */
include 'www/PHPExcelReader/Classes/PHPExcel/IOFactory.php';
include 'www/PHPExcelReader/Classes/PHPExcel/Classes/PHPExcel.php';
try {
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_sqlite3;
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setReadDataOnly(true);
$objReader->setLoadSheetsOnly( array("Sheet1") );
$objPHPExcel = $objReader->load($inputFileName);
$objSheet = $objPHPExcel->getActiveSheet();
$no_of_rows=$objSheet->getHighestRow();
//echo "no_of_rows=$no_of_rows";
I want both xls & .xlsx to be supported... Please guide me.
I solved this problem myself.
It is due excel version incompatibility.
Here is the code to resolve this issue.
/** Identify the type of $inputFileName **/
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
/** Create a new Reader of the type that has been identified **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);

PHPExcel - read 4 CSV files into a single workbook with 4 spreedsheets

I need to aggregate 4 CSV files into a single, Excel workbook using PHPExcel.
Working on a single CSV file and a mono-spreadsheet workbook all works fine.
Using more than one CSV, I'm unable to get each CSV file into a seperate sheet.
How can I achieve this using PHPExcel?
There is an example of this in the Documentation/Examples/Readers directory in the SVN repository for PHPEXcel: It's Example #13
include 'PHPExcel/IOFactory.php';
$inputFileType = 'CSV';
$inputFileNames = array('./example1.csv','./example2.csv','./example3.csv','./example4.csv');
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$inputFileName = array_shift($inputFileNames);
$objPHPExcel = $objReader->load($inputFileName);
$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));
foreach($inputFileNames as $sheet => $inputFileName) {
$objReader->setSheetIndex($sheet+1);
$objReader->loadIntoExisting($inputFileName,$objPHPExcel);
$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));
}
$loadedSheetNames = $objPHPExcel->getSheetNames();
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$objPHPExcel->setActiveSheetIndexByName($loadedSheetName);
echo $loadedSheetName,PHP_EOL;
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData);
echo PHP_EOL;
}
You didn't specify exactly where the problem is...
For multiple worksheet excel file proceed as following:
Load CSV file
Create new worksheet
Write CSV to new worksheet
Go to 1
From docs:
If you need to create more worksheets in the workbook, here is how:
$objWorksheet1 = $objPHPExcel->createSheet();
$objWorksheet1->setTitle('Another sheet');
To set active worksheet:
$objWorksheet1->setActiveSheetIndex($index);

Categories