HTML to word converting using javascript

此生再无相见时 提交于 2020-02-20 05:59:05

问题


I am trying to convert HTML to word (.docx) by using JavaScript. I am using this http://www.jqueryscript.net/other/Export-Html-To-Word-Document-With-Images-Using-jQuery-Word-Export-Plugin.html plug-in for conversion. But this one is just converting every thing inside the HTML file. i mean with head tag all elements even with some content inside. output file looks like this

Mime-Version: 1.0 Content-Base: file:///home/userprofile/JsWs/sample.html Content-Type: Multipart/related; boundary="NEXT.ITEM-BOUNDARY";type="text/html"

--NEXT.ITEM-BOUNDARY Content-Type: text/html; charset="utf-8" Content-Location: file:///home/userprofile/JsWs/sample.html

  <p>this is going to be paragraph </p>

  </body></html>

--NEXT.ITEM-BOUNDARY--

and my html is

<html> 
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script src="FileSaver.js"></script>
        <script src="jquery.wordexport.js"></script>
    </head>
    <body>
        <script type="text/javascript">
        jQuery(document).ready(function($) {
            $("a.word-export").click(function(event) {
                $("#export-content").wordExport();
            });
        });
        </script>
        <div id="export-content">

        <p>this is going to be paragraph </p>

        </div>

        <a class="word-export" href="javascript:void(0)"> Export as .doc </a>
    </body>
</html>

Help me out how can i convert content of HTML in word.


回答1:


I don't believe this will open in Microsoft Word consistently on all devices. I've actually been working on a similar project googoose. It is meant to convert html to word documents as well. This does work for me on both my desktop and mobile phone version of Microsoft Word.

From my testing, Word will have problems with MIME header, and also the fact that there are no html, body tags, etc.

You should look into this project if you're still trying to do this. It should be supported for some time, as there's also a Wordpress Plugin that uses googoose, which is linked to on the readme. The default action is to download the document on page load, but you can set it to be run onClick. For example,

Just include jquery and this plugin.

<script type="text/javascript" src="http://github.com/aadel112/googoose/jquery.googoose.js"></script>

Then to invoke onClick

jQuery(document).ready(function($) {
        $("a.word-export").click(function(event) {
            $(document).googoose({
                 area: '#export-content'
            });
        });
    });

With googoose you can include a table of contents, header, footer, automatic page numbering, etc. You can open the document up in Print view, set the margins, and page size. It's way more configurable than the referenced script, which is not configurable at all.




回答2:


Try this,

function ConvertToHtml()
{
    var div = document.createElement("div");
    div.id = "export-content";
    div.innerHTML = "<p>this is going to be paragraph </p>";
    document.body.appendChild(div);
    $("#export-content").wordExport();
}


来源:https://stackoverflow.com/questions/32222940/html-to-word-converting-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!