Report printing using DOCX template - Custom widget (new version)

Hi everyone,

I made a new version of @stan-donarise’s excellent docxtemplater widget. It allows you to attach a DOCX template to your Grist document that includes placeholders like ‘{last_name}’, then replace those with the actual data at the press of a button and have the resulting file offered to you for download. Because docxtemplater also handles conditions, loops, nested tables, and many other things, there are a lot of possibilities. Check out the documentation of docxtemplater for more info.

This new version of the widget is significantly improved in that

  1. as of the time of writing this, it actually works! :slight_smile:
  2. it allows you to use the advanced Angular parser capability of docxtemplater, too.
  3. there is some error handling in place to let users know what went wrong and why.

You can try out a working version of the widget here: https://docs.getgrist.com/t8GrTQaQ2CvF/showcase/p/1.

The source code is located at https://github.com/tomnitschke/gristwidgets/tree/main/docxtemplater.

To include the widget in your project, make a custom widget and set its URL to: https://tomnitschke.github.io/gristwidgets/docxtemplater/. Then map columns and you’re good to go.
However, generating the placeholder-to-data mapping from your Grist record isn’t trivial, so I threw together an example of how to do it. You might want to check out this “ready to copy and paste” solution here.

Enjoy!

6 Likes

Update: The widget can now also handle images. You can embed either images from an external URL (though this will only work if CORS allows it) or coming from your Grist attachments. I’ve documented the process on Github as best I could, see the source code link above generally and in particular under ‘Inserting Images from Grist attachments’.

Hope it helps someone.

Forgot to put the demo document into fiddle mode. Here’s the updated link to the demo.

Hello @TomNit ,

For information, I looked at your demo but actually I do not get image in the final downloaded .docx …
Sorry I had not time to test further for the moment.

@Sylvain_Page That’s strange. There should be two images in the document, the Grist logo and a random meme from Imgur. Are both missing? If it’s just the meme that’s missing, this happens if you open the document in Word’s “read only” mode (which is the default for downloaded files, afaik). Turn that off and the image should appear.

Let me know how it goes!

I get no image at all … (no grist logo).
The docx is downloaded normally.

  • When I open with word it says it is in safe mode read only → I accept modification and risk, it is refreshing but nothing more, no image
  • I relaunched the process, then it property on the fresh downloaded file without opening, I unlocked it directly from propoerties and open, no warning but no image neither :face_with_raised_eyebrow:

@Sylvain_Page Hmm, just to check: Perhaps you’re trying with ‘example1.docx’? That doesn’t contain any images, you need to process template ‘example2.docx’ instead. I might have mentioned that before, sorry.

Hi @TomNit :+1:

→ Perfect my bad I’ve not seen, just wanted to try out shortly the update and did not check along example.
That’s nice !
I’ll give a trial on my personnal hosting hoping to pass trhough cors (I’m a bit pessimistic) but that’s another topic.

Thanks !

@Sylvain_Page Glad it works for you now, you’re welcome!