On my site, I want to be able to display a JPG or PNG thumbnail of PDF documents. In the past I have used imagecache_actions to do this, by writing some code for a custom action. I then switched to using PDF Preview however it doesn't seem that there are any plans to producing a D8 version of this so I wanted to switch back to a custom action. However, after installing Imagemagick and Image Effects, I can't see any sign of a "custom action" in the options. Has this disappeared? Or is there any other way to produce a thumbnail from a PDF?

Comments

joel_guesclin created an issue. See original summary.

mondrake’s picture

The 'custom actions' effect has not been ported to this module, yet. Patches welcome!

But for your specific use case "display a JPG or PNG thumbnail of PDF documents", you can

  • Enable PDF format in the ImageMagick toolkit via the configuration UI (see ImageMagick's README.txt for more info);
  • Add a "convert" image effect to the style you use to produce the thumbnail, to set the derivative image format to JPEG or PNG.
joel_guesclin’s picture

Thanks for your help - I confess I find the whole image management very confusing at first (even more than on D7 tobe honest!). Here's what I've done (on an otherwise empty test site on my Mac):
Installed the Imagemagick module and activated it
1) Selected Imagemagick as the preferred toolkit
2) Changed the Imagemagick parameters so that it accepts PDF
3) Created an Image style with a "Convert" effect. I can see that PDF shows up in the list of possible conversions, so that seems ok. However, what is confusing is that there is no option to say what you want to convert to. I was expecting to see a "convert from" and "convert to" option. Or is it that the Convert already knows what it is converting from so you should only say what you want to convert to?
At all events, there is a problem here because when I save the Image Style I get this message:
The specified file public://styles/pdf_view/public/core/modules/image/sample.png.pdf could not be moved/copied because no file by that name exists. Please check that you supplied the correct filename.
I get the same error message no matter what type I select in the "Convert"
4) Anyway, having done all that I created a content type with two fields: an "Image" field and a "File" field. The Image field is configured to allow PDF as a file type as well as the other types. Now I find two strange things:
a) Although I can add "pdf" as a type to an Image field, it doesn't make any difference: I still can't upload a PDF to it
b) I can't apply an Image style to the file type field
5) So I thought I would try using File Entity. I'm not quite sure exactly what this does and how it works, but the main thing is that it allows me to display the File Field as "rendered entity" and in the File Type settings, I can now set the Image style I created as a rendering for the Document file type - which seems to include application/pdf
However, when I create a piece of content and load a PDF file, the image does not display and I get this error message in the reports:
/sites/d82.dd/files/styles/pdf_view/public/2016-10/Bruxelles_160322.pdf.png?itok=K1l8lMfD

So I'm still a bit stuck and not sure whether the problem is in Imagemagick, Image Effects, Core Image Styles, or something I've not understood...

martin.knapp’s picture

There's another question which is not clear to me: usually a PDF contains more than one page, and I believe that unless you specify that you want the first page, then Imagemagick will produce JPEGs (or whatever) of all the pages, which is not really what is wanted.

fietserwin’s picture

RE #3.3) There's only a "convert to", as all images of all types will be converted. The error message indicates that saving to pdf (by imagemagick) failed.

4a) I wouldn't be surprised if the list of allowed extensions is still hardcoded somewhere in core (image field) instead of taken from the actually enabled image toolkit. OTOH, image fields do not necessarily use the image toolkit, it depends on (the settings of) the display formatters used for that field and, optionally, if there's a resolution restriction (leading to resize on upload).

IIRC, in D7 there's a module to define the allowed image types per field. Not sure about D8.

4b) That is correct, it is image fields only, though there are ideas (ie an open issue) to widen the scope of image styles to file fields as well.

5) What is the error here? I only see a derivative file name (with token) in the message text.

joel_guesclin’s picture

