Jump to content

Recommended Posts

PDF Fieldtype/Inputfield
 
Module for ProcessWire allowing you to easily generate thumbnails of the PDF files embedded to the site.
 
Current version: 1.1.2 (Changelog)

Module page: http://modules.processwire.com/modules/fieldtype-pdf
Github: https://github.com/uiii/ProcessWire-FieldtypePDF

For detailed instructions see: https://github.com/uiii/ProcessWire-FieldtypePDF/blob/master/README.md

  • Like 22

Share this post


Link to post
Share on other sites

Looks great!

A few suggestions if I may :)

I think you should note somewhere is the requirement for imagemagick, ghostscript, and the imagick extension. Also, do a class_exists check on imagick and fail with a nice error if it isn't available.

Also, you may want to add these lines to your code:

$imagick->setOption("pdf:use-cropbox","true");
$imagick->setColorspace(Imagick::COLORSPACE_RGB);

Also, if you find that the colors in the image are not matching those in the PDF, this gives instructions on the changes that need to be made to your imagemagick delegates file: http://www.lassosoft.com/CMYK-Colour-Matching-with-ImageMagick

Share this post


Link to post
Share on other sites

Great first post! ;)

Just quickly looking and reading. I think instead of creating new class with extending Pagefile you could add a method via hook to Pagefile

$this->addHook("Pagefile::thumbnail", $this, "thumbnail");

public function thumbnail($width, $height) {}

Not sure if that's better at all. Looks all ok, and may better extend with new Class... but just wanted to mention.

  • Like 1

Share this post


Link to post
Share on other sites

Hi, thanks for the replies. :-)
 

I think you should note somewhere is the requirement for imagemagick, ghostscript, and the imagick extension ...

 
 Yes, that's right. When and how is the proper way to print this error?
 

$imagick->setOption("pdf:use-cropbox","true");
$imagick->setColorspace(Imagick::COLORSPACE_RGB);
Also, if you find that the colors in the image are not matching those in the PDF, this gives instructions on the changes that need to be made to your imagemagick delegates file: http://www.lassosoft.com/CMYK-Colour-Matching-with-ImageMagick

 
I haven't much experience with ImageMagick. I've just found somewhere the code snippet of "pdf to image" conversion and use it. What consequences will have these two options? 
 
The "delegate" thing - I think it's not related to the module itself.
 

$this->addHook("Pagefile::thumbnail", $this, "thumbnail");

public function thumbnail($width, $height) {}

Hey, I didn't know that it's so simple :-). If it's better or not I don't know, maybe the class is more suitable for future module's extensions.

  • Like 1

Share this post


Link to post
Share on other sites

You can provide the error like this:

if(!class_exists('Imagick')) {
    $this->error(__("This module requires the php imagick extension, but it is not installed"));
}

You could use this in the install routine of the module.

I am not sure how to check for ghostscript. A hacky way would be to include a tiny PDF with your module and run the thumbnail generator on it. If the thumbnail is created, then it must be available. If not, return another error.

The cropbox setting is important because PDFs can have a crop applied to them and if you don't set this, the thumbnail you generate will show the entire contents of the page, not just what is visible in the PDF.

The colorspace_rgb is just to convert colors to RGB. The IM delegates settings I linked to are not something you can do with the module, but I think they are worth noting in your readme. Without those changes, some colors can turn out pretty fluorescent. Of course many thumbnails will look just fine depending on the colors used.

Just an FYI - I created a similar module a long time ago that I haven't ever got around to finalizing for release:

https://github.com/adrianbj/ProcessPDFImageCreator

The main difference with this one is that it creates large preview images for every page of the PDF and then relies on PW to create the thumbnails as requested from these large images. It's not beautifully coded, but you might find something of interest in there. The reasons I never released were because it can be slow to count the number of pages in a PDF (which I wanted to store) depending on what other libraries are available on the server, and also the generation of images for a document of a couple of hundred pages can take a long time and I never got around to setting up something so that this is done in the background after page save.

  • Like 2

Share this post


Link to post
Share on other sites

Fine, thanks. I'll update the module soon.
 

Just an FYI - I created a similar module a long time ago that I haven't ever got around to finalizing for release:
https://github.com/adrianbj/ProcessPDFImageCreator


Yes, I've found it and got a little inspiration. Originally I wanted to use it, but I don't like it requires exact, properly named fields to work. And conversion of the whole PDF isn't my need, just the first page thumbnail. But based on this a got an idea to add optional parameter to the thumbnail method specifing the page number to convert.

Share this post


Link to post
Share on other sites

Yes, I've found it and got a little inspiration. Originally I wanted to use it, but I don't like it requires exact, properly named fields to work. 

Yeah, I didn't know about how to make a configurable module back then :)

  • Like 1

Share this post


Link to post
Share on other sites

Hi and thanks for this nice module  :)

