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

Site crashes #398

Closed
tobiasBlaser opened this issue May 4, 2018 · 9 comments
Closed

Site crashes #398

tobiasBlaser opened this issue May 4, 2018 · 9 comments

Comments

@tobiasBlaser
Copy link

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');
  });
}
@edi9999
Copy link
Member

edi9999 commented May 8, 2018

Can you share your template ?

Are you using the latest version of docxtemplater ?

@edi9999
Copy link
Member

edi9999 commented May 10, 2018

I think there could be one way that this is in an infinite loop :

In the case that there is an opening tag < but no closing tag >.

I could add some robustness by throwing an exception in that case, but can you share your template, it could be that the parsing is just not done correctly in all cases ?

@edi9999
Copy link
Member

edi9999 commented May 12, 2018

Any update on this ? @tobiasBlaser

@edi9999
Copy link
Member

edi9999 commented May 13, 2018

I have just published a fix in version 3.6.3 that should handle this case without looping infinitely (an error will be thrown now).

Let me know if that works.

I'm closing it for now.

@edi9999 edi9999 closed this as completed May 13, 2018
@tobiasBlaser
Copy link
Author

tobiasBlaser commented May 25, 2018

Hey @edi9999
I used the newest version now and got these errors:

{"error":{"message":"An XML file has invalid xml","name":"TemplateError","stack":"Error: An XML file has invalid xml\n    at new XTTemplateError (http://127.0.0.1:8887/js/docxtemplater.js:308:15)\n    at throwXmlInvalid (http://127.0.0.1:8887/js/docxtemplater.js:549:12)\n    at tagMatcher (http://127.0.0.1:8887/js/docxtemplater.js:726:4)\n    at Object.xmlparse (http://127.0.0.1:8887/js/docxtemplater.js:995:17)\n    at XmlTemplater.parse (http://127.0.0.1:8887/js/docxtemplater.js:2411:26)\n    at Docxtemplater.compileFile (http://127.0.0.1:8887/js/docxtemplater.js:4715:16)\n    at http://127.0.0.1:8887/js/docxtemplater.js:4757:13\n    at Array.forEach (<anonymous>)\n    at Docxtemplater.compile (http://127.0.0.1:8887/js/docxtemplater.js:4755:24)\n    at Docxtemplater.render (http://127.0.0.1:8887/js/docxtemplater.js:4791:9)","properties":{"id":"file_has_invalid_xml","content":"PK\u0003\u0004\u0014\u0000\u0006\u0000\b\u0000\u0000\u0000!\u0000\fO\u0016\u0012\u0006\u0001\u0000\u0000\u0006\u0007\u0000\u0000\u0013\u0000\b\u0002[Content_Types].xml 
Uncaught 
XTTemplateError {name: "TemplateError", message: "An XML file has invalid xml", stack: "Error: An XML file has invalid xml↵    at new XTTe…http://127.0.0.1:8887/js/docxtemplater.js:4791:9)", properties: {…}}
message
:
"An XML file has invalid xml"
name
:
"TemplateError"
properties
:
{id: "file_has_invalid_xml", content: "PK�����!O���������[Content_Types].xml …�DdocProps/app.xmlPK������Ƈ", offset: 700, explanation: "The docx contains invalid XML, it is most likely corrupt"}
stack
:
"Error: An XML file has invalid xml↵    at new XTTemplateError (http://127.0.0.1:8887/js/docxtemplater.js:308:15)↵    at throwXmlInvalid (http://127.0.0.1:8887/js/docxtemplater.js:549:12)↵    at tagMatcher (http://127.0.0.1:8887/js/docxtemplater.js:726:4)↵    at Object.xmlparse (http://127.0.0.1:8887/js/docxtemplater.js:995:17)↵    at XmlTemplater.parse (http://127.0.0.1:8887/js/docxtemplater.js:2411:26)↵    at Docxtemplater.compileFile (http://127.0.0.1:8887/js/docxtemplater.js:4715:16)↵    at http://127.0.0.1:8887/js/docxtemplater.js:4757:13↵    at Array.forEach (<anonymous>)↵    at Docxtemplater.compile (http://127.0.0.1:8887/js/docxtemplater.js:4755:24)↵    at Docxtemplater.render (http://127.0.0.1:8887/js/docxtemplater.js:4791:9)"
__proto__
:
Error

@edi9999
Copy link
Member

edi9999 commented May 25, 2018

Oh, now I get it.

The issue comes from the line :

zip.file('word/document.xml', content);

that you have added.
If you remove this line you won't have the problem anymore.

What you were doing in your code is to replace the file /word/document.xml by the whole .docx file, which is a zipped file format and not valid XML

@tobiasBlaser
Copy link
Author

tobiasBlaser commented May 25, 2018

Is this part ok?

 loadFile("examples/tag-example.docx", function (error, content) {
        if (error) {
          throw error
        };
        var zip = new JSZip("word/document.xml", content);
        var doc = new Docxtemplater().loadZip(zip);
        doc.setData({
          first_name: 'John',
          last_name: 'Doe',
          phone: '0652455478',
          description: 'New Website'
        });

because i get this error now:

Uncaught Error: Can't find end of central directory : is this a zip file ? If it is, see http://stuk.github.io/jszip/documentation/howto/read_zip.html
    at d.readEndOfCentral (jszip.min.js:12)
    at d.load (jszip.min.js:12)
    at new d (jszip.min.js:12)
    at d.b.exports [as load] (jszip.min.js:12)
    at new d (jszip.min.js:12)
    at (index):24
    at XMLHttpRequest.c.onreadystatechange (jszip-utils.min.js:10)

@edi9999
Copy link
Member

edi9999 commented May 25, 2018

It should be like this :

  loadFile('../tag-example.docx', function(error, content) {
    if (error) {
      throw error;
    }

    var zip = new JSZip(content);
    var doc = new Docxtemplater().loadZip(zip);

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

@tobiasBlaser
Copy link
Author

Everything works now! Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants