What does not work:
Converting filename-multiple-pages.pdf[0] PDF file to JPG
Converting filename-multiple-pages.pdf PDF file to JPG
Fatal error: Uncaught exception 'ImagickException' with message
'Postscript delegate failed `/path/to/filename-multiple-pages.pdf': No
such file or directory # error/pdf.c/ReadPDFImage/664' in ...
When I try this with a sollution found on the webs with fopen and then using readImageFile of the fopen handle:
Fatal error: Uncaught exception 'ImagickException' with message
'Postscript delegate failed `/tmp/magick-rGGsdy9f': No such file or
directory # error/pdf.c/ReadPDFImage/664'
What does work:
Converting filename-multiple-pages.pdf[1] PDF file to JPG (the second page)
Converting filename-single-page.pdf PDF to JPG [/list]
The used PHP codes:
<?php
// this does work for a single page file
// it does NOT work for multiple page file
// it does NOT work when using pdffile.pdf[0]
// it DOES work when using pdffile.pdf[1]
$filename = '/path/to/pdffile.pdf';
$im = new Imagick();
$im->readImage($filename);
$im = $im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
$im->scaleImage(150, 150, true);
$im->writeImage('/path/to/image/pdffile.jpg');
?>
<?php
// i used alternative code which gave me the second /tmp/ dir error (see above)
$filename = '/path/to/pdffile.pdf';
$pdf_handle = fopen($filename, 'rb');
$doc_preview = new Imagick();
$doc_preview->setResolution(150,150);
$doc_preview->readImageFile($pdf_handle);
$doc_preview->setIteratorIndex(0);
$doc_preview->setImageFormat('jpeg');
$doc_preview->writeImage('/path/to/image/pdffile.jpg');
$doc_preview->clear();
$doc_preview->destroy();
?>
Installed modules by hosting provider
ImageMagick v6.7.2.7-5
Ghostscript 8.70
Does anyone have any idea what to do?
After some research I found out that the hosting provider didn't install recent versions. After a few days of testing and debugging they managed to install the latest versions and it all works now. The code in my post is good and can be used by others :).
Related
I am looking for a solution to that problem, I am trying to convert pdf to image using php image magic, here is the code, I have tried many solutions but still I am not able to understand why it shows same issue every time
$FILE = realpath(__DIR__ . '/file.pdf');
$im = new Imagick();
$im->pingImage($FILE);
$pageCount = $im->getNumberImages();
echo $pageCount;
If I put jpg file in it, works fine, but if I add file.pdf it shows this error
Fatal error: Uncaught ImagickException: PDFDelegateFailed `The system
cannot find the file specified. ' # error/pdf.c/ReadPDFImage/794
Note: I have installed ghost script 9.52 64 bit , Imagick 7. something! , I am looking for the help
I had the same problem and after a week trying to fix, I discovered that this problem seems to be related to the recents Ghostscript version.
I solved removing it and installing the version 9.26 after trying all of those.
I want to convert the PDF to JPG by using imagick, but after I press submit it keeps showing a fatal error: uncaught imagick error. Do you guys know if this is code problem or an imagick extension problem?
Your $_FILES['fileToUpload']['type'] stores the MIME type and you are assigning it to the variable $pdf_file and you are trying to read this with imagick. This can't work. You could var_dump($_FILES) and see if you can find the actual file in there.
I am trying to read a RAF file in php using Imagick but somehow its not working. I have looked into the formats supported by Imagick and RAF is listed.
I later tried to simply read RAF file and echo it out but still no luck.
I keep on getting this error:
Uncaught exception 'ImagickException' with message 'unable to open image `/tmp/magick-H2rs1zGJ.ppm': No such file or directory
I tried rendering a png file saved in the same directory and it worked but somehow its not working for RAF.
$imagePath = "uploaded.raf";
$imagick = new \Imagick($imagePath);
header("Content-Type: image/jpg");
echo $imagick->getImageBlob();
Any help is appreciated.
Thank you!
Overview
I'm trying to directly download an Excel spreadsheet created using PHPExcel. I don't have server-level access so I can't install or enable mods (such as the Zip module).
The data is a guestlist for an event.
Code
<?php
if(isset($_GET["event_id"])&&
!empty($_GET["event_id"])){
//Include PHPExcel, Excel2007, classes
require_once("inc/PHPExcel/PHPExcel.php");
require_once("inc/PHPExcel/PHPExcel/Writer/Excel2007.php");
require_once("inc/classes.php");
//Zip not installed - change settings to use local compression
PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);
//Get event data
$event_id = intval($_GET["event_id"]);
$event = new Event($event_id);
$guests = $event->getGuests();
//Create new PHPExcel object
$spreadsheet = new PHPExcel();
//Add data
$spreadsheet->setActiveSheetIndex(0);
$spreadsheet->getActiveSheet()->SetCellValue("B2", "TMC Gateway");
$spreadsheet->getActiveSheet()->SetCellValue("B3", "Event register");
$spreadsheet->getActiveSheet()->SetCellValue("B5", "Name");
$spreadsheet->getActiveSheet()->SetCellValue("C5", "Member/Guest");
$spreadsheet->getActiveSheet()->SetCellValue("D5", "Checkin Time");
foreach($guests as $guest){
if($guest["degree"]=="guest"){
$arr[] = [$guest["name1"]." ".$guest["name2"], "Guest", $guest["checkintime"]];
} else {
$arr[] = [trim($guest["name2"]), "Member", $guest["checkintime"]];
}
}
$currentCell = 6;
foreach($arr as $a){
$spreadsheet->getActiveSheet()->SetCellValue("B$currentCell",$a[0]);
$spreadsheet->getActiveSheet()->SetCellValue("C$currentCell",$a[1]);
$spreadsheet->getActiveSheet()->SetCellValue("D$currentCell",$a[2]);
$currentCell++;
}
//Rename sheet
$spreadsheet->getActiveSheet()->setTitle("TMC Gateway");
//Open writer
$writer = new PHPExcel_Writer_Excel2007($spreadsheet);
//Set headers and force download
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment;filename=\"TMC_Gateway_Attendees-".$event_id.".xls\"");
$writer->save("php://output");
//Kill script
exit;
}
Issue
When processing originally and opening the file, I saw this error:
Fatal error: Class 'ZipArchive' not found in /home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Writer/Excel2007.php on line 227
I realised this is probably because the Zip module was either not installed or not enabled, so I followed these instructions at Class 'ZipArchive' not found error while using PHPExcel:
If you don't have ZipArchive installed/enabled for your PHP, and can't enable it yourself, then you can use
PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);
However, now when opening the file, this error appears:
Fatal error: Uncaught exception 'PHPExcel_Writer_Exception' with message 'Error zipping files : PCLZIP_ERR_READ_OPEN_FAIL (-2) : Unable to open temporary file '/tmppclzip-56df08ee0384c.tmp' in binary write mode' in /home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Shared/ZipArchive.php:108
Stack trace:
#0 /home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Writer/Excel2007.php(278): PHPExcel_Shared_ZipArchive->addFromString('_rels/.rels', '<?xml version="...')
#1 /home/loqui/public_html/doorapp/xls.php(66): PHPExcel_Writer_Excel2007->save('php://output')
#2 {main}
thrown in /home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Shared/ZipArchive.php on line 108
Question
As I don't have the Zip module enabled, and seemingly limited permissions in the working folder, how can I make this script download the correctly created Excel file?
If you are going to continue using PCLZIP, I would recommend checking the tmp directory that it is attempting to write to and see what user that directory is assigned to. More than likely Apache does not have write access to that tmp directory and thus is failing to write files inside of it. I am struggling with a similar problem on the ZipArchive front, but if you have sufficient permissions chown -R user:user foldername might alleviate your write issues.
I'm having problems with reading a file with Imagick. I've installed the imagick and ghostscript extension successfully.
This is what I'm doing:
<?php
$im = new Imagick();
$im->setResolution(300, 300);
$im->readImage('/Applications/MAMP/htdocs/mywebsite/wp-content/plugins/myplugin/uploads/magazine_cover.pdf[0]');
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>
I'm getting the error:
Fatal error: Uncaught exception 'ImagickException' with message
'Failed to read the file' in /Applications/MAMP/htdocs/imagick.php on
line 10
When I try to execute the following command in my terminal:
convert magazine_cover.pdf magazine_cover.jpeg
I'm getting a warning:
**** Warning: considering '0000000000 XXXXX n' as a free entry.
**** This file had errors that were repaired or ignored.
**** The file was produced by:
**** >>>> Mac OS X 10.11.3 Quartz PDFContext <<<<
**** Please notify the author of the software that produced this
**** file that it does not conform to Adobe's published PDF
**** specification.
But he created the jpeg ... . Why doesn't this work in the browser?
Well the warning is generated by Ghostscript, it says your PDF file is not technically legal, but it should process just fine, unless there are more problems.
I would suggest you find out what command ImageMagick is passing to Ghostscript and try that on the command line. If it works then the problem is most likely at IM's end, otherwise the problem is that Ghostscript doesn't like your PDF file. The command line will probably give you more information. Even if it doesn't with that information and the file you can open a Ghostscript bug report and it can be fixed (if possible).