Displaying attachments in print format (jinja)

I am trying to display attachments (images) in a print format.

I am wondering whether something like this is possible? But, I can’t find the list of attachments linked to the Doctype.

{%- for row in doc.attachments -%}

	<img src= {{ row }}

{%- endfor -%}

I know I can do it easily by creating additional fields. But, then the print format isn’t dynamic.

Hi @bglazier,

you could use a lookup function like this:

{% set files = frappe.get_all('File', filters={'attached_to_doctype': 'User', 'attached_to_name': doc.name}, fields=['file_name', 'file_url']) %}
{%- for file in files -%}
   <img src="{{ file.file_url}}" alt="{{ file.file_name }}" />
{%- endfor -%} 

The example is based on the user doctype. If your names are unique over all tables, it might be ok to used only the attached_to_name attribute (then it is fully generic). Maybe add a check for file extension, but that is straightforward.

Hope this helps.

4 Likes

@lasalesi,

Ah great !!

I haven’t tried it yet, but that looks like just the thing to do it.

Thanks

Ben

Finding this in 2021… and this is great, except how to handle displaying pdf attachments?

image

Ah I see… you have to use embed … but this doesnt work exactly either…

<embed src="{{ file.file_url}}" alt="{{ file.file_name }}" width="100%" height="100%" />

Since it displays the pdf with scrollbars.

This is super useful @lasalesi. :clap::clap:

I used this code in custom Html and was able to print all the attachments along with custom fields.
Thanks a lot !!