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.
5 Likes
@lasalesi ,
Ah great !!
I haven’t tried it yet, but that looks like just the thing to do it.
Thanks
Ben
lasalesi:
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.
Finding this in 2021… and this is great, except how to handle displaying pdf attachments?
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.
harsha
November 22, 2021, 12:45pm
6
This is super useful @lasalesi .
I used this code in custom Html and was able to print all the attachments along with custom fields.
Thanks a lot !!
Any other method to append pdf to print format?
Anyone an idea about doing this with pdfs?
Usecase: Within a printed work order summary we want to attach the customers drawing.
Tried following code without success:
<embed src="/files/xy.pdf" width="100%" height="100%" type="application/pdf" />
or
<iframe src="/files/xy.pdf" width="100%" height="100%"></iframe>
or
<object data="/files/xy.pdf" type="application/pdf" width="100%" height="100%"> </object>
Many thanks in advance.