繁体   English   中英

从HTML创建PDF文件会导致文本超出右边距(java + itext)

[英]Creating PDF file from HTML causes text to exceed the right margin (java + itext)

我在前端使用富文本编辑器,以允许用户创建PDF文件(使用java + itext)。 问题是,当用户输入大量空格(按空格键)时,它将导致文本超出PDF文件的右边距。

普通文字: 在此处输入图片说明

PDF: 在此处输入图片说明

现在,我添加了很多空格: 在此处输入图片说明

PDF: 在此处输入图片说明

这是我用来生成PDF的HTML模板:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <style>@page { size: A4; margin: 15mm 15mm 30mm 15mm; @bottom-left { font-family: Verdana; font-size: 10.0pt; content: "P&aacute;gina " counter(page) " de " counter(pages); } } body { font-family: Verdana; font-size: 10.0pt; } table { border-collapse: collapse; border-spacing: 0; table-layout: auto; -fs-table-paginate: paginate;}
    </style>
</head>
<body>
    <table style="width: 100%;">
        <thead>
            <tr>
                <th style="width: 100%;">
                    some header
                </th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <td style="width: 100%;">
                    some footer
                </td>
            </tr>
        </tfoot>
        <tbody>
            <tr>
                <td>
                    text from rich editor goes here
                </td>
            </tr>
        </tbody>
    </table>
</body>
</html>

我已经尝试过将表格和正文设置为以像素为单位的特定宽度,但是它会产生具有相同问题的PDF。

这是生成PDF的代码:

private void createPdfFile(Documento documento, String pathPdf) throws DocumentException, IOException {
    PdfWriter pdfWriter = new PdfWriter(new FileOutputStream(pathPdf));
    PdfDocument pdfDocument = new PdfDocument(pdfWriter);

    PdfDocumentInfo pdfInfo = pdfDocument.getDocumentInfo();
    pdfInfo.setCreator(creatorText);

    if (!StringUtils.isEmpty(documento.getTitleMetadata())) {
        pdfInfo.setTitle(documento.getTitleMetadata());
    }
    if (!StringUtils.isEmpty(documento.getAuthorMetadata())) {
        pdfInfo.setAuthor(documento.getAuthorMetadata());
    }
    if (!StringUtils.isEmpty(documento.getSubjectMetadata())) {
        pdfInfo.setSubject(documento.getSubjectMetadata());
    }

    ConverterProperties converterProperties = new ConverterProperties();
    HtmlConverter.convertToPdf(htmlHeader + documento.getBodyHtml() + htmlFooter, pdfDocument, converterProperties);

    pdfDocument.close();
}

我将CSS属性“ table-layout”设置为“ fixed”,并且现在可以使用该属性。 完整的HTML模板是:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <style>@page { size: A4; margin: 15mm 15mm 30mm 15mm; @bottom-left { font-family: Verdana; font-size: 10.0pt; content: "P&aacute;gina " counter(page) " de " counter(pages); } } body { font-family: Verdana; font-size: 10.0pt; } table { border-collapse: collapse; border-spacing: 0; table-layout: auto; -fs-table-paginate: paginate;}
    </style>
</head>
<body>
    <table style="width: 100%; table-layout: fixed;">
        <thead>
            <tr>
                <th style="width: 100%;">
                    some header
                </th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <td style="width: 100%;">
                    some footer
                </td>
            </tr>
        </tfoot>
        <tbody>
            <tr>
                <td>
                    text from rich editor goes here
                </td>
            </tr>
        </tbody>
    </table>
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM