简体   繁体   中英

html docx js blank docx issue

I am trying to create docx file using the github project HTML-DOCX-JS but since I want to do this process locally I have downloaded the project and found that it has some issues. I am able to edit in tinymce editor and when I press convert I do get docx file but its blank. There are some changes which I made to the sample.html file because the default project doesn't even give a docx on click. Please check the below code and let me know what's causing the issue, no exceptions from JS as well.

    <!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>HTML-DOCX test</title>
  <script src="http://tinymce.cachefly.net/4.1/tinymce.min.js"></script>
  <script src="vendor/FileSaver.js"></script>
  <script src="../dist/html-docx.js"></script>
</head>
<body>
  <p>Enter/paste your document here:</p>
  <textarea id="content" cols="60" rows="10"></textarea>
  <div class="page-orientation">
    <span>Page orientation:</span>
    <label><input type="radio" name="orientation" value="portrait" checked>Portrait</label>
    <label><input type="radio" name="orientation" value="landscape">Landscape</label>
  </div>
  <button id="convert">Convert</button>
  <div id="download-area"></div>

  <script>
    tinymce.init({
      selector: '#content',
      plugins: [
        "advlist autolink lists link image charmap print preview anchor",
        "searchreplace visualblocks code fullscreen fullpage",
        "insertdatetime media table contextmenu paste"
      ],
      toolbar: "insertfile undo redo | styleselect | bold italic | " +
        "alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | " +
        "link image"
    });
    document.getElementById('convert').addEventListener('click', function(e) {
      e.preventDefault();
      var content = tinymce.get('content').getContent();
      var orientation = document.querySelector('.page-orientation input:checked').value;
      var converted = htmlDocx.asBlob(content, {orientation: orientation});

      saveAs(converted, 'test.docx');

      var link = document.createElement('a');
      link.href = URL.createObjectURL(converted);
      link.download = 'document.docx';
      link.appendChild(
        document.createTextNode('Click here if your download has not started automatically'));
      var downloadArea = document.getElementById('download-area');
      downloadArea.innerHTML = '';
      downloadArea.appendChild(link);
    });
  </script>
</body>
</html>

The file structure is something like this: http://i.stack.imgur.com/qBgGW.png

The NPM library html-docx-js uses " altChunk " markup to embed a raw HTML document within a Docx file.

Unfortunately, this is not supported on MacOS or Google Drive, which is why you're seeing a blank document instead. Open it in Word on Windows (or even online at Office365) for it to work.

After getting the content inside tiny mce editor you must construct a full html document and then send it to htmlDocx.asBlob() function or the document will display blank.

Your code should look like this:

var content = tinymce.get('content').getContent();
var orientation = document.querySelector('.page-orientation input:checked').value;

//Build a full html document including doctype
var html_document = '<!DOCTYPE html><html><head><title></title>';
html_document  += '</head><body>' + content + '</body></html>';
var converted = htmlDocx.asBlob(html_document, {orientation: orientation});
saveAs(converted, 'test.docx');

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM