java后台生成PDF传给前台界面,前台界面生成下载链接IText,
所用的jar包百度云资源(itextpdf-5.5.8,itext-asian)
不会发生什么配置文件丢失的jar包,我也是翻了好久才找到。
链接:https://pan.baidu.com/s/1rSp7D4aWBvx25vmv2mQWNg 密码:hfcj
1.源码,我自己测试过的。主要生成PDF中表格数据的地方,若是图片等其他信息,等待有需求时,进一步实现。
直接JAVA指定路径实现。
public class ExportPdfUtil {
private Font FontChinese;
public void simplePDF(String deviceType) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
FontChinese = new Font(bfChinese, 12, Font.NORMAL);
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("E:\\FIRSTPDF.pdf"));
// PdfWriter.getInstance(document, baos);
document.open();
PdfPTable table = null;
if (deviceType == “固话”) {
table = new PdfPTable(6);
table.addCell(getCell(“清单类型”, 1, 1));
table.addCell(getCell(“使用设备”, 1, 1));
table.addCell(getCell(“日期”, 1, 1));
table.addCell(getCell(“起始时间”, 1, 1));
table.addCell(getCell(“通话时长”, 1, 1));
table.addCell(getCell(“通信费用”, 1, 1));
table.addCell(getCell("固网彩铃爱音乐铃声费", 1, 1));
table.addCell(getCell("2162146506", 1, 1));
table.addCell(getCell("20180801", 1, 1));
table.addCell(getCell("00:00:00", 1, 1));
table.addCell(getCell("0", 1, 1));
table.addCell(getCell("¥0.00", 1, 1));
} else if (deviceType == "手机") {
table = new PdfPTable(10);
table.addCell(getCell("通话类型", 1, 1));
table.addCell(getCell("开始时间", 1, 1));
table.addCell(getCell("时长", 1, 1));
table.addCell(getCell("呼叫类型", 1, 1));
table.addCell(getCell("对方号码", 1, 1));
table.addCell(getCell("通话地点", 1, 1));
table.addCell(getCell("基本费用(元)", 1, 1));
table.addCell(getCell("信息费用(元)", 1, 1));
table.addCell(getCell("其他费用(元)", 1, 1));
table.addCell(getCell("总费用(元)", 1, 1));
table.addCell(getCell("固网彩铃爱音乐铃声费", 1, 1));
table.addCell(getCell("2162146506", 1, 1));
table.addCell(getCell("20180801", 1, 1));
table.addCell(getCell("00:00:00", 1, 1));
table.addCell(getCell("0", 1, 1));
table.addCell(getCell("¥0.00", 1, 1));
table.addCell(getCell("20180801", 1, 1));
table.addCell(getCell("00:00:00", 1, 1));
table.addCell(getCell("0", 1, 1));
table.addCell(getCell("¥0.00", 1, 1));
}
table.setWidthPercentage(110); // 设置表格宽度
document.add(table);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private PdfPCell getCell(String cellValue, int colspan, int rowSpan) {
PdfPCell cell = new PdfPCell();
try {
cell = new PdfPCell(new Phrase(cellValue, FontChinese));
cell.setRowspan(rowSpan);
cell.setColspan(colspan);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
} catch (Exception e) {
e.printStackTrace();
}
return cell;
}
public static void main(String[] args) {
ExportPdfUtil c = new ExportPdfUtil();
c.simplePDF("手机");
}
}
2.java配合浏览器实现PDF文件下载
以下是将java的PDF文件传输给浏览器下载。配合任何一种controller都可以,我这里用的是SpringBoot的controller.
跟上面的代码非常相似。就是加入了流,将后台的指定路径下载,转交给浏览器指定路径去下载。
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import org.springframework.stereotype.Service;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.PdfStructTreeController.returnType;
@Service(“exportPdfUtil”)
public class ExportPdfUtil {
private Font FontChinese;
public ByteArrayOutputStream simplePDF(String deviceType) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
FontChinese = new Font(bfChinese, 12, Font.NORMAL);
Document document = new Document();
PdfWriter.getInstance(document, baos);
document.open();
PdfPTable table = null;
if (deviceType == "固话") {
table = new PdfPTable(6);//设置一共多少列表的
table.addCell(getCell("清单类型", 1, 1));
table.addCell(getCell("使用设备", 1, 1));
table.addCell(getCell("日期", 1, 1));
table.addCell(getCell("起始时间", 1, 1));
table.addCell(getCell("通话时长", 1, 1));
table.addCell(getCell("通信费用", 1, 1));
table.addCell(getCell("固网彩铃爱音乐铃声费", 1, 1));
table.addCell(getCell("2162146506", 1, 1));
table.addCell(getCell("20180801", 1, 1));
table.addCell(getCell("00:00:00", 1, 1));
table.addCell(getCell("0", 1, 1));
table.addCell(getCell("¥0.00", 1, 1));
} else if (deviceType == "手机") {
table = new PdfPTable(10);
table.addCell(getCell("通话类型", 1, 1));
table.addCell(getCell("开始时间", 1, 1));
table.addCell(getCell("时长", 1, 1));
table.addCell(getCell("呼叫类型", 1, 1));
table.addCell(getCell("对方号码", 1, 1));
table.addCell(getCell("通话地点", 1, 1));
table.addCell(getCell("基本费用(元)", 1, 1));
table.addCell(getCell("信息费用(元)", 1, 1));
table.addCell(getCell("其他费用(元)", 1, 1));
table.addCell(getCell("总费用(元)", 1, 1));
table.addCell(getCell("固网彩铃爱音乐铃声费", 1, 1));
table.addCell(getCell("2162146506", 1, 1));
table.addCell(getCell("20180801", 1, 1));
table.addCell(getCell("00:00:00", 1, 1));
table.addCell(getCell("0", 1, 1));
table.addCell(getCell("¥0.00", 1, 1));
table.addCell(getCell("20180801", 1, 1));
table.addCell(getCell("00:00:00", 1, 1));
table.addCell(getCell("0", 1, 1));
table.addCell(getCell("¥0.00", 1, 1));
}
table.setWidthPercentage(110); // 设置表格宽度
document.add(table);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
return baos;
}
private PdfPCell getCell(String cellValue, int colspan, int rowSpan) {
PdfPCell cell = new PdfPCell();
try {
cell = new PdfPCell(new Phrase(cellValue, FontChinese));
cell.setRowspan(rowSpan);
cell.setColspan(colspan);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
} catch (Exception e) {
e.printStackTrace();
}
return cell;
}
/* public static void main(String[] args) {
ExportPdfUtil c = new ExportPdfUtil();
c.simplePDF(“手机”);
}*/
}
上面是util,下面是controller中的方法。。
//下载PDF
@RequestMapping(“/downloadPdf”)
public void downloadPdf(HttpServletRequest request,HttpServletResponse response,Model model){
try {
ByteArrayOutputStream baos=exportPdfUtil.simplePDF(“手机”);
//设置请求返回类型
response.setContentType(“application/pdf”);
response.setHeader(“Content-Disposition”, “attachment; filename=file.pdf”);
response.setContentLength(baos.size());
OutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
out.close();
} catch (Exception e) {
// TODO: handle exception
}
}
这样,前台使用window.open()方式,访问controller,即可实现pdf下载,其中具体数据,根据你们自己业务的情况自行发挥。