Unable to resize the image while converting html to doc using doc4j

╄→гoц情女王★ 提交于 2019-12-25 09:16:42

问题


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:


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.



来源:https://stackoverflow.com/questions/41583022/unable-to-resize-the-image-while-converting-html-to-doc-using-doc4j

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