【问题标题】:How to generate PDF document using itext in java MVC pattern [closed]如何在java MVC模式中使用itext生成PDF文档[关闭]
【发布时间】:2015-02-11 19:25:34
【问题描述】:

所以我有一个我目前正在开发的 web 应用程序。它运行在 java/MVC 模式/MYsql/scala/play 框架上。

我正在寻找一种基于WEBAPP上的报告使用itext生成pdf文档的方法,所以基本上我们需要在报告页面上添加一个“打印按钮”,并将信息提取为PDF .

谢谢

【问题讨论】:

  • 问题在哪里?有什么尝试?你有什么问题?

标签: java scala pdf model-view-controller itext


【解决方案1】:

在不提供实际代码的情况下,我将提供一种方法来说明如何做到这一点。

如果我们假设您有某种结构化数据,例如要打印的项目列表,您可以使用visitor 模式。

您希望它为每种要打印的项目类型提供一个visit(...) 方法。

例如,如果你有 2 个类:

public class Foo {
    ...
    public int foo;
    ...
}

public class Bar {
    ...
    public boolean bar;
    ...
}

那么你可以让你的 PDF 访问者看起来像这样:

public class MyPDFVisitor {
    ...
    public void visit(Foo foo) {
        ...
        // do something with foo.foo
        ...
    }

    public void visit(Bar bar) {
        ...
        // do something with Bar.bar
        ...
    }
}

现在,我看到你想使用iText。所以,你可以添加到你的 MyPDFVisitor 类来支持它是这样的:

public class MyPDFVisitor {
    public MyPDFVisitor() {
        this.document = new Document(PageSize.A4);
        this.outputStream = new ByteArrayOutputStream();

        try {
            this.pdfWriter = PdfWriter.getInstance(this.document, this.outputStream);
        } catch (DocumentException e) {
            e.printStackTrace();
        }

        this.document.open();
    }

    private void addDocumentName(String description) throws DocumentException {
        Paragraph preface = new Paragraph();
        preface.setAlignment(ElementTags.ALIGN_CENTER);
        addEmptyLine(preface, 1);
        preface.add(new Paragraph(new Chunk(description, getTitleFont())));
        addEmptyLine(preface, 2);
        document.add(preface);
    }

    private void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
    }

    public void visit(Foo foo) {
        Integer fooValue = foo.foo;
        write(fooValue.toString(), Color.GREEN);
    }

    public void visit(Bar bar) {
        Boolean barValue = bar.bar;
        write(barValue.toString(), Color.RED);
    }

    public void write(String text, Color color) {
        // do the actual write to document here
    }

    public InputStream getInputStream() {
        try {
            // you can do some final actions here, before closing the writing to the document

            this.document.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        }

        ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
        return inputStream;
    }

}

请不要将其与生产代码混淆。我只是举了一个例子,说明如何解决问题并从那里解决。

免责声明:显然,这是一个 Java 解决方案,但目的是向您展示概念,而不是为您提供可以复制/粘贴的代码。

【讨论】:

  • 这个问题有很多可能的答案,但这个答案中解释的概念肯定是有效的。不是唯一一个(而且不是 100% 我会怎么做),但值得一看。
  • 现在你让我很感兴趣!我已经很长时间没有使用 iText,但我对图书馆的作者将如何解决解决方案感兴趣:D
  • 您的方法确实不错,但魔鬼在细节中。例如:我会使用Chunk.NEWLINE 插入新行。至于架构,我会先探索以下哪个选项最适合:stackoverflow.com/questions/26218444/…(当然:@AAFF 的问题太宽泛,无法做出准确的建议)。您的建议类似于我的 选项 1 ;-)
  • 很公平:D 这确实是一个很好的答案,我会确保我记住它以备将来使用:D
猜你喜欢
  • 2016-03-20
  • 2013-06-05
  • 1970-01-01
  • 1970-01-01
  • 2016-09-02
  • 2015-10-13
  • 1970-01-01
  • 1970-01-01
  • 2010-09-10
相关资源
最近更新 更多