open-xml-templating/docxtemplater

multi-label docx from single-label template in static pages hosting

abubelinha opened this issue ยท 7 comments

Thanks for this nice software.

Sorry but most of the issues I see and also the docs are quite advanced stuff.
Before diving into them, I basically want to ask if this is possible at all.
Maybe there is already a example about this somewhere.

I want to use a .docx template for creating the typical "name, surname, address, phone, email" labels (i.e. 20-25 labels fitting in each page, so I ony waste 4-5 paper pages to print 100 labels).

Data would be filled-in by using a html form and passed to a javascript array:

data = [
   {"name":"John", "surname":"Doe", 
    "address":"Central Street, 123", 
    "phone":876543210, 
    "email":"jonh@johndoe.com"}, 
   {...}, 
    ...
]

My .docx template (stored in a static pages server without nodejs) woud have a section to be filled in with those data.
So my code should repeat that section (and fill it in) for each of the array members to generate the final .docx

Can you point me to an example (or docs) of such a repetitive template job?
I have already tested your example of how to run docxtemplater from browser, but that's for filling in just once.
Don't know how to adapt it to what I want to do.

Thanks a lot for your work and time
@abubelinha

Hello @abubelinha ,

thanks for filing this issue.

Your data looks something like this :

doc.render({ users: [
       {
            "name":"John",
            "surname":"Doe", 
            "address":"Central Street, 123", 
            "phone":876543210, 
            "email":"jonh@johndoe.com"
       }, 
       {
            "name":"Mary",
            "surname":"Fing", 
            "address":"Baker Street, 6", 
            "phone":2302148124, 
            "email":"mary@fing.com"
       }, 
       {
            "name":"Sean",
            "surname":"Paul", 
            "address":"Other Street, 6", 
            "phone":49539259235, 
            "email":"sean-paul@gmail.com"
       }, 
    ]
});

I couldn't really understand what kind of output you want to achieve.

  1. Could you please send me some image preview or docx file of the kind of document that you want to generate ?

  2. Does your data look like what I wrote above, with about 20 users ?

Thanks for the immediate reply!

  1. I send both a template and the output (I just covered a couple of fields in 1st label)
  2. Yes, exactly. Well, many more users: I want to output 20 or 25 per page
    ... but my data still does not exist. That's how I figured out things could work ... if you agree.

docxtemplater_25-in-A4_template.docx
docxtemplater_25-in-A4_output.docx

Probably the fields in this template (which I used in the past with Python) are not yet in the format required by docxtemplater (I will have to read the documentation, of course).
I just show them as an example of what I am trying to replicate in a static web server.

As for data collection, I planned to design some kind of web form containgin an editable table (perhaps using datatables or any other js library for managing editable html tables). Another option would be pasting from excel, csv, or whatever ... inside a textarea.

name surname address email phone
John Doe Central Street, 123 876543210 john@johndoe.com
... ... ... ... ...
... ... ... ... ...
... ... ... ... ...

Then, using javascript, those data should be converted (to whatever array format you tell me) and passed to docxtemplater in order to produce the desired .docx output.

I guess "follow loops example" is the answer I was expecting, but thanks anyway.
๐Ÿ‘ ๐Ÿ‘ ๐Ÿ‘ such a great js tool !!

Side note: Just in case anyone tries to use github pages for hosting both docxtemplater .html tests and .docx templates and gets a CORS error, remember to link your templates using github.io urls (althought they are also available from github.com using raw links) ... since your .html pages will be served from github.io domain.

Hello @abubelinha .

I'm not sure your output :

Selection_001

can be achieved using the open-source part only.

In this case, I would advise to use the table module which adds the possibility to do such a thing in a grid :

See this demo for an example :

https://docxtemplater.com/demo/#grid-table-image

You can set the size of your grid (in the demo sample, it is 2*4, but can be done with other numbers).

Thanks @edi9999 I am surprised you say I can't.
I actually closed the issue because I achieved the output I was trying. More or less.

It is true that I have no great control of the number of labels (i.e. if the "address" contents are too long in one particular item, it could extend for several lines and then its label gets higher .... so it might not fit in the place intended for that).

But I still haven't dive into all docxtemplater options (probably there are "if" control structures that I could try to use to avoid that).

Oh I see that you're using multiple columns ! That's a way to make it work indeed !