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

browser version of the link module #12

Open
fuetti opened this issue Nov 17, 2016 · 10 comments
Open

browser version of the link module #12

fuetti opened this issue Nov 17, 2016 · 10 comments

Comments

@fuetti
Copy link

fuetti commented Nov 17, 2016

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
Copy link
Author

fuetti commented Nov 21, 2016

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
Copy link
Owner

sujith3g commented Nov 22, 2016

A script similar to this would do.

@fuetti
Copy link
Author

fuetti commented Nov 22, 2016

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
Copy link
Owner

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

@fuetti
Copy link
Author

fuetti commented Nov 28, 2016

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
Copy link
Owner

sujith3g commented Dec 3, 2016

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

@fuetti
Copy link
Author

fuetti commented Dec 5, 2016

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
Copy link
Owner

sujith3g commented Dec 5, 2016

<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
Copy link
Author

fuetti commented Dec 5, 2016

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
Copy link
Author

fuetti commented Dec 16, 2016

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.

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