如何在 Struts 中为 Mysql 的搜索结果创建 pdf

发布于 2024-08-15 12:27:19 字数 65 浏览 5 评论 0原文

我想根据使用 Struts 从 Mysql DB 传入的值创建一个 pdf。有人可以帮助我指出任何工具或如何处理吗?

I want to create a pdf according to Incoming Value From Mysql DB using Struts.Could Anyone help me by point any tool or how to process?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

新人笑 2024-08-22 12:27:19

您可以使用iText - 一个用于创建PDF文档的成熟开源库。以下示例基于 Struts 1,但可能对您有帮助:

public class PDF extends Action {
    public ActionForward perform(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    try {

        Document document = new Document();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baos);
        document.open();
        document.add(new Paragraph("Hello World"));
        //
        // add your data here ...
        //
        document.close();

        response.setContentType("application/pdf");
        response.setContentLength(baos.size());
        ServletOutputStream out = response.getOutputStream();
        baos.writeTo(out);
        out.flush();
    } catch (Exception e2) {
        System.out.println("Error in " + getClass().getName() + "\n" + e2);
    }
    return null;
    }
}

You can use iText - a mature open source library for creating PDF documents. The following example is Struts 1 based, but may help you:

public class PDF extends Action {
    public ActionForward perform(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    try {

        Document document = new Document();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baos);
        document.open();
        document.add(new Paragraph("Hello World"));
        //
        // add your data here ...
        //
        document.close();

        response.setContentType("application/pdf");
        response.setContentLength(baos.size());
        ServletOutputStream out = response.getOutputStream();
        baos.writeTo(out);
        out.flush();
    } catch (Exception e2) {
        System.out.println("Error in " + getClass().getName() + "\n" + e2);
    }
    return null;
    }
}
天赋异禀 2024-08-22 12:27:19

除了 iText 之外,您还可以使用 JasperResports - 它构建于之上iText,但为您的报告提供了 GUI 设计器(然后可以将其导出为 pdf、excel、打印机等)。

但无论哪种方式,你都必须阅读和学习很多东西;)

In addition to iText, you can use JasperResports - it is built ontop of iText, but provides a GUI designer for your report (which can be then exported to pdf, excel, printer, etc.).

But either way you will have to read and learn a lot ;)

却一份温柔 2024-08-22 12:27:19

Docmosis 将允许您以 Doc 或 ODT 格式创建文档的基础作为模板。然后,您可以在渲染 PDF 之前使用 mysql 数据来控制要对模板执行的操作(插入数据、删除数据、选择不同的模板等)。我不确定你的“具有复杂用户界面的 pdf”是什么意思。 Docmosis 可以让您制作相当复杂的文档模板(感谢 OpenOffice),而 Jasper 可以让您编写几乎任何您可能需要的输出结果。

Docmosis will let you create the basis of your document in Doc or ODT format as a template. You can then use your mysql data to control what you want to do to the template (insert data, remove data, pick a different template etc) before you render the PDF. I'm not sure what your "pdf with complex UI" means. Docmosis lets you template fairly sophistocated documents (thanks to OpenOffice), but Jasper lets you code pretty much any output result you could need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文