A little addition to the blog post : http://www.knackhq.com/links/blogs/dynamic-pdfs-and-documents-with-webmerge.html
Here is how you can merge subtable fields and create nice templates for your invoices, quotations etc. in webmerge and update your data base while generating a merged document in 1 click.
The example below shows how to merge the invoice details and invoices lines and generate the merge using a Knack submit button.
1) Ids and mapping.
All the information that my merge needs are contained in scene_20 ('scene' is knack's former version's name of 'page') which has 2 views : view_24 containing the invoice header and view_25 which contains the invoice lines.
the "submit" button is in view_29 which is a edit form of invoice without any field.
I want to merge the following fields in my invoice header from view_24:
Client : field _92
Clientemail : field_100
Invoicedate : field_93
invoicenumber : field_91
and the following fields from table view_25
Product: field_96
Price: field_97
Quantity: field_98
Total:field_99
Each field is defined in the webmerge document which url is : https://www.webmerge.me/merge/21325/ian1l1 (needed for the code below)
2) Code
In Knack's API and code / Javascript :
$(document).on('knack-scene-render.scene_20', function(event, scene) {// link hander: send invoice to webmerge
$(’#view_29’).submit(function(event) {event.preventDefault(); // get data var data_view_24 = Knack.models['view_24'].toJSON(); var data_view_25 = Knack.models['view_25'].data.toJSON(); url = 'https://www.webmerge.me/merge/21325/ian1l1?_use_get=1&download=1'; url += '&Client=' + encodeURI(data_view_24.field_92_raw); url += '&Clientemail=' + encodeURI(data_view_24.field_100_raw.email); url += '&invoicedate=' + encodeURI(data_view_24.field_93_raw.date); url += '&invoicenumber=' + encodeURI(data_view_24.field_91_raw); for(x = 0; x < data_view_25.length; x++){ item = data_view_25[x]; url += '&items[' + x + '][product]=' + encodeURI(item.field_96_raw); url += '&items[' + x + '][price]=' + encodeURI(item.field_97_raw); url += '&items[' + x + '][quantity]=' + encodeURI(item.field_98_raw); url += '&items[' + x + '][total]=' + encodeURI(item.field_99_raw); } window.location = url;
});
});
This code will merge and download the invoice.
Given that the knack submit button is used to trigger the event download, in form option do not use any redirect option or it will overwrite the dowload instruction.
You can add submit rules the usual way in the interface builder to the submit button which will be triggered together with the merge, in order that you update your data base and get a merged document in a single click!
In case you have doubt just contact the webmerge, they have fantastic customer support team