使用ITextRenderer设置页面的属性

时间:2015-09-17 10:21:55

标签: java backbone.js pdf-generation mustache flying-saucer

我有一个在某个时刻导出PDF的应用程序。问题是我想在最后一个PDF上显示我的html模板中的某个部分(总是)。

我在前端使用Backbone并使用Mustache编译html。 这是exportToPDF方法:

public void exportPDF(Petition petition, OutputStream outputStream) {
        PrintablePetition printablePetition = prepareForPDFExport(petition);
        MustacheFactory mf = new DefaultMustacheFactory();
        Mustache mustache;
        if (printablePetition.getPetitionType().equals(PetitionTypeEnum.MILK.petitionType())) {
            mustache = mf.compile(TEMPLATE_MILK);
        } else if (printablePetition.getPetitionType().equals(PetitionTypeEnum.FRUITS.petitionType())) {
            mustache = mf.compile(TEMPLATE_FRUIT);
        } else {
            throw new CPERuntimeException(CPEStringUtils.build("Unknown petition type: ", printablePetition.getPetitionType() + ""));
        }
        StringWriter stringWriter = new StringWriter();
        try {
            mustache.execute(stringWriter, new Object[]{printablePetition, new TemplateParams(BASE_URI)}).flush();

            ITextRenderer renderer = new ITextRenderer();
            ITextFontResolver resolver = renderer.getFontResolver();
            resolver.addFont(
                    "fonts" + FILE_SEPARATOR + "arial.ttf",
                    BaseFont.IDENTITY_H,
                    BaseFont.NOT_EMBEDDED
            );
            renderer.setDocumentFromString(stringWriter.toString());
            renderer.layout();
            renderer.createPDF(outputStream);
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        } catch (DocumentException e) {
            LOG.error(e.getMessage(), e);
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                LOG.error(e.getMessage(), e);
            }
        }
    }

这是我要在最后一页上出现的html片段:

<section id="last_page">
        <p>
            * - numarul maxim de copii efectiv beneficiari
            N**= 1.03 si intra in calculul cantitatii efectiv furnizate numai in cazul produselor cu unitate de masura in litrii
        </p>

        <br/>
        <br/>

        <table cellspacing="0" width="90%" align="center" style="font-size: 11pt; line-height: 20pt;">
            <tr>
                <td>
                    Întocmit de,<br/>
                    <span style="margin-right: 50pt;">Funcţia ......................</span> <br/>
                    Data _____/_____/_____<br/>
                    Numele, prenumele  şi  semnătura reprezentantului legal al  solicitantului,<br/>
                    ...................................................................................<br/>
                    Data _____/_____/_____
                </td>
                <td style="vertical-align: top;">
                    Ştampila solicitantului
                    <br/>
                </td>
            </tr>
        </table>
    </section>

问题是我不知道如何获取该section元素并告诉ITextRenderer始终在最后一页显示它。

有什么想法吗?感谢。

0 个答案:

没有答案