sujith3g / docxtemplater-link-module

:anchor: Hyperlink module for docxtemplater
https://www.npmjs.com/package/docxtemplater-link-module
MIT License
22 stars 15 forks source link

browser version of the link module #12

Open fuetti opened 8 years ago

fuetti commented 8 years ago

Hi there, I am using docxtemplater in browser and would like to create links in my documents as well. I found the link module but it seems there is no browser version of it?

I am not into node.js but tried to use browserify - but it still wont work.

Any help/ feedback appreciated.

fuetti commented 7 years ago

No feedback yet - I hope the module is still maintained?

I think it should not be a hughe effort to get a browser version of the module. If anyone could help here, it would be great! :)

sujith3g commented 7 years ago

A script similar to this would do.

fuetti commented 7 years ago

I am developingg on windows and do not have a linux/ bash. :-(

So I did "browserify index.js linkmodulebundle.js" where index.js is the index.js in the linkman root. No success so far :(

sujith3g commented 7 years ago

linkmodulebundle.js got generated ? I've got one generated can you check it bundle

fuetti commented 7 years ago

Sorry for the delay in reponse. I got your bundle - its 470KB? Its more than double size of docx templater itself.

How would I use it? The example I found looks like this:

var LinkModule = require('docxtemplater-link-module');
var linkModule = new LinkModule();
var doc = new Docxgen(content).attachModule(linkModule);

If I translate it to browser js, it would be (imho) :

var linkModule = new LinkModule();
var doc = new Docxgen(content).attachModule(linkModule);

But it says LinkModule is undefined. :-/

sujith3g commented 7 years ago

You can use this file, this works with docxtemplater v2.1.5 or below.

fuetti commented 7 years ago

Thank you! At least I get it to launch with this file! Now I ran into another problem - I am now getting an error during replace in docxtemplater. "Match not found in content"

I have overlooked my code several times and also adjusted the word template - it won't work with the {^mylink} but it works with {mylink} if I do not use the link functionallity.

Should I post my code?

sujith3g commented 7 years ago
<html>
    <script src="./docxtemplater.v2.1.5.js"></script>
    <script src="./file-saver.min.js"></script>
    <script src="./jszip-utils.js"></script>
    <script src="./docxtemplater-link-module.v0.2.3.js"></script>
    <!--
    Mandatory in IE 6, 7, 8 and 9.
    -->
    <!--[if IE]>
        <script type="text/javascript" src="examples/vendor/jszip-utils-ie.js"></script>
    <![endif]-->
    <script>
    var loadFile=function(url,callback){
        JSZipUtils.getBinaryContent(url,callback);
    }
    loadFile("./example-href.docx",function(err,content){
        if (err) { throw e};
        var linkModule = new LinkModule();
        doc=new Docxgen(content);
        doc.attachModule(linkModule);
        doc.setData( {
          link : {
            text : "dolor sit",
            url : "http://google.com"
          }
        }
        ) //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")
    })
    </script>
</html>

the above code works with example-href.docx. Use docxtemplater v2.1.1 ~ v2.1.5.

fuetti commented 7 years ago

I tried with your example and it worked. I also tried adding a loop (as I do in my project) and it worked also. But if I copy a part of my word and paste the {^link} tag there, where I want it... it fails.

InternalError: Match not found in content
docxtemplater-2.1.5.min.js (3,1025)

Attached the modified example:

var loadFile=function(url,callback){
        JSZipUtils.getBinaryContent(url,callback);
    }
    loadFile("./example-href.docx",function(err,content){
        if (err) { throw err;};
        var linkModule = new LinkModule();
        doc=new Docxgen(content);
        doc.attachModule(linkModule);
        var aloop = [];
        aloop.push({
                link : {
                    text : "dolor 2",
                    url : "http://google.com"
              }});
        aloop.push({
                link : {
                    text : "dolor 3",
                    url : "http://google.com"
              }});
        doc.setData( {
            link : {
                text : "dolor 1",
                url : "http://google.com"
              },
              aloop: aloop
        }); //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");
    })

example-href.docx

fuetti commented 7 years ago

Have you had a possibility to check the example? I now rebuild my docx and with carefully copy/pasting the placeholders and leaving space before and after, I got it to work. But Links are now not following the design (i.e. default font and font size) I have used for the placeholder... but well, it workes.