I get error:
Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Exception: Column
string index can not be longer than 3 characters in
/var/www/html/domain.com/phpspreadsheet/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/Coordinate.php:305
Stack trace: #0
/var/www/html/domain.com/phpspreadsheet/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Worksheet/Worksheet.php(1430):
PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString() #1
/var/www/html/domain.com/exceltests.php(393):
PhpOffice\PhpSpreadsheet\Worksheet\Worksheet->getColumnDimension() #2
{main} thrown in
/var/www/html/domain.com/phpspreadsheet/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/Coordinate.php
on line 305
And this is the line #393 in my file:
$spreadsheet->getActiveSheet()->getColumnDimension("B$i")->setWidth(90);
EDIT More code:
$blob = returnItemImageOnly($pdo, $row["item"]);
if($blob){
$spreadsheet->getActiveSheet()->getColumnDimension("B$i")->setWidth(90);
$spreadsheet->getActiveSheet()->getRowDimension("$i")->setRowHeight(80);
$type = $blob['type'];
$image = base64_encode($blob['image']);
$imgdata = base64_decode($image);
$mimetype = getImageMimeType($imgdata);
$fname = "tmp/".rand(). ".".$mimetype;
$forUnlink[] = $fname;
file_put_contents($fname, $imgdata);
$drawing = new PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setPath($fname);
$drawing->setCoordinates("B$i");
$drawing->setResizeProportional(false);
$drawing->setWidth(75);
$drawing->setHeight(75);
$drawing->setOffsetX(34);
$drawing->setOffsetY(15);
$drawing->setWorksheet($sheet);
}
$spreadsheet->getActiveSheet()->mergeCells("B$i:C$i");
I searched for possible issues but no luck!
Maybe someone had the same issue?
Thanks in advance!
Related
Files are submitted by user, so I dont know how many of them will there be and what would be their name.
<?php
echo "<ul>";
$files = scandir('docs/');
foreach($files as $file) {
echo "<li>$file[name]</li>";
}
echo "</ul>";
#
This is what I have now, but it shows "Fatal error: Uncaught TypeError: Cannot access offset of type string on string in D:\xampp\htdocs\zwa\cvic3.php:57 Stack trace: #0 {main} thrown in D:\xampp\htdocs\zwa\cvic3.php on line 57 (echo "<li>$file[name]</li>";)"
You have a mistake in second echo, look closer at quotes;
$filename = $file['name'];
echo "<li>$filename</li>";
Hi am using PhpSpreadsheet, and here is my code:
switch ($file_extension) {
case 'xlsx':
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
break;
case 'xls':
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
break;
default:
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
break;
}
$spreadsheet = $reader->load($_FILES['file']['tmp_name']);
Now I am uploading excelsheet using Ajax, from two source -
From Local Device: var fileName = $(this).find('input[type=file]').prop('files')[0]; //with tmp_name = C:\xampp\tmp\php742D.tmp
From Google Drive: var fileName = new File([res.body], doc.name, { type: doc.mimeType }) //with tmp_name = C:\xampp\tmp\php1F2A.tmp
So, when my php code executing $spreadsheet = $reader->load($_FILES['file']['tmp_name']) this line giving fatal error:
Fatal error: Uncaught
PhpOffice\PhpSpreadsheet\Reader\Exception: Could not find zip member
zip://C:\xampp\tmp\php2854.tmp#_rels/.rels in
C:\xampp\htdocs\myWebapp\GET-OCR\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Shared\File.php:159
Stack trace:
#0 C:\xampp\htdocs\myWebapp\GET-OCR\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Reader\Xlsx.php(393):
PhpOffice\PhpSpreadsheet\Shared\File::assertFile('C:\xampp\tmp\ph...',
'_rels/.rels')
#1 C:\xampp\htdocs\myWebapp\GET-OCR\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Reader\BaseReader.php(166):
PhpOffice\PhpSpreadsheet\Reader\Xlsx->loadSpreadsheetFromFile('C:\xampp\tmp\ph...')
#2 C:\xampp\htdocs\myWebapp\GET-OCR\controller\ajax-req.php(81): PhpOffice\PhpSpreadsheet\Reader\BaseReader->load('C:\xampp\tmp\ph...')
#3 {main} thrown in C:\xampp\htdocs\myWebapp\GET-OCR\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Shared\File.php
on line 159
I checked https://stackoverflow.com/a/50476471/3340665 & PhpSpreadsheet: Permissions | ZipArchive::close(): Failure to create temporary file But no help.
I am working on a project in which i need to import data form a excel(.xlsx) fileinto a table in database. I already write code for a controller upload.php
$this->load->library('excel');
ini_set('memory_limit', '-1');
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$path = $_SERVER['DOCUMENT_ROOT'] . '/bitcoinreports/uploads/data.xlsx';
$objPHPExcel=$objReader->load($path);
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
//loop from first data until last data
for($i=2; $i<=77; $i++){
$name = $objWorksheet->getCellByColumnAndRow(0,$i)->getValue();
$email = $objWorksheet->getCellByColumnAndRow(1,$i)->getValue();
$phone = $objWorksheet->getCellByColumnAndRow(2,$i)->getValue();
$details = $objWorksheet->getCellByColumnAndRow(3,$i)->getValue();
$location = $objWorksheet->getCellByColumnAndRow(4,$i)->getValue();
$data_user = array(
"name" => $name,
"email" => $email ,
"phone" => $phone ,
"details" => $details ,
"location" => $location ,
);
$this->dashboard_model->add_data($data_user);
}
Whenever i call this controller i got the following error
Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open C:/xampp/htdocs/bitcoinreports/uploads/data.xlsx for reading! File does not exist.' in C:\xampp\htdocs\bitcoinreports\application\third_party\PHPExcel\Reader\Excel2007.php:347
Stack trace:
#0 C:\xampp\htdocs\bitcoinreports\application\controllers\admin\upload.php(40): PHPExcel_Reader_Excel2007->load('C:/xampp/htdocs...')
#1 [internal function]: Upload->index()
#2 C:\xampp\htdocs\bitcoinreports\system\core\CodeIgniter.php(359): call_user_func_array(Array, Array)
#3 C:\xampp\htdocs\bitcoinreports\index.php(202): require_once('C:\\xampp\\htdocs...')
#4 {main}
thrown in C:\xampp\htdocs\bitcoinreports\application\third_party\PHPExcel\Reader\Excel2007.php on line 347
How to solve this problem ?
Before opening the file, check if it exists, AND is readable for the process that runs PHP (xampp in this case. Not sure how the user is named in xampp, maybe apache).
http://php.net/manual/en/function.is-readable.php
is_readable — Tells whether a file exists and is readable
So replace this:
$objPHPExcel=$objReader->load($path);
with
if (is_readable($path)){
$objPHPExcel=$objReader->load($path);
} else {
// handle problem, for example
echo "cannot read $path";
exit;
}
I have PDF with 25 pages, I want to convert it into images with all pages.
My code:
First I have found number of pages.
$tmpfname= 'test_pdf.pdf';
$path = "/var/www/my_path";
$numberOfPages = $this->count_pages($tmpfname);
$numberOfPages = intval($numberOfPages); // Number of pages eg.25
Loop for convert pdf to images
for($i=0;$i<=$numberOfPages;$i++){
$im = new imagick( $tmpfname.'['.$i.']' );
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->resizeImage('1024','800', Imagick::FILTER_UNDEFINED, 0.9, true);
$im->setCompressionQuality(100);
$im->setImageFormat('jpeg');
$im->writeImage($path.'/'.$i.'.jpg');
$im->clear();
$im->destroy();
}
Function for find number of pages
function count_pages($pdfname) {
$pdftext = file_get_contents($pdfname);
$num = preg_match_all("/\/Page\W/",$pdftext, $dummy);
return $num;
}
PDF uploaded ok, and also converted images from PDF. But got this type of error after completed process:
Fatal error: Uncaught exception 'ImagickException' with message
'Postscript delegate failed /var/www/php/flipbook/uploads/flipbooks/61/test_pdf.pdf':
# error/pdf.c/ReadPDFImage/663' in /var/www/php/flipbook/application/controllers/admin/flipbooks.php:83
Stack trace: #0 /var/www/php/flipbook/application/controllers/admin/flipbooks.php(83):
Imagick->__construct('/var/www/php/fl...')
#1 [internal function]: Flipbooks->create()
#2 /var/www/php/flipbook/system/core/CodeIgniter.php(359): call_user_func_array(Array, Array)
#3 /var/www/php/flipbook/index.php(210): require_once('/var/www/php/fl...')
#4 {main} thrown in /var/www/php/flipbook/application/controllers/admin/flipbooks.php on line 83
Any buddy can help me very appreciate...
If nothing else, you have an off by one error:
for($i=0;$i<=$numberOfPages;$i++){
//..
}
This attempts to print numberOfPages + 1 pages as the index starts at zero.
I got the perfect solution from here
My new code
$tmpfname= 'test_pdf.pdf';
$path = "/var/www/my_path";
$numberOfPages = $this->count_pages($tmpfname);
$numberOfPages = intval($numberOfPages); // Number of pages eg.25
// Saving every page of a PDF separately as a JPG thumbnail
$images = new Imagick("test_pdf.pdf");
foreach($images as $i=>$image) {
// Providing 0 forces thumbnail Image to maintain aspect ratio
$image->thumbnailImage(1024,0);
$image->writeImage($path.'/'.$i.'.jpg');
}
$images->clear();
function count_pages($pdfname) {
$pdftext = file_get_contents($pdfname);
$num = preg_match_all("/\/Page\W/",$pdftext, $dummy);
return $num;
}
I am trying to create Excel File and appending data to it from a webpage. Excel file is created corrected and data is also saved in it, but when I try to load the file again and append data to it. it gives a Fatal error
Uncaught exception 'PHPExcel_Writer_Exception' with message 'File
zip:///home/timespk/public_html/htmlParser/index.xlsx#xl/media/b1b39dadf76812b4c58e06ea6ddf57841.png
does not exist' in
/home/timespk/public_html/Classes/PHPExcel/Writer/Excel2007/ContentTypes.php:216
Stack trace: #0
/home/timespk/public_html/Classes/PHPExcel/Writer/Excel2007/ContentTypes.php(164):
PHPExcel_Writer_Excel2007_ContentTypes->_getImageMimeType('zip:///home/tim...')
1 /home/timespk/public_html/Classes/PHPExcel/Writer/Excel2007.php(224):
PHPExcel_Writer_Excel2007_ContentTypes->writeContentTypes(Object(PHPExcel),
false) #2 /home/timespk/public_html/htmlParser/index.php(216):
PHPExcel_Writer_Excel2007->save('/home/timespk/p...') #3 {main} thrown
in
/home/timespk/public_html/Classes/PHPExcel/Writer/Excel2007/ContentTypes.php
on line 216
Can anyone tell what I am doing wrong here?
This is the code that I am using to save Image to Excel file
$objPHPExcel2->getActiveSheet()->getColumnDimension('A')->setWidth(22);
$image = file_get_contents($imgSrc);
file_put_contents('image' . $highestRow . '.jpg', $image);
$gdImage = imagecreatefromjpeg('image' . $highestRow . '.jpg');
$objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setImageResource($gdImage);
$objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
$objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
$objDrawing->setHeight(150);
$objDrawing->setCoordinates('A'. $highestRow);
$objDrawing->setWorksheet($objPHPExcel2->getActiveSheet());
I believe that I've found proper solution to this problem which works with PHPExcel_Worksheet_Drawing:
https://stackoverflow.com/a/23951597/925196