0
votes

I am trying to generate the document from html using docx4j 3.3.1. I am facing below issue can some one help me on these?

HTML Code:

<!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" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" lang="en">
<head>
<title>MonthlyReport</title>
<style>
                    table
                    {
                    border:double #000;
                    table-layout: fixed;
                    vertical-align: top;
                    border-collapse: collapse;
                    width: 7.25in;
                    
                    }
                    .main-title
                    {
                    margin-top:25%;
                    font-size:48pt;
                    font-family:Century Gothic;
                    color:#2F5897;
                    text-align:center;
                    font-weight:300;
                    }
                    .year
                    {
                    padding-top:10%;
                    text-align:center;
                    font-size:18px;
                    
                    }
                    .other-detail
                    {
                    padding-top:10%;
                    padding-bottom:10%;
                    font-weight:bold;
                    text-align:center;
                    font-size:18px;
                    }
                    .cont
                    {
                    font-family:Palatino Linotype;
                    margin:0 auto;
                    margin-top:10px;
                    }
                    .cont-table
                    {
                    border:double #000;
                    table-layout: fixed;
                    vertical-align: top;
                    border-collapse: collapse;
                    width: 7.25in;
                    }
                    .cont h1
                    {
                    font-family:Century Gothic;
                    color:#2F5897;
                    font-weight:400;
                    margin-left:.15in;
                    }
                    .cont h4
                    {
                    font-family:Century Gothic;
                    font-weight:400;
                    margin-left:.15in;
                    }
                    .cont .school-detail
                    {
                    margin-left:.1in;
                    margin-right:.1in;
                    }
                    .cont ol
                    {
                    margin-left:1in;
                    margin-right:1in;
                    }
                    .cont li
                    {
                    margin-bottom:.25in;
                    }
                    .cont-heading
                    {
                    margin-left:.5in;
                    font-weight:bold;
                    text-decoration:underline;
                    margin-top:.35in;
                    
                    }
                    span
                    {
                    display:block;
                    padding-bottom:10px;
                    }
                    .rheight
                    {
                    height: 3.6in;
                    }
                    img
                    {
                    width:100%;
                    }
                    .img-table
                    {
                        width:5in;
                    }
                    .img-table td
                    {
                        width:5in;
                    }
                    .cont-img
                    {
                    width:4in;
                    }
                </style>
</head>
<body>
<table align="center" cellspacing="0" cellpadding="0">
<tr class="rheight">
<td> &nbsp; </td>
</tr>
<tr>
<td>
<div class="main-title"> Monthly report </div>
</td>
</tr>
<tr>
<td>
<div class="year"> 2016-2017 </div>
</td>
</tr>
<tr>
<td>
<div class="other-detail">MONTH: January<br />
            CITY: TEST_CITY<br /> FACILITATOR:
            TEST_PERSON<br />
</div>
</td>
</tr>
<tr class="rheight">
<td> &nbsp; </td>
</tr>
</table>
<table class="cont-table" align="center" cellspacing="0" cellpadding="0">
<tr>
<td>
<div class="cont">
<h1>Monthly Report</h1>
<h4>2016-2017</h4>
<br />
<p class="school-detail">
<p class="cont-heading">TEST SCHOOL</p>
<ol>
<li>
<strong>Test Question</strong>
<br />TEST_ANSWER</li>
</ol>
<p>Photos</p>
<table class="img-table" cellspacing="0" cellpadding="0">
<tr>
<td>
<img class="cont-img" src="C:\CodeBase\SampleProjects\fmstest\fms\TESTIMAGE.jpg" alt="TESTIMAGE.jpg" />
</td>
</tr>
</table>
</p>
</div>
</td>
</tr>
</table>
</body>
</html>

Issue 1: On Converting html without any image to document i am getting proper document style but if i use image in html styles are completely changed. Code is given below

`private static void documentGenerator(String html, File file) throws Docx4JException, JAXBException { //Word Processing Package WordprocessingMLPackage wordMLPackage = getWordMLPackage(); NumberingDefinitionsPart ndp = new NumberingDefinitionsPart(); wordMLPackage.getMainDocumentPart().addTargetPart(ndp); ndp.unmarshalDefaultNumbering();

//Saving the Document
wordMLPackage.getMainDocumentPart().addAltChunk(AltChunkType.Xhtml, html.getBytes());
wordMLPackage.save(file);

}`

Issue 2: On Using XHTMLImporterImpl Doc is generated with same content twice and styles are not proper. Code is given below private static void documentGenerator(String html, File file) throws Docx4JException, JAXBException { //Word Processing Package WordprocessingMLPackage wordMLPackage = getWordMLPackage(); NumberingDefinitionsPart ndp = new NumberingDefinitionsPart(); wordMLPackage.getMainDocumentPart().addTargetPart(ndp); ndp.unmarshalDefaultNumbering(); // Convert the XHTML, and add it into the empty docx we made XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage); XHTMLImporter.setHyperlinkStyle("Hyperlink"); String baseurl = file.getPath(); baseurl = baseurl.substring(0, baseurl.lastIndexOf("\\")); wordMLPackage.getMainDocumentPart().getContent().addAll(XHTMLImporter.convert(html, baseurl)); //Saving the Document wordMLPackage.getMainDocumentPart().addAltChunk(AltChunkType.Xhtml, html.getBytes()); wordMLPackage.save(file); }

Issue 3:
Generated document cannot be viewed in any application except MS Word.

1
You are most likely to get an answer if you stick to 1 issue per question. If you have any followup questions, please post new questions.JasonPlutext

1 Answers

0
votes

Regarding issue 1: addAltChunk just adds an altChunk (aka AlternativeFormatInput part), and since you just save the docx, you are relying on Word to convert the content, so your behaviour is Word specific.

Regarding issue 2: you're are getting duplicate content, since you do both XHTMLImporter.convert and addAltChunk.