Jump to content

PDF Fieldtype/Inputfield


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

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

Link to comment
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
Link to comment
Share on other sites

  • 2 weeks later...
  • 1 month later...

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

Link to comment
Share on other sites

  • 3 months later...
  • 1 month later...

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

Link to comment
Share on other sites

  • 2 months later...

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.

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

Link to comment
Share on other sites

  • 1 month later...

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. 

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

Link to comment
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. :-)

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

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

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
×
×
  • Create New...