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
as of the time of writing this, it actually works!
it allows you to use the advanced Angular parser capability of docxtemplater, too.
there is some error handling in place to let users know what went wrong and why.
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’.
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.
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
@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.
→ 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.
Hello, @TomNit I launch docxtemplater, work fine, thank you for your great work.
Please specify the code if you have time:
# First off, you might want to make sure the placeholder data gets updated whenever
# the user makes changes to the template. To do this, reference the template
# attachment column explicitly:
depend = [
$TPL_Name,
$tpl_id,
$DOCX_Name,
# You could add more columns here. Note in particular that the placeholder
# mapping will *not* get updated automatically if you change a column in the
# source record. This is because the formula in $helper uses PEEK() to get
# the column values, and therefore doesn't set up any dependencies.
]
# To make a placeholder mapping for this record itself, do 'record = rec' instead.
record = rec
# Create the mapping and return it.
return $helper.create_placeholder_mapping(record)
It seems to me that the record your passing to create_placeholder_mapping is the very same record that create_placeholder_mapping is defined in. This leads to recursion trouble. You might try moving create_placeholder_mapping to a different table. Does that help?
Thank you for your explanation, I understand my mistake:
I was trying to make a single template for multiple data sources (1:M)
In the plugin process, you need one template for one source (1:1)
If I haven’t messed up again…