Giter Site home page Giter Site logo

docxtemplater-build's People

Contributors

dependabot[bot] avatar edi9999 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

docxtemplater-build's Issues

Docxtemplater is not defined ( browser )

Hi,
I'm using the below code , but I got this error : Docxtemplater is not defined.

<html>
    <script src="js/docxtemplater.min.js"></script>
    <script src="js/FileSaver.min.js"></script>
    <script src="js/jszip-utils.min.js"></script>
    <!--
    Mandatory in IE 6, 7, 8 and 9.
    -->
    <!--[if IE]>
        <script type="text/javascript" src="js/jszip-utils-ie.js"></script>
    <![endif]-->
    <script>
    var loadFile=function(url,callback){
        JSZipUtils.getBinaryContent(url,callback);
    }
    loadFile("sample.pptx",function(err,content){
        if (err) { throw e};
        doc=new Docxtemplater(content);
        doc.setOptions({fileType: "pptx"});
        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")
    })
    </script>
</html>

what's wrong here ?

Site crashes

Hi,

The things I know are:

  • My site crashes because of this loop in the docxtemplater.
  • The contentLength is about 140'000 and cursor is 0 in the start.
  • It doesn't execute totalMatches.push because it crashes before.

docxtemplater code:

while (cursor < contentLength) {
      cursor = content.indexOf("<", cursor);
      if (cursor === -1) {
        break;
      }
      var offset = cursor;
      cursor = content.indexOf(">", cursor);
      var tagText = content.slice(offset, cursor + 1);
  
      var _getTag = getTag(tagText),
          tag = _getTag.tag,
          position = _getTag.position;
  
      var text = allMatches[tag];
      if (text == null) {
        continue;
      }
      totalMatches.push({
        type: "tag",
        position: position,
        text: text,
        offset: offset,
        value: tagText,
        tag: tag
      });
    }

my script:

function doIt() {
  function loadFile(url, callback) {
    JSZipUtils.getBinaryContent(url, callback);
  }
  loadFile('../tag-example.docx', function(error, content) {
    if (error) {
      throw error;
    }

    var zip = new JSZip();
    zip.file('word/document.xml', content);
    var doc = new Docxtemplater().loadZip(zip);

    doc.setData({
      first_name: 'John',
      last_name: 'Doe',
      phone: '0652455478',
      description: 'New Website'
    });

    try {
      // render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
      doc.render();
      console.log('asdf');
    } catch (error) {
      var e = {
        message: error.message,
        name: error.name,
        stack: error.stack,
        properties: error.properties
      };
      console.log(JSON.stringify({ error: e }));
      // The error thrown here contains additional information when logged with JSON.stringify (it contains a property object).
      throw error;
    }

    console.log('rendered');
    var out = doc.getZip().generate({
      type: 'blob',
      mimeType:
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
    }); //Output the document using Data-URI
    saveAs(out, 'output.docx');
  });
}

Docxtemplater is undefined (Browser)

Hi,
I installed via npm install docxtemplater
My Page has a script tag to the js-file and from the development-tools (F12) I can see, the file has been loaded. But if I use var doc = new Docxtemplater().loadZip(zip); console Output is 'Docxtemplater' is undefined.
Within the docxtemplater.js-file I can find var Docxtemplater=function()...
So, what is the Problem here?
Any help appriciated, with Kind Regards, Ronny

Docxtemplater is not defined

My codes:

docx