Problem sorted, I think, or at least partly. I'm using a copy of a site running under Centos, on my workstation which is an iMac. I kept getting confused by the ImageMagick settings when I switched from one to the other because on Centos Imagemagick installs to /usr/local/bin whereas on OSX it installs to /opt/local/bin - so I set up a symbolic link for "convert" but hadn't realised I would need one also for "identify". Having done that, I manage to get the PDF to show up. Cool! Also the error on setting up the styles has gone away.
Better still, it seems to handle multiple-page PDFs since looking in the "styles" folder there is only a single-page png file. Fantastic! And many thanks!

The slightly weird thing is that you can't apply an Image Style directly to a File field, but you can apply it to the File Field if you go via the "rendered entity" setting once the File Entity module is installed. This is a rather roundabout way of doing things it seems to me, but it does appear to work.
You're probably right about the file types being hardcoded in the Image field - which also raises questions about handling SVG: but that's another story altogether!

fietserwin’s picture

Title: Where have "custom actions" gone? » Convert pdf to jpg
Status: Active » Closed (works as designed)

Closing this support request as the problem has been solved. Porting the custom action should still be done but in a separate task issue.

mondrake’s picture

#5:

I wouldn't be surprised if the list of allowed extensions is still hardcoded somewhere in core (image field) instead of taken from the actually enabled image toolkit

#6:

You're probably right about the file types being hardcoded in the Image field - which also raises questions about handling SVG: but that's another story altogether!

That's true... see #1014816: Allow image fields to use any extensions the current image toolkit supports (instead of hard-coding jpg, png and gif only) for extensive discussion. Help there appreciated :)

mondrake’s picture

#4:

There's another question which is not clear to me: usually a PDF contains more than one page, and I believe that unless you specify that you want the first page, then Imagemagick will produce JPEGs (or whatever) of all the pages, which is not really what is wanted.

When ImageMagick (the D8 version of the module, mind) is converting FROM a multi-frame image file (multipage PDFs, but also e.g. Animated GIFs) TO any other format, only the FIRST frame is converted - as @joel_guesclin found in #6.

mondrake’s picture

Project: Image Effects » ImageMagick

Changing issue queue to ImageMagick as this (with this issue title) belongs to there and not to Image Effects.

mondrake’s picture

#6:

You're probably right about the file types being hardcoded in the Image field - which also raises questions about handling SVG: but that's another story altogether!

See also #2674818: SVG's instead of png, gif, jpg, jpeg

joel_guesclin’s picture

Can I add a point to this though (or two points actually).
First of all, thanks to everyone for taking the time on it.
Second, something I noticed when trying this out using a 36-page brochure in PDF format for testing. Whereas a small (3-4 page) PDF gets converted almost immediately, the 36-page one takes a good deal longer (really noticeably longer).
When I tried this out using imagemagick on the command line, I saw that unless you specify that only page 1 is to be included, then it does take a lot longer, so like this:
convert my-brochure.pdf my-brochure.png
produces 36 png files and takes quite along time, whereas
convert my-brochure.pdf[0] my-brochure.png
produces only one file and is almost instantaneous
So, my question: is the ImageMagick module producing 36 pages and discarding all but the first, and if not why does it take so long?
Third point, what I would like to do is to link the PNG image to the original file (ie clicking on the thumbnail opens the PDF). This used to work using PDF Preview module but here I simply don't get the option - however I don't know where I should ask the question: here? or in the File Entity module which I suppose is what handles this.

mondrake’s picture

@joel_guesclin the points were 3 in fact :)

2. When the ImageMagick toolkit is converting from a multipage PDF to a png, only the first page is converted, exactly using the [0] notation. You can see that if you click on the 'Display debugging information' in the image toolkit configuration form. You will get debug messages in the dblog and on the page with the actual commands executed by the toolkit. Can't say why it's taking long.

3. This does not belong here, it's about Image field formatting. There's no option to link to the original image in core's Image formatter, and I do not know if there's any issue in core to request such a possibility. File one in core's issue queue if you believe. The Textimage module provides an alternative formatter for Image fields, that does allow to link to the original image. However, just note it has a totally different engine to build image derivatives.

danthorne’s picture

@mondrake Is there anyway to get it to convert ALL the pages into images?