-1

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

<?php

$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.
Nas Atchia
  • 407
  • 3
  • 7
  • 14
  • Possible duplicate of [Convert PDF to JPG image with PHP](http://stackoverflow.com/questions/13600598/convert-pdf-to-jpg-image-with-php) – Nasreddine May 15 '16 at 20:30
  • Converting the pdf file without space in the filename is working for me. I'm having problems with files that contains space in their filenames. – Nas Atchia May 15 '16 at 20:36
  • The linked question also offers a solution for this which is to put the filename in between quotes – Nasreddine May 15 '16 at 20:39
  • Still it doesn't convert pdf files with space in their filename. – Nas Atchia May 15 '16 at 20:49

3 Answers3

3

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");
Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
  • Still it doesn't convert pdf files with space in their filename. – Nas Atchia May 15 '16 at 20:48
  • It should. Could you please edit your question to specify which OS you're using PHP on (Linux, Windows?), and the specific error that you're getting? (You *are* [logging errors](http://stackoverflow.com/questions/3531703/how-to-log-errors-and-warnings-into-a-file), right?) – Ilmari Karonen May 15 '16 at 20:52
  • Not I'm not logging errors. I'm using PHP on windows. – Nas Atchia May 15 '16 at 20:55
  • 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. – Nas Atchia May 15 '16 at 21:00
  • 1
    Does it work if you rename the files to have no space in their name (and change your code accordingly, of course)? Looking at your error message, it looks like the spaces are working fine, but the file is not in the directory that PHP looks for it in. – Ilmari Karonen May 15 '16 at 21:03
  • Just tested with renaming 'file 1' to 'file'. It works. It is some problem with the path of the file? – Nas Atchia May 15 '16 at 21:06
  • 1
    Thanks. It works now. I restarted Apache and it's now good :) – Nas Atchia May 15 '16 at 21:09
1

Try to put the arguments (filenames) in double quotes (which you have to escape by backslashes):

exec ("convert \"$pdf_first_page\" \"$jpg\"");

To surround the passed shell comand arguments with double quotes is generally a good practice - even with arguments/filenames without spaces

Reto
  • 1,305
  • 1
  • 18
  • 32
  • Still it doesn't convert pdf files with space in their filename. – Nas Atchia May 15 '16 at 20:48
  • Your code with the last line altered as proposed in my answer runs successfully on my machine (Debian). It outputs `file 1.jpg` as expected. – Reto May 15 '16 at 20:56
  • 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. – Nas Atchia May 15 '16 at 21:02
0

Spaces in file names on Linux must be escaped with \ note escape character before the space. Below is an example.

file\ 1.pdf
Dan Sabin
  • 896
  • 1
  • 9
  • 14