open-xml-templating/docxtemplater

DocxGen is not defined but compiled with -s

tinokl opened this issue · 8 comments

Hello!

Currently i am juggeling around to get this working and now i have no clue left what to try.
I read the old issue where the compiled version with -s solved it but sowehow this doesnt work for me.

So here is the problem test page: http://ciubic.at/word/

Error message:
(first there appears the GET as desired then error)
ReferenceError: DocxGen is not defined
doc = DocxGen(content)

What i did (everything with windows)
1: compiled like described for the browser part (no error)
(browserify -r ./js/docxgen.js -s Docxgen > build/docxgen.js)
2: used it like so on my test page:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>click demo</title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="docxgen.min.js" type="text/javascript"></script>
</head>
<body>

<h1>Templates</h1>
<button id="save_btn">Speichern</button>

</body>
<script src="angular-expressions.js"></script>
<script src="jszip-utils.js"></script>
<script src="FileSaver.min.js"></script>
<script src="./downloadify/js/downloadify.min.js"></script>
<script src="./downloadify/js/swfobject.js"></script>

<script>

loadFile=function(url,callback){
    JSZipUtils.getBinaryContent(url,function(err,data){
        callback(null,data)
    });
}

$( "#save_btn" ).click(function(event) {
    event.preventDefault();

                loadFile("in.docx",function(err,content){

                doc = DocxGen(content)
                doc.setData( {"first_name":"Hipp",
                    "last_name":"Edgar",
                    "phone":"0652455478",
                    "description":"New Website"
                    }
                ) //set the templateVariables
                doc.render() //apply them (replace all occurences of {first_name} by Hipp, ...)
                out=doc.getZip().generate({type:"blob"}) //Output the document using Data-URI
                saveAs(out,"word.docx")
            })

});
</script>
</html>

3: tried the minimal version also
4: tried some required statements like:
DocxGen=require("docxgen.js")

no success :/
What i am doing wrong?
Thanks in advance!

Shouldnt it be Docxgen (not DocxGen) ?

Otherwise I can't see the html code. Can you post it by escaping it with triple backtick ? like this

Sry i forgot to escape the code xD
I tried "Docxgen" but this didn't solved the problem, i just used your code on the demo page just with an different file as input:

            loadFile("tagExample.docx",function(err,content){
                doc=new DocxGen(content)
                doc.setData( {"first_name":"Hipp",
                    "last_name":"Edgar",
                    "phone":"0652455478",
                    "description":"New Website"
                    }
                ) //set the templateVariables
                doc.render() //apply them (replace all occurences of {first_name} by Hipp, ...)
                out=doc.getZip().generate({type:"blob"}) //Output the document using Data-URI
                saveAs(out,"output.docx")
            })

one interessting thing was that when i additionaly include your script file "http://javascript-ninja.fr/docxtemplater/v1/browser/main.min.js" you used on the demo page then it worked.
Didn't know why though? (but this doesnt seem to be the proper solution^^)
thanks for the quick response!

After looking into http://ciubic.at/word/docxgen.js, I think the exported code is docxgen

How can that be, i did not changed anywhere the code?
You are right, now the error is gone but a new one appeared:

TypeError: this.setOptions is not a function
this.setOptions({});

Is there something wrong compiled?

This time it is because you forgot to write the new keyword.

Doc = new DocxGen()

Thank you! Now it works ;)

I would guess you're on a Mac, and that is why they was a problem with the casing. See browserify/browserify#1104

Just to note, i used windows 8.1 on lenovo thinkpad :)
and by the way nice job with that library!