I'm having a bit of a problem with the thumbnail component of the module.  Sometimes when I create a thumbnail the resize creates extra noise in the image. Sometimes deleting the pdf from the page and re-uploading it removes the noise, so it doesn't seem to be an issue with the file itself. For now I'm working around it by commenting out the line in the module that resizes the image ($imagick->scaleImage($width, $height); from PagePDF.php) and using Image's size function. 

0wA5Ygs.jpg

Any help would be appreciated. 

Thanks

Share this post


Link to post
Share on other sites

Hi, thanks for your report. Sorry, but I don't have enough time now to solve the issue. If you can wait until mid-August I will look at that and try to replace the scale function with the ProcessWire's one.

Share this post


Link to post
Share on other sites

Richard are you still there? Haven't seen any activity on this as I filed couple issues on github some time ago. Thanks :D

Share this post


Link to post
Share on other sites

Hi, I'm still there, but I don't have much time to fix the issues. Sorry. If you find the solution you can create a pull request or make your version of the module. If I find some time a will try to look at that. Once again, sorry.

Share this post


Link to post
Share on other sites

I'm having huge problems with ImageMagick on my local setup.

I get the following error in the backend:

NoDecodeDelegateForThisImageFormat `C:/usr/local/domain.local/public_html/site/assets/files/1037/testfile.pdf' @ error/constitute.c/ReadImage/555

As I understand it, this has something to do with image formats. Doing a var_dump(Imagick::queryFormats()) yields an empty array.

I have the php_imagick.dll extension installed, along with the necessary binaries, all in my System PATH.

The only version that works with my PHP 5.5.11 (installed with XAMPP 1.8.3) is ImageMagick 6.8.8-4 Q16 x86. The Windows installer is the same version. Every other version causes PHP to skip loading the extension.

No errors in php_error_log.

Any ideas?

Update - If I run convert -list configure, I see that there is no pdf delegate listed. How do I install that? And why isn't it included with IM?

Update - seems this has more to do with GhostScript... I've always had it installed, so I don't know where the problem is.

Note - Have also asked on SO here: http://stackoverflow.com/questions/27382843

Share this post


Link to post
Share on other sites

Has anyone a hint how to get this awesome module to work if there is no imagick extension on the server but ImageMagick is available as executable? Could it be possible to write a wrapper for the excution of ImageMagick or am I missing something which is only available by the PHP Modul?


By the way ... I want to use it on a managed server from DomainFactory.

Share this post


Link to post
Share on other sites

You could definitely do this so long as you have exec available for PHP and also have imagemagick compiled with ghostscript. 

If these requirements are met, then something like this should work:

exec('convert -density 600 document.pdf[0] -colorspace RGB -resample 300 output.png');

inside the thumbnails function: https://github.com/uiii/ProcessWire-FieldtypePDF/blob/master/PagePDF.php#L7

Share this post


Link to post
Share on other sites

My shared hosting provider has asked me what the minimum version of GhostScript is for this to work. They also need to know if it should be a PHP module or the standalone binaries (added to PATH, I would assume).

Safe to say latest version and binaries?

Share this post


Link to post
Share on other sites

Hi Mike,

I'm not sure if there is some minimal version of ghostscript needed. I have installed ghostscript 9.15 with imagemagick 6.9.0 and according to phpinfo the php's imagick module is of version 3.1.2. In this configuration everything works fine.

No binaries are executed directly from php, the php's imagick module is used (imagick.so in php.ini), but you must have installed ghostscript and imagemagick as a system package.

Hope this helps you.


BTW: If you are going to use this module on PW 2.5, you will probably get some errors. I'm working on that and hope to finish it this week. PW 2.4 should be ok. 

Share this post


Link to post
Share on other sites

Hi Richard,

Seems right to me. Going to let them know now.

The module worked just fine on my 2.5 install - no problems. What errors have you been getting?

Share this post


Link to post
Share on other sites

Really, so you are lucky :-). There are some issues https://github.com/uiii/ProcessWire-FieldtypePDF/issues. Not all issues are fatal, but needs to fix. I have already fixed all of them but not published yet. I have also changed the API (still backward compatible). The new version isn't ready yet, I need to update the documentation and want to add some tests.

Share this post


Link to post
Share on other sites

Hehe, guess so. I originally used it in a development-site on 2.5.13, and have kept up to date since, now on 2.5.23 (current). The only problems I had (only on production server) were specifically about Imagick and GhostScript.

Will give the update a test when you release it. :-)

Share this post


Link to post
Share on other sites

Hi Richard

So GhostScript was installed on my shared server, but I'm getting this now:

 - PDFDelegateFailed `[ghostscript library 8.70] -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r300x300" -dUseCropBox -dFirstPage=1 -dLastPage=1 "-sOutputFile=/tmp/magick-11484_KMMlcXf5Ras%d" "-f/tmp/magick-11484_ZdfVOglomq7" "-f/tmp/magick-11484jAuPBFSKDl5M"':    **** Warning:  An error occurred while reading an XREF table.
   **** The file has been damaged.  This may have been caused
   **** by a problem while converting or transfering the file.
   **** Ghostscript will attempt to recover the data.
   **** Page has no /MediaBox attribute. Using the current page size.
Error: /stackunderflow in --run--
Operand stack:
   --dict:4/4(L)--   612.0   792.0   3
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1862   1   3   %oparray_pop   1861   1   3   %oparray_pop   1845   1   3   %oparray_pop   --nostringval--   --nostringval--   2   1   1   --nostringval--   %for_pos_int_continue   --nostringval--   --nostringval--
Dictionary stack:
   --dict:1157/1684(ro)(G)--   --dict:1/20(G)--   --dict:75/200(L)--   --dict:75/200(L)--   --dict:106/127(ro)(G)--   --dict:286/300(ro)(G)--   --dict:23/25(L)--   --dict:2/6(L)--
Current allocation mode is local
GPL Ghostscript 8.70: Unrecoverable error, exit code 1
 @ error/pdf.c/InvokePDFDelegate/271

Have you seen this before?

Update: Seems that the error is only appearing for one PDF file. I'll need to convert it or something to get it to work. Anyways, I also got an error saying "Field "certificate_pdf" is not yet ready to use and needs to be configured." I don't quite understand what needs to be configured...

Update: Just saw this - need to upgrade GS.

Share this post


Link to post
Share on other sites

Hi Mike,

I haven't seen this GS error before. I'm not an expert in Ghostscript nor ImageMagick, I have just go through many internet discussions and found working php code for pdf to image conversion. But as you say, it looks like a GS bug.

I also got an error saying "Field "certificate_pdf" is not yet ready to use and needs to be configured." I don't quite understand what needs to be configured...

This is one of the issues in PW 2.5 as I mentioned earlier. See https://github.com/uiii/ProcessWire-FieldtypePDF/issues/6. If you want, you can temporarily use  somatonic's fork or wait for my upgrade. 

Share this post


Link to post
Share on other sites

Hi Richard

I've asked support to upgrade GS to 9.x.

Would rather wait for your upgrade to come out. The site in questions is in need of some layout changes, so getting this working isn't an urgent matter.

:-)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • 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 gebeer
      Although the PW backend is really intuitive, ever so often my clients need some assistance. Be it they are not so tech savvy or they are not working in the backend often.
      For those cases it is nice to make some help videos available to editors. This is what this module does.
      ProcessHelpVideos Module
      A Process module to display help videos for the ProcessWire CMS. It can be used to make help videos (screencasts) available to content editors.
      This module adds a 'Help Videos" section to the ProcessWire backend. The help videos are accessible through an automatically created page in the Admin page tree. You can add your help videos as pages in the page tree. The module adds a hidden page to the page tree that acts as parent page for the help video pages. All necessary fields and templates will be installed automatically. If there are already a CKEditor field and/or a file field for mp4 files installed in the system, the module will use those. Otherwise it will create the necessary fields. Also the necessary templates for the parent help videos page and it's children are created on module install. The module installs a permission process-helpvideos. Every user role that should have access to the help video section, needs this permission. I use the help video approach on quite a few production sites. It is stable so far and well received by site owners/editors. Up until now I installed required fields, templates and pages manually and then added the module. Now I added all this logic to the install method of the module and it should be ready to share.
      The module and further description on how to use it is available on github: https://github.com/gebeer/ProcessHelpVideos
      If you like to give it a try, I am happy to receive your comments/suggestions here.
    • By Robin S
      A module created in response to the topic here:
      Page List Select Multiple Quickly
      Modifies PageListSelectMultiple to allow you to select multiple pages without the tree closing every time you select a page.
      The screencast says it all:

       
      https://github.com/Toutouwai/PageListSelectMultipleQuickly
      https://modules.processwire.com/modules/page-list-select-multiple-quickly/
    • By gebeer
      Hello all,
      sharing my new module FieldtypeImagePicker. It provides a configurable input field for choosing any type of image from a predefined folder.
      The need for it came up because a client had a custom SVG icon set and I wanted the editors to be able to choose an icon in the page editor.
      It can also be used to offer a choice of images that are used site-wide without having to upload them to individual pages.
      There are no image manipulation methods like with the native PW image field.
      Module and full description can be found on github https://github.com/gebeer/FieldtypeImagePicker
      Kudos to @Martijn Geerts. I used his module FieldTypeSelectFile as a base to build upon.
      Here's how the input field looks like in the page editor:

      Hope it can be of use to someone.
      If you like to give it a try, I'm happy to hear your comments or suggestions for improvement. Eventually this will go in the module directory soon, too.
    • By bernhard
      @Sergio asked about the pdf creation process in the showcase thread about my 360° feedback/survey tool and so I went ahead and set my little pdf helper module to public.
      Description from PW Weekly:
       
      Modules Directory: https://modules.processwire.com/modules/rock-pdf/
      Download & Docs: https://github.com/BernhardBaumrock/RockPDF
       
      You can combine it easily with RockReplacer: 
      See also a little showcase of the RockPdf module in this thread:
       
×
×
  • Create New...