Spring MVC PDF下载

Spring MVC PDF下载

PDF导出接口

package mypackage.export.pdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;

/**
 * PDF导出接口
 * @author Henry
 *
 */
public interface IPdfExport {
    /**
     * 获得文档对象
     * @return
     */
    public Document getDocument();
    /**
     * 生成文档内容
     * @throws DocumentException
     */
    public void generate() throws DocumentException;
    /**
     * 获得文件输出名称
     * @return
     */
    public String getOutFileName();

}

Spring MVC View实现

package mypackage.controller.view;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.AbstractView;

import mypackage.pdf.IPdfExport;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * PDF导出视图
 * @author Henry
 *
 */
@Component
public class PdfStamperView extends AbstractView {
    /** PDF导出对象 */
    public static final String BEAN = "PDF.BEAN";

    public PdfStamperView(){
        setContentType("application/pdf");
    }


    @Override
    protected boolean generatesDownloadContent() {
        return true;
    }

    @Override
    protected final void renderMergedOutputModel(
            Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        IPdfExport export = (IPdfExport)model.get(BEAN);
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(export.getOutFileName().getBytes(), "ISO8859-1"));
        PdfWriter.getInstance(export.getDocument(), response.getOutputStream());
        export.getDocument().open();
        export.generate();
        export.getDocument().close();
    }
}

Spring Controller

package mypackage.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class PdfController{

    /**
     * PDF导出
     * @return
     */
    @RequestMapping("/pdf")
    public String pdf(Model model){
        IPdfExport pdf = null;
        //这里只需要初始化pdf实现对象实例
        ...
        model.addAttribute(PdfStamperView.BEAN, pdf);
        return "pdfStamperView";
    }

}

PDF导出实现

package mypackage.export.pdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

/**
 * 账单PDF导出
 * @author Henry
 *
 */
public class BillPdf implements IPdfExport {

    protected Document doc;
    /** 输出文件名 */
    protected String outFileName;

    protected Font yh10;

    protected Font yh10b;

    protected Font yh16b;

    public BillPdf(String accountId,String opponentId,Map<String, Object> pdfData,Date startDate,Date endDate) {
        FontFactory.register(TradeConfirmationPdf.class.getResource("/")+ "fonts/MSYH.TTC");
        doc = new Document(PageSize.A4.rotate(), 40, 40, 44, 44);
        yh10 = FontFactory.getFont("微软雅黑",BaseFont.IDENTITY_H,10);
        yh10b = FontFactory.getFont("微软雅黑",BaseFont.IDENTITY_H,10,Font.BOLD);
        yh16b = FontFactory.getFont("微软雅黑",BaseFont.IDENTITY_H,16,Font.BOLD);
        this.outFileName = "测试.pdf";
    }

    public Document getDocument() {
        return this.doc;
    }

    /**
     * 生成PDF文档
     */
    public void generate() throws DocumentException {
        Paragraph title = new Paragraph(rise,yh16b);
        title.setSpacingBefore(10);
        title.setSpacingAfter(20);
        title.setAlignment(Element.ALIGN_CENTER);
        doc.add(title);

        PdfPTable headerTable = new PdfPTable(3);
        headerTable.setTotalWidth(new float[]{135f,330f,305f});
        headerTable.setLockedWidth(true);
        PdfPCell cell = new PdfPCell(yh10("账户编号"));
        cell.setBorder(0);
        headerTable.addCell(cell);
        addCell(headerTable, "1234567890");
        addCell(headerTable, " ");

        cell = new PdfPCell(yh10("账户名称"));
        cell.setBorder(0);
        headerTable.addCell(cell);
        addCell(headerTable, "0123456789");
        addCell(headerTable, " ");

        cell = new PdfPCell(yh10("报告起始日期"));
        cell.setBorder(0);
        headerTable.addCell(cell);
        addCell(headerTable, "2017-01-01");
        addCell(headerTable, " ");

        cell = new PdfPCell(yh10("报告截止日期"));
        cell.setBorder(0);
        headerTable.addCell(cell);
        addCell(headerTable, "2017-01-01");
        addCell(headerTable, " ");

        addCell(headerTable, " ");
        addCell(headerTable, " ");
        addCell(headerTable, " ");
        doc.add(headerTable);
    }

    public void addCell(PdfPTable table,String text,int flag){
        PdfPCell cell = new PdfPCell(yh10(text));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_CENTER);
        cell.setBorder(0);
        cell.setMinimumHeight(16.5f);
        if(flag == 1) cell.setBorderWidthLeft(1f);
        if(flag == 2) cell.setBorderWidthRight(1f);
        table.addCell(cell);
    }

    public String getOutFileName() {
        return this.outFileName;
    }

    public Paragraph yh10(String text){
        return new Paragraph(text,yh10);
    }

    public Paragraph yh10Bold(String text){
        return new Paragraph(text,yh10b);
    }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值