how to convert pdf url to png ?

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
khajenejad

how to convert pdf url to png ?

Post by khajenejad »

Hi,
I convert my local pdf file pages to png files by this code:
$im = new imagick( $url.'['. $i . ']');
$im->setImageFormat( "png" );
$im->writeImage($thisFile);

this code works only when my pdf file exists on my local (for example $url = "./pdf_files/test.pdf" )
but, if I change it to a url , $url="http://news.um.ac.ir/test.pdf" ,it can not convert pdf url page to png. it reports error (http://news.um.ac.ir/test.pdf is a valid file)

what can I do?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to convert pdf url to png ?

Post by fmw42 »

I would guess it is a PHP security restriction, but I am not an expert on PHP and Imagick.
khajenejad

Re: how to convert pdf url to png ?

Post by khajenejad »

fmw42 wrote:I would guess it is a PHP security restriction, but I am not an expert on PHP and Imagick.
allow_url_fopen is On in php.ini . and I set file mod to 777 .
but I still receive this error:
exception 'ImagickException' with message 'unable to open image `/var/www/html/pn-sites/pdfviewer/http:/khajenejad.staffcms.um.ac.ir/imagesm/1023/stories/pm-76.pdf': No such file or directory @ magick/blob.c/OpenBlob/2418' in /var/www/html/pn-sites/pdfviewer/index.php:39 Stack trace: #0 /var/www/html/pn-sites/pdfviewer/index.php(39): Imagick->__construct('http://khajenej...') #1 {main}

notice : when I execute this code
$url = "http://localhost/pdf/pdf_file/test.pdf";
$im = new imagick( $url.'['. $i . ']');
it can create the page but when I execute it with other url (bellow) I receive the above error
$url = "http://localhost/pdf/pdf_file/test.pdf";
$im = new imagick( $url.'['. $i . ']');

please help me.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: how to convert pdf url to png ?

Post by el_supremo »

Code: Select all

notice : when I execute this code
$url = "http://localhost/pdf/pdf_file/test.pdf";
$im = new imagick( $url.'['. $i . ']');
it can create the page but when I execute it with other url (bellow) I receive the above error
$url = "http://localhost/pdf/pdf_file/test.pdf";
$im = new imagick( $url.'['. $i . ']'); 
The two urls in this example look identical.
exception 'ImagickException' with message 'unable to open image
The pathname given in this error message would not be legal in Windows (the colon is not allowed in a filename) but perhaps it is legal in Linux.
Is it significant that the name contains "http:/khajenejad." not "http://khajenejad."? It looks like something is getting confused between a pathname and an URL because the path which precedes it (/var/www/html/pn-sites/pdfviewer/) is also the path to the PHP script index.php.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
khajenejad

Re: how to convert pdf url to png ?

Post by khajenejad »

so, imagick() function maybe can not open a url? it just open a local file?
Now, What should I do? I want to write a program that get a url of pdf file then (without upload it ) convert it's pages to png files and show it , such as docs.google.com viewer.
Is imagick usefull for this perpose?
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: how to convert pdf url to png ?

Post by el_supremo »

Imagick() can open an URL. The error message you received suggests that you didn't specify the URL correctly. I think you must have specified "http:/khajenejad.staffcms.um.ac.ir/imagesm/1023/stories/pm-76.pdf" as the url. That is not a valid URL (it should start with http://) but it does look like a relative pathname to a file, so imagick tries to open it as a local file. That is why the Imagick error message reported that "/var/www/html/pn-sites/pdfviewer/http:/khajenejad.staffcms.um.ac.ir/imagesm/1023/stories/pm-76.pdf" was the missing filename.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
khajenejad

Re: how to convert pdf url to png ?

Post by khajenejad »

el_supremo wrote:Imagick() can open an URL. The error message you received suggests that you didn't specify the URL correctly. I think you must have specified "http:/khajenejad.staffcms.um.ac.ir/imagesm/1023/stories/pm-76.pdf" as the url. That is not a valid URL (it should start with http://) but it does look like a relative pathname to a file, so imagick tries to open it as a local file. That is why the Imagick error message reported that "/var/www/html/pn-sites/pdfviewer/http:/khajenejad.staffcms.um.ac.ir/imagesm/1023/stories/pm-76.pdf" was the missing filename.

Pete
No, I execute this code:
$url = "http://khajenejad.staffcms.um.ac.ir/imagesm/1023/stories/pm-76.pdf";
$im = new imagick( $url.'['. $i . ']');

and receive this error:
exception 'ImagickException' with message 'unable to open image `/var/www/PDF/http:/khajenejad.staffcms.um.ac.ir/imagesm/1023/stories/pm-76.pdf': No such file or directory @ blob.c/OpenBlob/2439' in /var/www/PDF/index.php:37 Stack trace: #0 /var/www/PDF/index.php(37): Imagick->__construct('http://khajenej...') #1 {main}
so, maybe imagick just get a path not url!!!!.....

would you test this function (new imagick()) with a pdf url input to know if it work or not?
what should I do to convet my pdf url to png files page by page with out uploading?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to convert pdf url to png ?

Post by fmw42 »

what should I do to convet my pdf url to png files page by page with out uploading?
What do you mean without uploading? IM has to retrieve the file over the internet if you use http://... So it will be downloaded from that web site.

If you are trying to get the file from your same server, then using http:// is not appropriate.

Can you clarify further? Is the file you are trying to use coming from some other server on the internet or from your own computer?

What do you mean by "without uploading"?

If your pdf file is coming from another server, do you have permission on that server to access the directory that it is in? Can you retrieve the file in your browser?

OK. I can access it in my browser. So testing...

Your PDF file is very large and takes a long time to download. Therefore PHP may timeout before it finishes.

In command line, I can do

time convert http://khajenejad.staffcms.um.ac.ir/ima ... /pm-76.pdf tmp.png

real 1m25.334s
user 0m6.081s
sys 0m0.779s

And tmp.png has 4 files: -0, -1, -2, and -3

Note that it took over 1 minute on my fast Comcast connection. Perhaps your connection is not fast enough and it times out.

I don't know if this works in PHP or especially Imagick.

You could try doing it with PHP and exec rather than Imagick. Perhaps, Imagick cannot do (or is not configured appropriately -- permission?) for that particular operation with web-based files.

Perhaps your Imagick is not configured with the Ghostscript delegate or png delegate and so does not know how to use pdf or png?

Try doing the same by retrieving some image other than in pdf format (say jpg or gif)?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to convert pdf url to png ?

Post by fmw42 »

I tried both:

<?php
$image = "http://khajenejad.staffcms.um.ac.ir/ima ... m-76.pdf[0]";
$output = "tmp.png";
$im = new Imagick();
$im->readImage( $image );
$im->writeImage( $output );
?>

and

<?php
$image = "http://khajenejad.staffcms.um.ac.ir/ima ... m-76.pdf[0]";
$output = "tmp.png";
exec("/usr/local/bin/convert $image $output", $out, $returnval);
print_r($out);
?>

The latter gives the following error:

Array ( [0] => CRIT: rangecheck in .setuserparams [1] => Operand stack: [2] => --nostringval-- --nostringval-- --nostringval-- )

which I do not understand. Perhaps it does not like some character in the url?


So perhaps as I said, PHP is not configured to use urls for input images. Or I am not expert enough with PHP to know how to do it.
Post Reply