JAVA/PDF:从模板动态添加表行.doc.pdf输出



我的私密问题实际上只是展示了我正在尝试做的解决方案之一。实际情况如下: /**(1)Our clients(A) will offer us(B) templates in .doc format *(2)We should fill all of the empty tables with data rows accroding to the table-head in templates. *(3)The output of these documents shoulded be delivered in .pdf format to users(C) for the seek of avoiding being changed. *(4)PS-->part of the final PDF contents are come from rich-text-editor stored in database through browers.So the pdf is make up with two main parts totally **/ (5)PPS-->它必须支持中文

我的想法是,要分别处理.doc模板和富文本,因为我还没有找到"银弹"。稍后我将把这两个.pdf部分组合在一起。

(1(富文本部分:我将使用飞碟或pef-box从html生成临时.pdf文件。

(2(.doc模板部分:这是我在这里尝试的。

1,I tried FOP and other xstl library, but it can't work with .doc well.
2,I can not use itext because the format of the .doc is complex and strict, it would kill me if I make a .pdf looking like the same as the .doc tempates with code.
3,I have worked with flying-saucer in the folowing steps:
      1).doc transformed to .html documents
      2)edit the .html documents with free-marker language to generte .flt templates
      3)fill table rows in .flt with dynamic data fetched from database generate middle xhtml stream
      4)generate .pdf file from xhtml stream with flying-saucer

如您所见,我的解决方案真的太尴尬了。处理 100+ 模板不是一个好的解决方案。

这是我的问题:(1)是否有"银弹"可以处理.doc(应该动态添加表行数据(和富文本(html/xhtml(到我的.pdf文件中?

(2(如果没有(我真的找不到。我应该如何解决它?我应该如何处理.doc模板?

(3(@MihaiC给我一个解决方案,但我的老板不买账,因为他想让我尝试一个开源的解决方案。那么,如果使用预言机发布器,我现在可以自由地使用它并具有完整的功能吗?

====

=============================================================

我有一个pdf tampalte文件,它只有一个带有表头的表格。现在我需要根据从数据库中获取的数据添加表行内容。我在SO中搜索了很多,但是找不到解决此问题的方法。有谁知道是否有Java库可以帮助我做到这一点?

PS->

不想为 pdf 文件创建新表,而是在表头后面添加行。

我无法从头开始创建pdf的原因是输出pdf文件的格式非常严格,以至于通过代码创建它太复杂了。所以我只需要使用 pdf 模板文件来维护它的格式和布局

我希望我理解你的问题,我认为你需要的是一种从template文件开始生成pdf的方法,该文件可以随时间更改,以及数据库中的数据。

为此,您可以使用Oracle Bi Publisher Desktop中的库。他们的位置通常是

C:Program FilesOracleBI PublisherBI Publisher DesktopTemplateViewerlib

您需要的是(自较新版本以来名称可能已更改,但或多或少是相同的,如果有疑问,请添加项目中存在的所有名称(

i18nAPI_v3版本信息星芯XMLPARSERV2-904

您可以阅读有关构建RTF模板的教程,例如创建 RTF 模板。网上有很多例子。

您需要将数据库中的数据转换为 xml 格式,因为库使用标记来标识行和数据。

首先,您需要为数据库返回的查询结果定义这样的方法。

你可以使用 JAXB

public byte[] getXMLData() { ... }

下面的方法将根据getXMLData()方法生成的xml生成报告,以及具有outFileType输出文件类型的templateFile

public byte[] getReport(String templateFile, String outFileType) {
    byte[] dataBytes = null;
    try {
        //Process RTF template to convert to XSL-FO format
        RTFProcessor rtfp = new RTFProcessor(templateFile);
        ByteArrayOutputStream xslOutStream = new ByteArrayOutputStream();
        rtfp.setOutput(xslOutStream);
        rtfp.process();
        ByteArrayInputStream xslInStream = new ByteArrayInputStream(xslOutStream.toByteArray());
        FOProcessor processor = new FOProcessor();
        ByteArrayInputStream dataStream = new ByteArrayInputStream(getXMLData());
        processor.setData(dataStream);
        processor.setTemplate(xslInStream);
        ByteArrayOutputStream pdfOutStream = new ByteArrayOutputStream();
        processor.setOutput(pdfOutStream);
        byte outFileTypeByte = FOProcessor.FORMAT_PDF;
        if ("HTML".equalsIgnoreCase(outFileType)) {
            outFileTypeByte = FOProcessor.FORMAT_HTML;
        } else if ("RTF".equalsIgnoreCase(outFileType)) {
            outFileTypeByte = FOProcessor.FORMAT_RTF;
        }
        processor.setOutputFormat(outFileTypeByte);
        processor.generate();
        dataBytes = pdfOutStream.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("IOException when generating pdf " + e);
    } catch (XDOException e) {
        e.printStackTrace();
        System.out.println("XDOException when generating pdf " + e);
    }
    return dataBytes;
}

通过调用以下内容生成pdf报告:

public byte[] getReport() {
   String templatePath ="<pathToTemplate>/Template.rtf";
   return getReport(templatePath, "pdf");
}

相关内容

最新更新