1

我正在为我的项目使用 iTextPDF + FreeMarker。基本上,我使用 FreeMarker 加载并填充 HTML 模板,然后使用 iTextPDF 将其渲染为 pdf XMLWorker

模板是:

<html>
    <body style="font-family; ${fontName}">
        <table>
            <tr>
                <td style="text-align: right">${timestampLabel}&nbsp;</td>
                <td><b>${timestampValue}</b></td>
            </tr>
            <tr>
                <td style="text-align: right">${errorIdLabel}&nbsp;</td>
                <td><b>${errorIdValue}</b></td>
            </tr>
            <tr>
                <td style="text-align: right">${systemIdLabel}&nbsp;</td>
                <td><b>${systemIdValue}</b></td>
            </tr>
            <tr>
                <td style="text-align: right">${descriptionLabel}&nbsp;</td>
                <td><b>${descriptionValue}</b></td>
            </tr>
        </table>
    </body>
</html>

这是我的代码:

SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

String errorId = "ERROR-01";
String systemId = "SYSTEM-01";
String description = "A SHORT DESCRIPTION OF THE ISSUE";

Map<String, String> parametersMap = new HashMap<String, String>();
parametersMap.put("fontName", fontName);  //valid font name

parametersMap.put("timestampLabel",   "   TIMESTAMP:");
parametersMap.put("errorIdLabel",     "    ERROR ID:");
parametersMap.put("systemIdLabel",    "   SYSTEM ID:");
parametersMap.put("descriptionLabel", " DESCRIPTION:");

parametersMap.put("timestampValue", DATE_FORMAT.format(new Date()));
parametersMap.put("errorIdValue", errorId);
parametersMap.put("systemIdValue", systemId);
parametersMap.put("descriptionValue", description);

FreeMarkerRenderer renderer = new FreeMarkerRenderer(); //A utility class
renderer.loadTemplate(errorTemplateFile);               //the file exists
String rendered = renderer.render(parametersMap);

File temp = File.createTempFile("document", ".pdf");
file = new FileOutputStream(temp);
Document document = new Document();

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
document.setPageSize(new Rectangle(290f, 150f));
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

document.setMargins(10, 10, 10, 10);
PdfWriter writer = PdfWriter.getInstance(document, file);
document.open();
InputStream is = new ByteArrayInputStream(rendered.getBytes());


XMLWorkerFontProvider provider = new XMLWorkerFontProvider();
provider.register(fontFile); //the file exists
FontFactory.setFontImp(provider);
byte[] errorStyle = getErrorStyleByteArray(); //returns a byte array from a css file (works)

XMLWorkerHelper helper = XMLWorkerHelper.getInstance();
helper.parseXHtml(writer, document, is, new ByteArrayInputStream(errorStyle), provider);

document.close();
file.close();

此代码工作正常,但固定高度是一个问题。

IE 假设:

errorId = "ERROR-01"
systemId = "SYSTEM-01"
description = "A SHORT DESCRIPTION OF THE ISSUE"

生成的文件是:

在此处输入图像描述

如果相反我使用

errorId = "ERROR-01"
systemId = "SYSTEM-01"
description = "A SHORT DESCRIPTION OF THE ISSUE. THIS IS MULTILINE AND IT SHOULD STAY ALL IN THE SAME PDF PAGE."

生成的文件是:

在此处输入图像描述

如您所见,在最后一个文档中,我有两页。我希望只有一页根据内容高度改变其高度。

使用 iText 可以实现这样的事情吗?

4

1 回答 1

2

将内容添加到该页面后,您无法更改页面大小。解决此问题的一种方法是分两次创建文档:首先创建一个文档以添加内容,然后操作该文档以更改页面大小。如果我有时间立即回答,那将是我的第一个答复。

现在我花了更多时间考虑它,我找到了一个不需要两次通过的更好的解决方案。看看HtmlAdjustPageSize

Element在此示例中,我首先使用此方法将内容解析为对象列表:

