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.
Related
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!
<?php
require_once 'PHPExcel/PHPExcel.php';
$downloadedFileName = "sheet.xls";
set_time_limit(0);
if (file_exists($downloadedFileName)) {
unlink($downloadedFileName);
}
$spreadsheet_url="Google Sheet with 4 tabs URL goes here";
file_put_contents($downloadedFileName,file_get_contents($spreadsheet_url));
if (file_exists($downloadedFileName)) {
$inputFileType = PHPExcel_IOFactory::identify($downloadedFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcelReader = $objReader->load($downloadedFileName);
$loadedSheetNames = $objPHPExcelReader->getSheetNames();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$objWriter->setSheetIndex($sheetIndex);
$objWriter->save($loadedSheetName.'.csv');
}
}
else{
echo "File not found";
}
?>
This gives an error :
Fatal error: Uncaught exception 'PHPExcel_Calculation_Exception' with
message 'ArticulosQueNoSonKARE!A1 -> internal error' in
D:\xampp\htdocs\xlstocsv\PHPExcel\PHPExcel\Cell.php:291 Stack trace: #0
D:\xampp\htdocs\xlstocsv\PHPExcel\PHPExcel\Worksheet.php(2492): PHPExcel_Cell-
>getCalculatedValue() #1
D:\xampp\htdocs\xlstocsv\PHPExcel\PHPExcel\Writer\CSV.php(142):
PHPExcel_Worksheet->rangeToArray('A1:M1', '', true) #2
D:\xampp\htdocs\xlstocsv\index.php(30): PHPExcel_Writer_CSV-
>save('ArticulosQueNoS...') #3 {main} thrown in
D:\xampp\htdocs\xlstocsv\PHPExcel\PHPExcel\Cell.php on line 291
I can figureout that its because of some cells containing formulas..But I need to take that value from the cell.
I have no edit permission to modify and edit those cells. I need this script running as a chrone job.. Ihave done with the other part.
Please help.
Thank you
I'm trying to convert a pdf file to a jpeg. But I cant even get the simplest example to work.
<?php
if (!extension_loaded('imagick'))
die('Imagick is not installed');
else
echo 'Imagick installed<hr>';
$inputFile="test.pdf";
if(file_exists($inputFile)){
echo "$inputFile exists.<br>";
}
else{
die("$inputFile doesnt exist.");
}
$imagick = new Imagick();
$imagick->readImage($inputFile);
$imagick = $imagick->flattenImages();
$imagick->writeFile('test.jpg');
?>
The first test says the file exists but imagaick is not able to read the file.
Fatal error: Uncaught exception 'ImagickException' with message
'unable to open image `test.pdf': No such file or directory #
error/blob.c/OpenBlob/2646' in C:\xampp\htdocs\img\index.php:14 Stack trace: #0
C:\xampp\htdocs\img\index.php(14): Imagick->__construct('test.pdf') #1 {main} thrown in
C:\xampp\htdocs\img\index.php on line 14
What am I doing wrong ?
The problem is in your path to pdf file,
simply use $_SERVER['DOCUMENT_ROOT']
$inputFile = $_SERVER['DOCUMENT_ROOT']."/test.pdf";
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
When I run blow code:
/*** PPT to Image conversion ***/
$ppt_file = 'E:\wamp\www\temp/a.pptx';
$app = new COM("PowerPoint.application") or die("Unable to instantiate PowerPoint");
$app->Visible = true;
$app->Presentations->Open($ppt_file);
$app->Presentations[1]->SaveAs("E:/tmp/outdir",18);
$app->Presentations[1]->Close();
$app->Quit();
$app = null;
It gives me one exception :
Fatal error: Uncaught exception 'com_exception' with message 'Source: Microsoft Office PowerPoint 2007Description: PowerPoint could not open the file.' in E:\wamp\www\temp\video_conversion.php:107 Stack trace: #0 E:\wamp\www\temp\video_conversion.php(107): variant->Open('E:\wamp\www\tem...') #1 {main} thrown in E:\wamp\www\temp\video_conversion.php on line 107
I am unable to figure out what is the problem.
This is kind of issue is due to the following factors.
PHP.ini settings
Folder Permission
allow open is not enabled in the server
allowed upload size
Within your error, you see the following message: PowerPoint could not open the file.' in E:\wamp\www\temp\video_conversion.php:107
Does the PHP user have permissions to the file E:\wamp\www\temp/a.pptx?
Try correcting your slashes: E:\wamp\www\temp\a.pptx as / normally refers to an option or argument.
At the end of the day, it appears to be a permissions error, a location issue or alike which is preventing access to that file. Can you open the file with fopen or file_get_contents?
Try this with com class:
COM class Reference: - http://us2.php.net/manual/en/class.com.php
<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<?
$ppApp = new COM("PowerPoint.Application");
$ppApp->Visible = True;
$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp
$ppName = "MySlides.ppt";
$FileName = "MyPP";
//*** Open Document ***//
$ppApp->Presentations->Open(realpath($ppName));
//*** Save Document ***//
$ppApp->ActivePresentation->SaveAs($strPath."/".$FileName,17); //'*** 18=PNG, 19=BMP **'
//$ppApp->ActivePresentation->SaveAs(realpath($FileName),17);
$ppApp->Quit;
$ppApp = null;
?>
PowerPoint Created to Folder <b><?=$FileName?></b>
</body>
</html>
Or try this :
$powerpnt = new COM("powerpoint.application") or die("Unable to instantiate Powerpoint");
$presentation = $powerpnt->Presentations->Open(realpath($file), false, false, false) or die("Unable to open presentation");
foreach($presentation->Slides as $slide)
{
$slideName = "Slide_" . $slide->SlideNumber;
$exportFolder = realpath($uploadsFolder);
$slide->Export($exportFolder."\\".$slideName.".jpg", "jpg", "600", "400");
}
$powerpnt->quit();