user1194310
user1194310

Reputation: 129

Export HTML with data to MS Word

We have a requirement where we are asking our customers to fill the BRD document which is in a HTML file. HTML consists of radio buttons, text box etc along with colors and table. We will have a button which when clicked should call a java class which exports the HTML along with data customer inputs to word document. We are successful in converting a HTML code which is given directly as a string in the java program to word document. We are having issues in sending the HTML along with data.

Can any one let me know how I can achieve this? Or is there any better way we can do this.

public class XhtmlToDocx {

    public static void main(String[] args) throws Exception {

        //String html = "<html><form><input type=\"checkbox\" name=\"xhtml_mp_tutorial_chapter\" value=\"1\"/></form></html>";


String html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"+
"<html xmlns=\"http://www.w3.org/1999/xhtml\">"+
"<head>"+
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">"+
"<title>Untitled Form</title>"+
"<link rel=\"stylesheet\" type=\"text/css\" href=\"view.css\" media=\"all\">"+
"<script type=\"text/javascript\" src=\"view.js\"></script>"+
"<script type=\"text/javascript\" src=\"calendar.js\"></script>"+
"</head>"+
"<body id=\"main_body\" >"+
"   "+
"   <img id=\"top\" src=\"top.png\" alt=\"\">"+
"   <div id=\"form_container\">"+
"   "+
"       <h1><a>Untitled Form</a></h1>"+
"       <form id=\"form_82495\" class=\"appnitro\"  method=\"post\" action=\"\">"+
"                   <div class=\"form_description\">"+
"           <h2>Untitled Form</h2>"+
"           <p>This is your form description. Click here to edit.</p>"+
"       </div>                      "+
"           <ul >"+
"           "+
"                   <li id=\"li_1\" >"+
"       <label class=\"description\" for=\"element_1\">Text </label>"+
"       <div>"+
"           <input id=\"element_1\" name=\"element_1\" class=\"element text medium\" type=\"text\" maxlength=\"255\" value=\"\"/> "+
"       </div> "+
"       </li>       <li id=\"li_3\" >"+
"       <label class=\"description\" for=\"element_3\">Multiple Choice </label>"+
"       <span>"+
"           <input id=\"element_3_1\" name=\"element_3\" class=\"element radio\" type=\"radio\" value=\"1\" />"+
"<label class=\"choice\" for=\"element_3_1\">First option</label>"+
"<input id=\"element_3_2\" name=\"element_3\" class=\"element radio\" type=\"radio\" value=\"2\" />"+
"<label class=\"choice\" for=\"element_3_2\">Second option</label>"+
"<input id=\"element_3_3\" name=\"element_3\" class=\"element radio\" type=\"radio\" value=\"3\" />"+
"<label class=\"choice\" for=\"element_3_3\">Third option</label>"+
""+
"       </span> "+
"       </li>       <li id=\"li_2\" >"+
"       <label class=\"description\" for=\"element_2\">Date </label>"+
"       <span>"+
"           <input id=\"element_2_1\" name=\"element_2_1\" class=\"element text\" size=\"2\" maxlength=\"2\" value=\"\" type=\"text\"> /"+
"           <label for=\"element_2_1\">MM</label>"+
"       </span>"+
"       <span>"+
"           <input id=\"element_2_2\" name=\"element_2_2\" class=\"element text\" size=\"2\" maxlength=\"2\" value=\"\" type=\"text\"> /"+
"           <label for=\"element_2_2\">DD</label>"+
"       </span>"+
"       <span>"+
"           <input id=\"element_2_3\" name=\"element_2_3\" class=\"element text\" size=\"4\" maxlength=\"4\" value=\"\" type=\"text\">"+
"           <label for=\"element_2_3\">YYYY</label>"+
"       </span>"+
"   "+
"       <span id=\"calendar_2\">"+
"           <img id=\"cal_img_2\" class=\"datepicker\" src=\"calendar.gif\" alt=\"Pick a date.\">   "+
"       </span>"+
"       <script type=\"text/javascript\">"+
"           Calendar.setup({"+
"           inputField   : \"element_2_3\","+
"           baseField    : \"element_2\","+
"           displayArea  : \"calendar_2\","+
"           button       : \"cal_img_2\","+
"           ifFormat     : \"%B %e, %Y\","+
"           onSelect     : selectDate"+
"           });"+
"       </script>"+
"        "+
"       </li>"+
"           "+
"                   <li class=\"buttons\">"+
"               <input type=\"hidden\" name=\"form_id\" value=\"82495\" />"+
"               "+
"               <input id=\"saveForm\" class=\"button_text\" type=\"submit\" name=\"submit\" value=\"Submit\" />"+
"       </li>"+
"           </ul>"+
"       </form> "+
"       <div id=\"footer\">"+
"           Generated by <a href=\"http://www.phpform.org\">pForm</a>"+
"       </div>"+
"   </div>"+
"   <img id=\"bottom\" src=\"bottom.png\" alt=\"\">"+
"   </body>"+
"</html>";


        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
        AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/hw.html"));
        afiPart.setBinaryData(html.getBytes());
        afiPart.setContentType(new ContentType("text/html"));
        Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(afiPart);

        // .. the bit in document body
        CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
        ac.setId(altChunkRel.getId() );
        wordMLPackage.getMainDocumentPart().addObject(ac);

        // .. content type
        wordMLPackage.getContentTypeManager().addDefaultContentType("html", "text/html");
        wordMLPackage.save(new java.io.File("C:/Users/****/Downloads/Word.docx"));

  }
}

Upvotes: 0

Views: 383

Answers (1)

Avinash Sagar
Avinash Sagar

Reputation: 517

The problem you are facing seems to be happening because you are reading the static HTML page not the submitted page.

In order to get the full content of the submitted html, you need to submit your form first with the data, create it as a static html page and then access that page with XMLSerializer or URLStreamReader to get the final data to be passed to the word processing part of your program.

I am not providing an exact solution with code as I suppose you will be able to implement the solution yourself and you are mainly stuck on the logic.

Upvotes: 1

Related Questions