1

I would like to autogenerate a thumbnail of PDF's, and from this question I found the plugin PDFThumb. The problem I'm running into is I'm relatively new to Craft, and I can't seem to figure out how to use the provided code when it is with a Matrix field.

As I said I'm new to this, and I usually end up tying myself in knots when trying to backtrack the variable references, and I think that's the problem here.

The reference code from the plugin repo:

{% for myAsset in item.assetRef %}
    <img src="{{ craft.PDFThumb.thumbnail(myAsset) }}">
    <img src="{{ craft.PDFThumb.thumbnail(myAsset, 100, 100) }}">
    <img src="{{ craft.PDFThumb.thumbnail(myAsset, 100, 150, true, 'jpg') }}">
{% endfor %}

And my code:

{% set pdfBlocks = entry.infoPagePDF.all() %}
{% if pdfBlocks|length %}
        {% for block in pdfBlocks %}
            {% if block.type == "pdf" %}
                {% if block.pdfFile %}
                    {% set rel = block.pdfFile.one() %}
                    {% if rel %}
                        <a href="{{ rel.url }}">{{ rel.title }}</a>
                    {# Autogenerated Thumb #}
                        {% for myAsset in item.assetRef %}
                            <img src="{{ craft.PDFThumb.thumbnail(myAsset) }}">
                        {% endfor %}
                    {% endif %}
                {% endif %}
            {% endif %} 
       {% endfor %}
{% endif %}
                

The link generates fine, it's the image that doesn't.

I've tried a bunch of different variations, but the problem (I think) revolves around the fact I'm not sure what I'm supposed to be referencing in the FOR(myAsset and item.assetRef), so for clarity I reset my attempts to the provided code example.

2

You mixed in some code from the documentation resulting in a wrong variable name..

{% set pdfBlocks = entry.inhaltMatrix.all() %}
    {% if pdfBlocks|length %}
        {% for block in pdfBlocks %}
            {% if block.type == "pdf" %}
                {% if block.pdfFile %}
                    {% set rel = block.pdfFile.one() %}
                    {% if rel %}
                        <a href="{{ rel.url }}">{{ rel.title }}</a>
                        <img src="{{ craft.PDFThumb.thumbnail(rel) }}">
               
                    {% endif %}
                {% endif %}
            {% endif %} 
       {% endfor %}
{% endif %}

Also be sure to go to the plugins settings and set the appropriate assets paths.

3
  • Thanks, that helps me understand the variable, but it still doesn't work. The plugin creates the preview folder for the thumbs, but doesn't create the thumbs. I've double checked paths, permissions, and that ImageMagik is installed, but for whatever reason it won't create the thumbnail. Tried on MAMP and an Arcustech server. Feb 20 at 19:25
  • Does it generate an url for the image asset? Also double-check the volumes url and that the volume has public urls enabled.
    – MisterMike
    Feb 21 at 9:04
  • Yes, it generates the url. Public is enabled (the actual PDF links work fine). I'm stumped. Perhaps the plugin doesn't work well with 3.6.x? Feb 22 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.