Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to do multiple pages repeating the same template with docxtemplater? #615

Closed
Coronelpanter opened this issue Aug 24, 2021 · 1 comment

Comments

@Coronelpanter
Copy link

Coronelpanter commented Aug 24, 2021

Environment

  • Version of docxtemplater : 3.22.8
  • Used docxtemplater-modules :
  • Runner : Browser

How to reproduce my problem :

My template is the following : https://docxtemplater.com/tag-example.docx

With the following js file :

<html>
    <body>
        <button onclick="generate()">Generate document</button>
    </body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/docxtemplater/3.22.8/docxtemplater.js"></script>
    <script src="https://unpkg.com/[email protected]/dist/pizzip.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.js"></script>
    <script src="https://unpkg.com/[email protected]/dist/pizzip-utils.js"></script>
    <script>
    function loadFile(url,callback){
        PizZipUtils.getBinaryContent(url,callback);
    }
    function generate() {
        loadFile("https://docxtemplater.com/tag-example.docx",function(error,content){
            if (error) { throw error };
            function replaceErrors(key, value) {
                if (value instanceof Error) {
                    return Object.getOwnPropertyNames(value).reduce(function(error, key) {
                        error[key] = value[key];
                        return error;
                    }, {});
                }
                return value;
            }
            function errorHandler(error) {
                console.log(JSON.stringify({error: error}, replaceErrors));

                if (error.properties && error.properties.errors instanceof Array) {
                    const errorMessages = error.properties.errors.map(function (error) {
                        return error.properties.explanation;
                    }).join("\n");
                    console.log('errorMessages', errorMessages);
                }
                throw error;
            }

            var zip = new PizZip(content);
            var doc;
            try {
                doc = new window.docxtemplater(zip, { paragraphLoop: true, linebreaks: true });
            } catch(error) {
                errorHandler(error);
            }
            doc.setData({first_name: 'John',last_name: 'Doe',phone: '0652455478',description: 'New Website'});
            try {
                doc.render();
            }
            catch (error) {
                errorHandler(error);
            }
            var out=doc.getZip().generate({
                type:"blob",
                mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
            });
            saveAs(out, "output.docx");
        })
    }
    </script>
</html>

Hi, I want to do multiple pages repeating the same template in different pages with different datas in each page, I was looking for somethig like that in the issues and docs, but only found how to do multiples files and save it in a zip, i hope can you help me, thanks a lot.

@edi9999
Copy link
Member

edi9999 commented Aug 25, 2021

Hello @Coronelpanter , I think this issue is the same as : #182

There are multiple solutions suggested in that issue.

@edi9999 edi9999 closed this as completed Aug 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants