简体   繁体   English

在Struts 1.x中导出为pdf

[英]Export to pdf in struts 1.x

I have some values to be exported to PDF, how can i do this in struts? 我有一些要导出到PDF的值,如何在struts中做到这一点?

I would like to do the following, have option button or link when the user clicks on the button or link than the user should get a download option to download the exported PDF file. 我想执行以下操作,当用户单击按钮或链接时具有选项按钮或链接,那么用户应该获得下载下载的PDF文件下载选项。

I have a resultset which needs to be exported to PDF. 我有一个结果集,需要将其导出为PDF。

Please help me how to go about. 请帮我怎么做。

Regards 问候

Take a look to this example of an action class it may help you out. 看一下这个动作类的示例,它可能会对您有所帮助。

       import javax.servlet.http.HttpServletRequest;
       import javax.servlet.http.HttpServletResponse;
       import org.apache.struts.action.ActionForm;
       import org.apache.struts.action.ActionForward;
       import org.apache.struts.action.ActionMapping;
       import com.lowagie.text.pdf.*;
       import com.lowagie.text.*;
       import java.io.*;

        public class download extends org.apache.struts.action.Action {

        /* forward name="success" path="" */
        private static final String SUCCESS = "success";


        @Override
        public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Document document=new Document();
        System.out.println(clientIp);
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition","attachment;filename=temp.pdf");
        try
    {
    OutputStream out = response.getOutputStream();
    PdfWriter.getInstance(document,out);
            document.open();
            document.add(new Paragraph("Hello Pdf"));
            document.close();

    }
        finally
        {
          return mapping.findForward(SUCCESS);
        }
    }

Also update your struts-config.xml according to it. 还要根据它更新您的struts-config.xml。

Here is an example that may help you to do what you want: 这是一个示例,可以帮助您完成所需的操作:

import java.io.*;
import java.sql.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

public class CreatePDF{
    public static void main(String arg[])throws Exception{
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream("C:/data.pdf"));

        document.open();

        PdfPTable table = new PdfPTable(2);
        table.addCell("Name");
        table.addCell("Address");

        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(
                                        "jdbc:mysql://localhost:3306/test",
                                        "root", "root");

        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("Select * from data");

        while(rs.next()) {
            table.addCell(rs.getString("name"));
            table.addCell(rs.getString("address"));
        }

        document.add(table);
        document.close();
    }
}

But before implementing it you have to put itext.jar into your WEB-INF/lib folder. 但是在实施之前,您必须将itext.jar放入WEB-INF/lib文件夹中。 And you also found plenty of methods to manipulate your pdf file. 您还发现了很多方法来处理pdf文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM