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;
...
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);
}
}