Nas Atchia Nas Atchia - 2 years ago 193
PHP Question

PHP exec() convert pdf to jpg with space in filename on Windows

How to convert pdf files that contain spaces in their filenames using the exec() function.

For e.g: file 1.pdf to file 1.jpg


$pdf = "file 1.pdf";
$pdf_first_page = "file 1.pdf[0]";
$jpg = str_replace("pdf", "jpg", $pdf);
exec ("convert $pdf_first_page $jpg");


I'm getting the following errors:

convert.exe: unable to open image `file 1.pdf': No such file or directory @ error/blob.c/OpenBlob/2702.
convert.exe: no images defined `file 1.jpg' @ error/convert.c/ConvertImageCommand/3257.

Answer Source

You need to pass the arguments through escapeshellarg(), like this:

$pdf_escaped = escapeshellarg($pdf_first_page);
$jpg_escaped = escapeshellarg($jpg);
exec("convert $pdf_escaped $jpg_escaped");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download