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
Related
I would like to convert a pdf file to images in PHP.
I have got some exception like this:
Fatal error: Uncaught ImagickException: UnableToOpenBlob './sample.pdf': No such file or directory # error/blob.c/OpenBlob/3533 in D:\Task\Clients\James\toImage\toImage.php:4 Stack trace: #0 D:\Task\Clients\James\toImage\toImage.php(4): Imagick->readImage('./sample.pdf') #1 {main} thrown in D:\Task\Clients\James\toImage\toImage.php on line 4
I wrote some code like as follows.
<?php
$pdf_url = ('./sample.pdf');
$imagick = new Imagick();
$imagick->readImage($pdf_url);
$imagick->resizeImage( 200, 200, imagick::FILTER_LANCZOS, 0);
$imagick->setImageFormat( "png" );
$imagick->writeImage('pdfAsImage.png');
Please help me!
Here is the code I have ever used before.
Use the full path to the image, for example:
$image = new Imagick($_SERVER['DOCUMENT_ROOT'] . '/pdfs/sample.pdf');
It should work like a charm!!!
<?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 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'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 having an issue with imagick php library.
I am doing a recursive search in my file system and look for any pdf files.
$it = new RecursiveDirectoryIterator("/test/project");
$display = Array ('pdf');
foreach(new RecursiveIteratorIterator($it) as $file){
if (in_array(strtolower(array_pop(explode('.', $file))), $display))
{
if(file_exists($file)){
echo $file; //this would echo /test/project/test1.pdf
$im = new Imagick($file);
$im->setImageFormat("jpg");
file_put_contents('test.txt', $im);
}
}
}
However, I am getting an error saying
Fatal error: Uncaught exception 'ImagickException' with message 'Can not process empty Imagick object' in /test.php:57
Stack trace:
#0 /test.php(57): Imagick->setimageformat('jpg')
#1 {main}
thrown in /test.php on line 57
line 57 is $im->setImageFormat("jpg");
However, if I replace my $im = new Imagick($file) with $im = new Imagick('/test/project/test1.pdf'), the error is gone.
I don't know why this is happening. Can someone give me a hint for this issue? Thanks so much
According to this, the .jpg files' format is JPEG.
Note 1:
Your $file variable is an object SplFileInfo, but you are using it always like a string. Use RecursiveDirectoryIterator::CURRENT_AS_PATHNAME flag in RecursiveDirectoryIterator's constructor, to be a real string.
Note 2:
You can filter the iterator entries with RegexIterator, f.ex.: new RegexIterator($recursiveIteratorIterator, '/\.pdf$/') (after Note 1). Or, you can use GlobIterator too for searching only pdf files.
As #pozs pointed out
Note 1: Your $file variable is an object SplFileInfo, but you are
using it always like a string.
Here's a code snippet which gets you the filename as string and has an optimized method to get the file extension:
<?php
$display = array ('pdf');
$directoryIterator = new RecursiveDirectoryIterator('./test/project');
// The key of the iterator is a string with the filename
foreach (new RecursiveIteratorIterator($directoryIterator) as $fileName => $file) {
// Optimized method to get the file extension
$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
if (in_array(strtolower($fileExtension), $display)) {
if(file_exists($fileName)){
echo "{$fileName}\n";
// Just do what you want with Imagick here
}
}
Maybe try this approach from : PDF to JPG conversion using PHP
$fp_pdf = fopen($pdf, 'rb');
$img = new imagick();
$img->readImageFile($fp_pdf);
It also seems from reading other posts that GhostScript is faster?