在iText中将html转换为pdf后,表格边框丢失了

时间:2018-10-12 19:20:03

标签: html pdf itext

我正在使用iText将html转换为pdf。 pdf已成功生成,但是html中的表格内容在生成的pdf中缺少边框。如何确保转换后表格边框没有丢失?

pom.xml

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf.tool/xmlworker -->
<dependency>
    <groupId>com.itextpdf.tool</groupId>
    <artifactId>xmlworker</artifactId>
    <version>5.4.1</version>
</dependency>

功能

public StreamedContent getFile() throws IOException, DocumentException{
        final PortletResponse portletResponse = (PortletResponse) FacesContext.getCurrentInstance().getExternalContext()
                .getResponse();
        final HttpServletResponse res = PortalUtil.getHttpServletResponse(portletResponse);
        res.setContentType("application/pdf");
        res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        res.setHeader("Content-Disposition", "attachment; filename=" + subject + ".pdf");
        res.setHeader("Refresh", "1");
        res.flushBuffer();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStream out = res.getOutputStream();
        Document document = new Document(PageSize.LETTER);
        PdfWriter pdfWriter =PdfWriter.getInstance(document, baos);
        document.open();
        document.addCreationDate();
        XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
        //htmlWorker.parse(new StringReader(getMessage()));
        worker.parseXHtml(pdfWriter, document, new StringReader(getMessage()));
        document.close();
        baos.writeTo(out);
        out.flush();
        out.close();
        return null;
    }

HTML内容 -bean中的属性-(很抱歉,这里的长内容。这只是实际内容的一部分,以表明表边界属性已正确设置)

<o:p></o:p></span></p></td></tr><tr style="height: 15pt;"><td width="237" colspan="2" style="width: 177.7pt; border: 1pt solid windowtext; padding: 0in 5.4pt; height: 15pt;"><p class="MsoNormal" align="right" style="margin-bottom: 0.0001pt; text-align: right; line-height: normal;"><a name="RANGE!A15"><b><span arial",sans-serif;="" mso-fareast-font-family:"times="" new="" roman""="" style="font-size: 9pt;">XXX:</span></b></a><b><span arial",sans-serif;="" mso-fareast-font-family:"times="" new="" roman""="" style="font-size: 9pt;"><o:p></o:p></span></b></p></td><td width="394" colspan="3" style="width: 4.1in; border-top: 1pt solid windowtext; border-right: 1pt solid windowtext; border-bottom: 1pt solid windowtext; border-image: initial; border-left: none; padding: 0in 5.4pt; height: 15pt;"><p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><span tahoma",sans-serif"="" style="font-size: 9pt;">«0266855430»</span><span arial",sans-serif;="" mso-fareast-font-family:"times="" new="" roman""="" style="font-size: 9pt;"><o:p></o:p></span></p></td></tr><tr style="height: 15pt;"><td width="237" colspan="2" style="width: 177.7pt; border-right: 1pt solid windowtext; border-bottom: 1pt solid windowtext; border-left: 1pt solid windowtext; border-image: initial; border-top: none; padding: 0in 5.4pt; height: 15pt;"><p class="MsoNormal" align="right" style="margin-bottom: 0.0001pt; text-align: right; line-height: normal;"><b><span arial",sans-serif;mso-fareast-font-family:="" "times="" new="" roman""="" style="font-size: 9pt;">SENDER NAME:<o:p></o:p></span></b></p></td><td width="394" colspan="3" style="width: 4.1in; border-top: none; border-left: none; border-bottom: 1pt solid windowtext; border-right: 1pt solid windowtext; padding: 0in 5.4pt; height: 15pt;"><p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><span tahoma",sans-serif"="" style="font-size: 9pt;">«DAREO»

源图像(p:质素编辑器)

How it looks like before conversion to pdf

输出(边界丢失)

pdf output

其他所有东西都完美无缺。

请提出建议。

0 个答案:

没有答案