jinja2docassembledocxtpl

Rescaling images in Docassemble docx template


Usually, like described here, images uploaded into DocAssemble (DA) can be put into the template with the corresponding Jinja2 tag defined in the DA YAML file like:

question: |
  Please upload a picture of yourself.
fields:
  - Picture: user_picture
    datatype: file

If {{ user_picture }} is written somewhere in the docx template, the image will be put there. (see doc)

But how can I influence the size of the image in the later docx file?

There are no Jinja2 filters that are applicable. The only solution I found was that DocAssemble uses the package docxtpl and in the documentation there is a method for replacing docx images:

tpl.replace_pic('dummy_header_pic.jpg','header_pic_i_want.jpg')

But I don't find the right way to call this method from within the DA YAML file. What is best practice to put images with a definded width and height into a docx template with DocAssemble?


Solution

  • You can write {{ user_picture.show(width="2in") }}. For more information about the .show() method, see the documentation for DAFile. This also works in the context of displaying images on the screen.