Jump to content

PDF to JPEG thumbnail error


lenoir
 Share

Recommended Posts

Hi,

I can't get this script to work.

I'm simply trying to generate the first page of a PDF from a file fieldtype.

I keep getting an error message like this: "Error: Exception: Unable to read the file: …". The path to the PDF seems correct to me.

foreach($page->datei as $publikation){
  if($publikation->ext === "pdf"){
		$file = $publikation->url;
        $im = new Imagick();
        $im->setResolution(300, 300);
        $im->readImage($file . '[0]');
        $im->setImageFormat('jpg');
        header('Content-Type: image/jpeg');
        echo $im;
	}
}

Any idea what I'm doing wrong here?

Thanks!

Link to comment
Share on other sites

Use this module: http://modules.processwire.com/modules/fieldtype-pdf/

Or if you want to make images of all pages in the PDF, then try this one: https://github.com/adrianbj/ProcessPDFImageCreator

Keep in mind that creating images from PDF files requires that Ghostscript is compiled into Imagemagick.

Also, if you are uploading PDFs created for print (ie in CMYK), then you should read this: http://www.lassosoft.com/CMYK-Colour-Matching-with-ImageMagick so that you can get better color matching - otherwise the images will look washed out.

  • Like 3
Link to comment
Share on other sites

Thanks Adrian.

Yes, I looked into the module "Filetype-pdf", but there are two issues for me. First, it's not supported in the current Processwire version (>3.0). Second, my field has both Jpegs and PDFs, and this module only accepts PDFs into the field.

That's why i was looking for a simple code to do the job.

Link to comment
Share on other sites

5 hours ago, adrian said:

That first module is marked in the modules directory as being compatible with PW 3.x - what errors are you getting with it?

I haven't tested the module yet. True, I missed the compatibility part. It says somewhere below: 

Requirements: Processwire >=2.5 <3.0

That's what threw me off. I guess the text isn't quite up to date…

I'll give it a try now. 

Thanks for your help.

Link to comment
Share on other sites

Yep. Just what i was dreading: I installed the module and it's not compatible with version 3.0. Now I can't delete the field that cause the issue, and I can't get rid of the module since it's used by a field… 

:(

 

PS: I could restore from backup. I'll inform the compatibility issue in the right forum.

Link to comment
Share on other sites

If you ever have this situation again, it's fairly easy to delete a module via the database. Deleting a field is also possible although there are some more things to consider.

I just took a look at that module and it definitely will need some tweaking to get working with PW 3 because of it's use of namespaces and the way extends just certain fieldtype classes in PW, rather than Wire or WireData.

I do know for certain that my old module (linked to above) works on PW 3 - I am using it on several sites. It is definitely less polished, but it gets the job done :)

  • Like 1
Link to comment
Share on other sites

Ok ok ok… You got me curious, I'll try out your module ;-)

I was reluctant to use it since I only need the first page of each PDF, but the PDFs I'm dealing with have 1-5 pages, so it's not likely going to take too much storage space. 

Will it work if placed in a repeater?

Link to comment
Share on other sites

It would be simple to make it only grab the first page: https://github.com/adrianbj/ProcessPDFImageCreator/blob/12a402175c39b506d49497d201f79bd8db6313ef/ProcessPDFImageCreator.module#L138-L140

Replace with:

$this->createPdfImage($page, $image_field, $pdf_field_name, 0);

I don't know if it will work in a repeater - I haven't tested, but I expect it will need some tweaks to reference the actual page correctly.

  • Like 2
Link to comment
Share on other sites

  • 1 year later...

It happened to me several times as well, because of the PDF's being corrupted, I supposed. Nevertheless, I wasn't capable enough to sort that out with just looking at the code and thinking, so I fixed them manually with the editor. A trial version of Acrobat Pro didn't allow to do so, and I did with this [link-removed ]because it's cheaper and has the same functionality. Hit-or-miss, but worked out nevertheless

Edited by Macrura
external link removed
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...