pom:
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.0.6</version>
</dependency>
控制器Controller
@RequestMapping(value = "/purchase/order/print", method = RequestMethod.GET)
public ModelAndView printInvoice(@RequestParam Long id) throws IOException {
List<Map> list = purchaseOrderService.getList(id);
Map<String, Object> model = new HashMap<>();
model.put("sheet", list);
model.put("row", 8);
String[] title = new String[]{
"商品條碼","商品名稱", "規格/單位", "進價/元", "折扣", "採購數量",
"進貨金額/元"
};
model.put("titles", title);
return new ModelAndView(new ViewPDF(), model);
}
ViewPDF文件:
/**
* @作者:huangliang
* @時間:2020-03-16 15:35
* @註釋:ViewPDF
*/
public class ViewPDF extends AbstractPdfView {
@Override
protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) throws Exception {
String fileName = new java.util.Date().getTime() + "_order.pdf";
response.setCharacterEncoding("UTF-8");
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "filename=" + new String(fileName.getBytes(), "iso8859-1"));
List<Map> products = (List<Map>) model.get("sheet");
PdfUtil pdfUtil = new PdfUtil();
int row = (int) model.get("row");
String[] title = (String[]) model.get("titles");
pdfUtil.createPDF(document, writer, products, row,title);
}
}
如果不想要這種直接在瀏覽器中預覽的效果,而是直接下載文件的話就把response.setHeader()方法裏面的參數改成下面的,相當於加了一個attachment,以附件形式下載
response.setHeader("Content-Disposition","attachment;filename=" + new String(fileName.getBytes(), "iso8859-1"));
PdfUtil類:
/**
* @作者:huangliang
* @時間:2020-03-16 15:40
* @註釋:PdfUtil
*/
public class PdfUtil {
public void createPDF(Document document, PdfWriter writer, List<Map> products, int row, String[] titles) throws IOException {
try {
document.setPageSize(PageSize.A4.rotate());//設置橫向打印
document.addTitle("採購訂單");
document.addAuthor("huangliang");
document.addSubject("product sheet.");
document.addKeywords("product.");
document.open();
PdfPTable table = createPurchaseOrderTable(products, row, titles);
document.add(table);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} finally {
document.close();
}
}
public static PdfPTable createPurchaseOrderTable(List<Map> products, int row, String[] titles) throws IOException, DocumentException {
PdfPTable table = new PdfPTable(row);//生成*列表格
table.setWidthPercentage(105);
PdfPCell cell;
int size = 20;
ClassPathResource resource = new ClassPathResource("static/report" + File.separator + "wryh.ttf");
InputStream inputStream = resource.getInputStream();
byte[] st1 = new byte[inputStream.available()];
inputStream.read(st1);
BaseFont baseFont = BaseFont.createFont("wryh.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED, BaseFont.NOT_CACHED,
st1, st1);
Font font = new Font(baseFont);
//服務器上改成下面這種
//BaseFont baseFont = BaseFont.createFont(new ClassPathResource("/static/report/wryh.ttf").getPath(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// Font font = new Font(baseFont);
PdfPCell title = new PdfPCell(new Phrase("採購訂單", font));
title.setColspan(row);//設置所佔列數
title.setFixedHeight(size * 2);//設置高度
title.setHorizontalAlignment(Element.ALIGN_CENTER);//設置水平居中
title.setVerticalAlignment(Element.ALIGN_MIDDLE);//設置垂直居中
table.addCell(title);
PdfPCell er1 = new PdfPCell(new Phrase("訂單編號", font));
er1.setColspan(2);//設置所佔列數
er1.setFixedHeight(size * 2);//設置高度
er1.setHorizontalAlignment(Element.ALIGN_CENTER);//設置水平居中
er1.setVerticalAlignment(Element.ALIGN_MIDDLE);//設置垂直居中
table.addCell(er1);
PdfPCell er2 = new PdfPCell(new Phrase("CG202003110004", font));
er2.setColspan(2);//設置所佔列數
er2.setFixedHeight(size * 2);//設置高度
er2.setHorizontalAlignment(Element.ALIGN_CENTER);//設置水平居中
er2.setVerticalAlignment(Element.ALIGN_MIDDLE);//設置垂直居中
table.addCell(er2);
PdfPCell er3 = new PdfPCell(new Phrase("訂單總金額", font));
er3.setColspan(2);//設置所佔列數
er3.setFixedHeight(size * 2);//設置高度
er3.setHorizontalAlignment(Element.ALIGN_CENTER);//設置水平居中
er3.setVerticalAlignment(Element.ALIGN_MIDDLE);//設置垂直居中
table.addCell(er3);
PdfPCell er4 = new PdfPCell(new Phrase("50000", font));
er4.setColspan(2);//設置所佔列數
er4.setFixedHeight(size * 2);//設置高度
er4.setHorizontalAlignment(Element.ALIGN_CENTER);//設置水平居中
er4.setVerticalAlignment(Element.ALIGN_MIDDLE);//設置垂直居中
table.addCell(er4);
addRow(table,font);
for(String str:titles){
cell = new PdfPCell(new Phrase(str, font));
cell.setFixedHeight(size);
if("商品名稱".equals(str)){
cell.setColspan(2);
}
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//設置水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//設置垂直居中
table.addCell(cell);
}
for (int i = 0; i < products.size(); i++) {
cell = new PdfPCell(new Phrase(products.get(i).get("goodsCode").toString(), font));
cell.setFixedHeight(size);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//設置水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//設置垂直居中
table.addCell(cell);
cell = new PdfPCell(new Phrase(products.get(i).get("goodsName").toString(), font));
cell.setColspan(2);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//設置水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//設置垂直居中
table.addCell(cell);
cell = new PdfPCell(new Phrase(products.get(i).get("goodsSpecifications")+"/"+products.get(i).get("goodsUnit"), font));
cell.setFixedHeight(size);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//設置水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//設置垂直居中
table.addCell(cell);
cell = new PdfPCell(new Phrase(products.get(i).get("goodsOnMoney").toString(), font));
cell.setFixedHeight(size);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//設置水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//設置垂直居中
table.addCell(cell);
cell = new PdfPCell(new Phrase(products.get(i).get("discount")+"%", font));
cell.setFixedHeight(size);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//設置水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//設置垂直居中
table.addCell(cell);
cell = new PdfPCell(new Phrase(products.get(i).get("buyNumber").toString(), font));
cell.setFixedHeight(size);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//設置水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//設置垂直居中
table.addCell(cell);
cell = new PdfPCell(new Phrase(products.get(i).get("totalMoney").toString(), font));
cell.setFixedHeight(size);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//設置水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//設置垂直居中
table.addCell(cell);
}
return table;
}
private static void addRow(PdfPTable table,Font font) {
PdfPCell er1 = new PdfPCell(new Phrase("制單日期", font));
er1.setColspan(2);//設置所佔列數
er1.setFixedHeight(30);//設置高度
er1.setHorizontalAlignment(Element.ALIGN_CENTER);//設置水平居中
er1.setVerticalAlignment(Element.ALIGN_MIDDLE);//設置垂直居中
table.addCell(er1);
PdfPCell er2 = new PdfPCell(new Phrase(DateUtil.getNow(), font));
er2.setColspan(2);//設置所佔列數
er2.setFixedHeight(30);//設置高度
er2.setHorizontalAlignment(Element.ALIGN_CENTER);//設置水平居中
er2.setVerticalAlignment(Element.ALIGN_MIDDLE);//設置垂直居中
table.addCell(er2);
PdfPCell er3 = new PdfPCell(new Phrase("供應商", font));
er3.setColspan(2);//設置所佔列數
er3.setFixedHeight(30);//設置高度
er3.setHorizontalAlignment(Element.ALIGN_CENTER);//設置水平居中
er3.setVerticalAlignment(Element.ALIGN_MIDDLE);//設置垂直居中
table.addCell(er3);
PdfPCell er4 = new PdfPCell(new Phrase("四川新希望集團", font));
er4.setColspan(2);//設置所佔列數
er4.setFixedHeight(30);//設置高度
er4.setHorizontalAlignment(Element.ALIGN_CENTER);//設置水平居中
er4.setVerticalAlignment(Element.ALIGN_MIDDLE);//設置垂直居中
table.addCell(er4);
}
}
wryh.ttf 文件爲字體文件(微軟雅黑),需要的可以留言.
效果圖: