ImageMagick/Imagick convert PDF to JPG using native PHP API - php

I’m attempting to convert PDF files into PNGs. It works great from the command line (I do have GhostScript 8.64 installed). But from PHP I’m having a problem:
code:
$im = new Imagick($pdf_file); // this is where it throws the exception below
output:
Fatal error: Uncaught exception ‘ImagickException’ with message ‘Postscript delegate failed `23_1235606503.pdf’: No such file or directory # pdf.c/ReadPDFImage/612′ in get_thumbnail.php:93
Stack trace:
\#0 get_thumbnail.php(93): Imagick->__construct(’…’)
etc. etc.
I'm not sure what I'm doing wrong here, but I suspect it has something to do with my server configuration somewhere. I'm running:
Apache 2.2.11
PHP 5.2.8
ImageMagick 6.4.8-9
GhostScript 8.64

Finally figured this out. The GhostScript executable (gs) wasn't in Apache's environment path. It was in /usr/local/bin. Though I tried several ways to add /usr/local/bin to the path, I did not succeed. I ended up putting a symlink for gs in the /usr/bin directory. Now everything works perfectly.

I don't have the "reputation" on Stackoverflow to add a comment inline above, but there is an extra step I had to perform to get this working on my Mac with the latest Sierra update.
When you enter the command:
sudo ln -s /usr/local/bin/gs /usr/bin/gs
On the Mac, you may get the error, "Operation not Permitted".
Apparently Apple made a change that the "bin" directory is not editable, unless you disable SIP (System Integrity Protection).
So here are the steps to do that:
Reboot your Mac into Recorvery Mode by restarting your computer and holding down "Command + R" until the Apple logo appears on your screen.
Click Utilities > Terminal
In the Terminal window, type in crutil disable and press "Enter"
Restart your Mac.
I just went through these steps and now my Ghostscript works great and I successfully converted a PDF to JPG.

I am successfully doing this. Here is the code that I am using to do the conversion. We are using this solution commercially. I know this question has been out there for awhile, but it may still help you.
//Convert PDF contract to image using ImageMagik and Ghostscript
// NOTE: This will need to be change if running on Linux
$source = $appDir."\\".$clientID."\\".$clientID.".pdf";
$dest = $appDir."\\".$clientID."\\".$clientID.".jpg";
//print("c:\\IM\\convert.exe $source $dest ");
exec("c:\\IM\\convert.exe $source $dest ");

Related

Unable to run inkscape in PHP shell_exec() command

I have a design tool extension that is used on a website I'm working on. The design tool uses inkscape command line to export images. There is a php interface to work with the command line operations which ultimately calls shell_exec($inkscapeCmd). After noticing the image files weren't being exported, I created a few tests to try and debug. I changed the execute line to shell_exec($inkscapeCmd . ' 2>&1') in order to see the error message:
sh: inkscape: command not found
...which is odd since it's definitely installed and accessible. I added a check for the user on my test page to make sure the commands were being executed by a user with access to inkscape:
$processUser = posix_getpwuid(posix_geteuid());
echo 'user: ' . $processUser['name'];
Then I ssh'd into the server to confirm that I could run the same commands as that user, and was able to run them without any issues (which also confirmed inkscape was in the PATH). I'm able to run other, basic shell commands from PHP without issue, like so:
echo shell_exec('ls');
But now I'm at a loss; I'm not sure what else to check to determine why I'm getting the 'command not found' error. Any direction would be helpful.
The server (rather old, I know):
CentOS 6.7
PHP 5.3.3
Inkscape v0.47
The process does not have the location for inkscape in it's path.
You will have to supply to full path to the executable.
Example
/usr/bin/inkscape
At the command line type 'whereis inkscape' to find is location.

WKHTMLTOPDF works on command line, but returns 0 byte pdf when generating from PHP

I'm using WKHTMLTOPDF to generate PDFs. It's been working fine but we've recently changed server and it's now only giving us back 0 byte files from our PHP admin panel.
Strangely, if I run the below from ssh, it generates just fine. I guess it's perhaps an issue with permissions of www-data but I can't figure it out :(
WKHTMLTOPDF is at /usr/local/bin/wkhmtltopdf and we're using httpd and centos.
If more info is needed I'm happy to try provide it. I can't seem to find any log for wkhtmltopdf either, it doesn't go into the httpd log.
Appreciate the help! Pulling my hair out :p
EDIT: here's the code I'm using to generate the PDF - it has worked on the old server
PDF::setOutputMode('F'); // force to file
PDF::setPageSize('a0');
$gen = PDF::html('pdf.print.web', [
'products' => $catalogue,
'name' => $master->name
], storage_path()."/pdf/gen-".$master->url);
return Response::download(storage_path()."/pdf/gen-".$master->url.'.pdf');
A had a similar issue using wkhtmltopdf CLI (not PHP). It ended up being a permission issue.
When I did:
wkhtmltopdf http://google.com google.pdf
The operation would succeed but generate a 0-byte pdf. No errors thrown.
When I did:
sudo wkhtmltopdf http://google.com google.pdf
The pdf has content. So ensure the relevant user(s) have r/w/e permission to the command itself and to the location where you want to write the file. Also make sure they have permission to make the outbound web request if relevant, and the server is config'd to allow this. You won't actually have to sudo once the permissions are fixed.
Hope this helps.

Trying to making work russian lang in TeX

Ubuntu 12.04.4 LTS
PHP 5.3.10-1ubuntu3.11
MediaWiki 1.16.5
I installed TeX themself and he works, but russian letters in \text{} not working, error: Failed to parse (PNG conversion failed; check for correct installation of latex, dvips, gs, and convert).
If I run command (/home/user/www/site.ru/math/texvc '/home/user/www/site.ru/images/tmp' '/home/user/www/site.ru/images/tmp' '\text{ пример }' 'utf-8' 'transparent') (user is same as in apache) from cmd it works! File in /home/user/www/site.ru/images/tmp exists. And output is cd7d4363323218b64410c10a0a2b46773 пример . Wich proving installation is correct.
When same command executed in MediaWiki, output is similar (cd7d4363323218b64410c10a0a2b46773 пример ), but file doesn't exists. In /home/user/www/site.ru/images/tmp folder appears log missfont.log with this contents:
mktextfm larm1200
mktextfm larm0800
mktextfm larm0600
I'm tried to run this commands but it change nothing. I even tried create test.php and test without CMS. Result is same as in MediaWiki (missfont.log created in temp).
PS In Ubuntu 9.10 russian lang in \text{} worked. I added in texutil.ml russian babel "\usepackage[russian]{babel}\n\usepackage{cancel}\n\pagestyle{empty}\n\begin{document}\n$$\n" and recompile sources. And did some changes in Math.php (with locals). Also I downloaded latest Math extension and replaced math folder. Read more here.
Problem was in system variable HOME. So to fix you need to add in LocalSettings.php line putenv('HOME=/home/user'); (with your user home dir).

PHMagick not working after code moved to a different server

I built a pdf generator script for one of my clients and everything works properly. He then decided that he wanted me to create a thumbnail image from the PDF to display on another page, so I came across PHMagick and that seemed to do the trick.
The following code worked on my building server (iPage -- I think it's debian?), but now that I've moved it to his VPS, it is no longer working. FPDF has no trouble generating the pdf, so it is not a permissions issue.
Code:
require("phmagick/phmagick.php");
$pdf->Output("pdf.pdf");
$p = new phmagick('','pdf.png');
$p->debug = true;
$p->resize(800);
$p->acquireFrame("pdf.pdf");
This code returns the following error:
Error executing "convert "pdf.pdf"[0] "pdf.png"" return code: 127 command output :"sh: convert: command not found"
I have not changed any of the code since I moved it from the original server, and I just copied the entire site over, so why is PHMagick not working now? How do I get it to recognize convert.php in the plugins folder?
Just a note: I am only using PHMagick, not the whole of ImageMagick.
PHMagic appears to be using the command line convert command in background.
Most likely that command is simply not installed on the server.
PHMagic use the command line convert,composite etc... commands in background.
These commands are only available if ImageMagick is installed in your server.
In your previous server ImageMagick must have been installed.
Ipage sometimes do not pre install ImageMagick on their VPS servers. And you might not be able to install it your self ( through terminal login using ... yum install ImageMagick ) coz you will be prompt for root password.
To make sure the ImageMagick binaries are installed in ur server, in ur case the convert command,
type this command in the ssh terminal...
whereis convert
If the path to the convert command is not shown, it means you need to install ImageMagick.
You will have to open a support ticket and ask them to install it for you
As arkascha said, you'll need to install imagemagick. Try yum install imagemagick since you're running CentOS.
EDIT: May be a different capitalization: ImageMagick

PHP on IIS - Exec Returns 1

I have ImageMagick, IIS 6, Ghostscript, and PHP 5.3.1 installed on a Windows Server 2008 box and am trying to convert a PDF file into a JPG thumbnail.
However, it doesn't seem to be working-- the call to exec() produces a return code of 1 (which to my understanding, means that some general error has occurred). The output variable is simply an empty array. $output is simply an empty array. The same occurs if I use system() instead of exec()
Running the command from the command line seems to work, so my initial guess would be a simple permission issue... The directory is writable by PHP because the script that uploads a PDF to the directory works, and I have verified that everyone has permission to write to the directory as well.
Also, safe mode is off.
Any ideas as to what the issue could be?
Relevant code:
<?php
$output = array();
$ret = 0;
echo exec('convert D:\content\myfile.pdf[0] D:\content\myfile_thumb_1.jpg', $output, $ret);
var_dump($ret);
?>
Note: While I would test this on Apache on Mac OS X, I can't seem to get ImageMagick or Ghostscript installed correctly.
I've also ensured the following:
Correct case for the file names (all lowercase anyways)
Using the full path of convert.exe in addition to simply convert.exe
UPDATE:
After checking the task manager, convert.exe is being run and is taking up CPU time, suggesting to me that it is file permissions of some sort... I'm going to check it out now.
It turns out there was a permission issue with something in IIS-- the website ran under a different user than the command from CMD, which obviously results in it not working under IIS because it has lesser permissions than from CMD.

Categories