var DocxReader = function() {
    // Var
    this.url = "";
    this.error = null;
    this.zipContent = null;
    this.file = {
        contentAsString: null,
        contentAsXml: null
    };
    this.docxtemplater = null;
    this.name = "output.docx";

    // Methods

    this.Load = function(url, loaded) {
        this.url = url;
        var splitUrl = this.url.split('/');
        this.name = splitUrl[splitUrl.length - 1];

        var docThis = this;

        JSZipUtils.getBinaryContent(this.url, function(error, content) {
            if (error) {
                docThis.error = error;
                return;
            }

            docThis.zipContent = new JSZip(content);

            var documentxml = docThis.zipContent.file("word/document.xml");
            var strDocumentxml = documentxml.asText();

            docThis.file.contentAsString = strDocumentxml;
            docThis.file.contentAsXml = $(strDocumentxml);
            docThis.docxtemplater = new Docxgen();
            docThis.docxtemplater.loadZip(docThis.zipContent);

            loaded();
        });
    };
    this.loadZip(zip) {
        if (zip.loadAsync) {
            throw new XTInternalError(
                "Docxtemplater doesn't handle JSZip version >=3, see changelog"
            );
        }
        this.zip = zip;
        this.updateFileTypeConfig();
        return this;
    }

    this.Search = function(txt) {
        var lowerCaseTxt = txt.toLowerCase();
        var innerDocument = this.file.contentAsXml.find('w\\:body').text().toLowerCase();
        if (innerDocument.indexOf(lowerCaseTxt) != -1) {
            return true;
        }

        return false;

    };

    this.Replace = function(oldtxt, newtxt) {
        var oldContent = this.file.contentAsString;
        var newContent = oldContent.replace(new RegExp(oldtxt, 'g'), newtxt);
        this.zipContent.file("word/document.xml", newContent);

        // Update object
        this.docxtemplater.loadZip(this.zipContent);
        this.file.contentAsString = newContent;
        this.file.contentAsXml = $(newContent);
    };

    this.ReplaceVariable = function(variables) {
        this.docxtemplater = setData(variables);
        this.docxtemplater.render();

        // Update object
        this.zipContent = this.docxtemplater.getZip();
        var documentxml = this.zipContent.file("word/document.xml");
        var strDocumentxml = documentxml.asText();
        this.file.contentAsString = strDocumentxml;
        this.file.contentAsXml = $(strDocumentxml);

    };

    this.SetName = function(name) {
        this.name = name + ".docx";
    };

    this.Download = function() {
        var out = this.docxtemplater.getZip().generate({
            type: "blob",
            mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        })

        saveAs(out, this.name);
    };

    this.UploadToSharePoint = function(clientContext, jsomListObject, onSucceed, onFailed) {
        var base64 = this.zipContent.generate();
        var listRootFolder = jsomListObject.get_rootFolder();
        clientContext.load(listRootFolder);
        clientContext.executeQueryAsync(Function.createDelegate(this, function() {
            var fileName = listRootFolder.get_serverRelativeUrl() + '/' + this.name;

            //Create FileCreationInformation object using the read file data  
            var createInfo = new SP.FileCreationInformation();
            createInfo.set_content(base64);
            createInfo.set_url(fileName);

            //Add the file to the library  
            var uploadedDocument = jsomListObject.get_rootFolder().get_files().add(createInfo);

            //Load client context and execcute the batch  
            clientContext.load(uploadedDocument);
            clientContext.executeQueryAsync(Function.createDelegate(this, onSucceed), Function.createDelegate(this, onFailed));
        }), Function.createDelegate(this, onFailed));

    };
}

scripts:

<script src="/js/docxgen.min.js"></script>
<script src="/js/jszip.min.js"></script>
<script src="/js/jszip.utils.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/docxtemplater/3.1.7/docxtemplater.js"></script>
<script src="/js/file-saver.min.js"></script>
<script src="/js/DocxReader.js"></script>  

js using docxreader
```
var docx = new DocxReader();
docx.Load(tmppath, function() {

    docx.Replace("Lorem", "REPLACED");
    try {
        // render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
        docx.docxtemplater.render();
    } catch (error) {
        var e = {
            message: error.message,
            name: error.name,
            stack: error.stack,
            properties: error.properties,
        }
        console.log(JSON.stringify({ error: e }));
        throw error;
    }

});


I'm using Google Chrome and get docxtemplater is not defined on console.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.