public ElementList parseHtml(String html, String css) throws IOException {
    // CSS
    CSSResolver cssResolver = new StyleAttrCSSResolver();
    CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(css.getBytes()));
    cssResolver.addCss(cssFile);

    // HTML
    CssAppliers cssAppliers = new CssAppliersImpl(FontFactory.getFontImp());
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
    htmlContext.autoBookmark(false);

    // Pipelines
    ElementList elements = new ElementList();
    ElementHandlerPipeline end = new ElementHandlerPipeline(elements, null);
    HtmlPipeline htmlPipeline = new HtmlPipeline(htmlContext, end);
    CssResolverPipeline cssPipeline = new CssResolverPipeline(cssResolver, htmlPipeline);

    // XML Worker
    XMLWorker worker = new XMLWorker(cssPipeline, true);
    XMLParser p = new XMLParser(worker);
    p.parse(new ByteArrayInputStream(html.getBytes()));

    return elements;
}

注意:我已经多次复制/粘贴此方法,因此我决定将其设为XMLWorkerHelper类中的静态方法。它将在下一个 iText 版本中提供。

重要提示:我已经做到了我的承诺,这个方法现在可以在 XML Worker 版本中使用。

出于测试目的,我对 HTML 和 CSS 使用了静态String值:

public static final String HTML = "<table>" +
    "<tr><td class=\"ra\">TIMESTAMP</td><td><b>2014-11-28 11:06:09</b></td></tr>" +
    "<tr><td class=\"ra\">ERROR ID</td><td><b>ERROR-01</b></td></tr>" +
    "<tr><td class=\"ra\">SYSTEM ID</td><td><b>SYSTEM-01</b></td></tr>" +
    "<tr><td class=\"ra\">DESCRIPTION</td><td><b>TEST WITH A VERY, VERY LONG DESCRIPTION LINE THAT NEEDS MULTIPLE LINES</b></td></tr>" +
    "</table>";
public static final String CSS = "table {width: 200pt; } .ra { text-align: right; }";
public static final String DEST = "results/xmlworker/html_page_size.pdf";

您可以看到我采用的 HTML 看起来或多或少类似于您正在处理的 HTML。

我将此 HTML 和 CSS 解析为ElementList

ElementList el = parseHtml(HTML, CSS);

或者,从 XML Worker 5.5.4 开始:

ElementList el = XMLWorkerHelper.parseToElementList(HTML, CSS);

到目前为止,一切都很好。我没有告诉你任何你不知道的事情,除了这个:我现在要使用它el两次:

  1. 我将列表添加到ColumnText模拟模式中。这ColumnText还没有与任何文档或作者绑定。这样做的唯一目的是知道我需要多少垂直空间。
  2. 我会将列表添加到ColumnText真实的列表中。这ColumnText将完全适合我使用在模拟模式中获得的结果定义的大小的页面。

一些代码将阐明我的意思:

// I define a width of 200pt
float width = 200;
// I define the height as 10000pt (which is much more than I'll ever need)
float max = 10000;
// I create a column without a `writer` (strange, but it works)
ColumnText ct = new ColumnText(null);
ct.setSimpleColumn(new Rectangle(width, max));
for (Element e : el) {
    ct.addElement(e);
}
// I add content in simulation mode
ct.go(true);
// Now I ask the column for its Y position
float y = ct.getYLine();

上面的代码只对一件事有用:获取y将用于定义页面大小的值和将实际添加Document的列尺寸:ColumnText

Rectangle pagesize = new Rectangle(width, max - y);
// step 1
Document document = new Document(pagesize, 0, 0, 0, 0);
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
// step 3
document.open();
// step 4
ct = new ColumnText(writer.getDirectContent());
ct.setSimpleColumn(pagesize);
for (Element e : el) {
    ct.addElement(e);
}
ct.go();
// step 5
document.close();

请下载完整的HtmlAdjustPageSize.java代码并更改HTML. 您会看到这会导致不同的页面大小。

于 2014-11-30T08:58:48.703 回答