Jump to content
lenoir

PDF to JPEG thumbnail error

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!

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

I could run two fields (PDF & JPEG) in a repeater, that might do the job. It's only a matter of compatibility with the latest PW version.

Share this post


Link to post
Share on other sites
41 minutes ago, lenoir said:

It's only a matter of compatibility with the latest PW version.

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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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?

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Fanni
      Hi all, 
      I am having some issues uploading a PDF to our website (a problem we’ve had before).
      I have tried reducing the size of the PDF but the problem seems to be with generating the thumbnail image because the upload works under the Fallback mode but the thumbnail is terrible (see attached). 
      Does anybody have any advice about this? Please note that I'm not a developer. 
      Thanks!
       


    • By August
      Hi again, after trying pages2PDF, RockPDF and mpdf I'm not sure which of them I should use. Of course, there is also wirePDF.
      I merged mpdf with rockPDF, add a footer and a page in Landscape and that works, like in the code-snippet below but be am I on the right way?
      After that I tried a couple of times rendering an image. Nothing happend, the same with Pages2PDF too. I also copied an Image-URL within processwire and tried further getting an Image within the PDF-Document, but it still display only the red marked placeholder.
      I simply would like create an PDF on click from some Pages including the Images.
      Are there special requirements on Debian 10 with Apache and php 7.3 using Processwire 3.0.123.?
      Is there a guide that I didn't found?
      Is somebody out there who has a similar issue solved and would you be so kind to explain?
       
      $pdf = $modules->get('RockPdf'); $mpdf = $pdf->mpdf; # $mpdf->showImageErrors = true; $pdf->set('SetHeader', 'header text'); $mpdf->AddPage('L'); # $mpdf->Image(urlencode('site/assets/files/1845/image-2.275x200.jpg'), 0, 0, 210, 297, 'jpg', '', true, false); $pdf->set('SetFooter', ' <table width="100%"> <tr> <td width="33%">{DATE j.m.Y}</td>        <td width="33%" align="center">{PAGENO}/{nbpg}</td> <td width="33%" style="text-align: right;">whatever</td> </tr> </table>'); $pdf->show(); // generate pdf  
    • By tonicastillo
      Hi. Can I ask why this happens?
      When I upload a jpg, the meta keywords and description of the file loses the accents and spanish symbols of the words.
      Original image on the left, and the image generated by PW on resize on the right.

      This happens on resize. The uploaded file is ok.
      How can I control which meta keep in the photo?
      Thanks a lot (and sorry for my English)...
    • By PawelGIX
      Can I convert uploaded images to PNG, GIF, BMP -> JPEG?
      Is there any function in the API that I can change the file type?
    • By gregory
      Hi everyone.
      I can't see the PDF uploaded via a field called "pdf".
      I get a url like this: http://localhost:8888/mywebsite/site/assets/files/1129/test.pdf%EF%BB%BF%EF%BB%BF
      Could anyone help me? Thank you.
      <?php foreach($page->case_studies as $item) { if($item->type == 'contenuto') { echo " <a class='btn btn-primary btn-sm' target='blank' href='{$item->pdf->first()->url}' role='button'>Download</a> "; } } ?>  
×
×
  • Create New...