// 创建一个Document对象 System.out.println("&&&&&&&&&&&&&&"); Document document = new Document(); try{
PdfWriter.getInstance(document, new FileOutputStream("D:\\测试.pdf"));
// 添加PDF文档的一些信息 document.addTitle("Hello World example"); document.addAuthor("Bruno Lowagie"); document.addSubject("This example explains how to add metadata."); document.addKeywords("iText, Hello World, step 3, metadata"); document.addCreator("My program using iText"); BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN);
// 打开文档,将要写入内容 document.open();
Chunk chunk1 = new Chunk("ceshi1\n"); Chunk chunk2 = new Chunk("ceshi2");
Phrase phr = new Phrase(); phr.add(chunk1); phr.add(chunk2);
Paragraph par = new Paragraph(chunk2); par.setAlignment(2);
Paragraph para = new Paragraph(phr); para.setAlignment(1); document.add(par); document.add(para); document.add(new Paragraph("Zhang San!"));
document.newPage(); Table table = new Table(3); table.setBorderWidth(1); table.setBorderColor(Color.green); table.setPadding(5); table.setSpacing(2);
Paragraph pa = new Paragraph("头部",fontChinese); pa.setAlignment(2);
Cell cell = new Cell(pa); cell.setBackgroundColor(new Color(255, 0, 255)); cell.setHeader(true); cell.setColspan(3); table.addCell(cell); table.endHeaders(); cell = new Cell("example cell with colspan 1 and rowspan 2"); cell.setRowspan(2); cell.setBorderColor(new Color(255, 0, 0)); table.addCell(cell); Point p = new Point(); table.addCell("1.1"); table.addCell("2.1"); table.addCell("1.2"); table.addCell("2.2"); table.addCell("cell test1"); cell = new Cell("big cell"); cell.setRowspan(2); cell.setColspan(2); table.addCell(cell); table.addCell("cell test2"); document.add(table); }catch(Exception e){ System.out.println("1111111111111111"); e.printStackTrace(); } System.out.println("4444444444444444"); document.close();我是这样用itext生成一个PDF格式的文件的~~ 就是说不让生成的这个PDF文件保存在服务器中,直接下载~
PdfWriter.getInstance(document, new FileOutputStream("D:\\测试.pdf")); //把你这里的new FileOutputStream("D:\\测试.pdf")换成从response获取的response.getOutputStream();就可以了 PdfWriter.getInstance(document, response.getOutputStream());
public void writePdf(OutputStream os){ // 创建一个Document对象 System.out.println("&&&&&&&&&&&&&&"); Document document = new Document(); try{
PdfWriter.getInstance(document, os);
// 添加PDF文档的一些信息 document.addTitle("Hello World example"); document.addAuthor("Bruno Lowagie"); document.addSubject("This example explains how to add metadata."); document.addKeywords("iText, Hello World, step 3, metadata"); document.addCreator("My program using iText"); BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN);
// 打开文档,将要写入内容 document.open();
Chunk chunk1 = new Chunk("ceshi1\n"); Chunk chunk2 = new Chunk("ceshi2");
Phrase phr = new Phrase(); phr.add(chunk1); phr.add(chunk2);
Paragraph par = new Paragraph(chunk2); par.setAlignment(2);
Paragraph para = new Paragraph(phr); para.setAlignment(1); document.add(par); document.add(para); document.add(new Paragraph("Zhang San!"));
document.newPage(); Table table = new Table(3); table.setBorderWidth(1); table.setBorderColor(Color.green); table.setPadding(5); table.setSpacing(2);
Paragraph pa = new Paragraph("头部",fontChinese); pa.setAlignment(2);
Cell cell = new Cell(pa); cell.setBackgroundColor(new Color(255, 0, 255)); cell.setHeader(true); cell.setColspan(3); table.addCell(cell); table.endHeaders(); cell = new Cell("example cell with colspan 1 and rowspan 2"); cell.setRowspan(2); cell.setBorderColor(new Color(255, 0, 0)); table.addCell(cell); Point p = new Point(); table.addCell("1.1"); table.addCell("2.1"); table.addCell("1.2"); table.addCell("2.2"); table.addCell("cell test1"); cell = new Cell("big cell"); cell.setRowspan(2); cell.setColspan(2); table.addCell(cell); table.addCell("cell test2"); document.add(table); }catch(Exception e){ System.out.println("1111111111111111"); e.printStackTrace(); } System.out.println("4444444444444444"); document.close(); }//然后在servlet或者action里边执行如下方法就可以了 writePdf(response.getOutputStream());
大哥,我按照你说的改了,还是不行弹出一个对话框 上面写的 format error: not a PDF or corrupted. 点击确定后弹出下载提示,下下来是错误的
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置输出格式为pdf
response.setContentType("application/pdf");
//获取输出流
OutputStream os = response.getOutputStream();
//在这里通过outputstream流进行文件输出
//清空流
os.flush();
//关闭流
os.close();
}
你要保存到本地需要一个文件流FileOutputStream,要让别人下载的话只需要将这个流换成能连接到客户端的即可,这个链接到客户端的流可以从HttpServletResponse.getOutputStrean()获取,直接给这里写字节码即可.
System.out.println("&&&&&&&&&&&&&&");
Document document = new Document();
try{
PdfWriter.getInstance(document, new FileOutputStream("D:\\测试.pdf"));
// 添加PDF文档的一些信息
document.addTitle("Hello World example");
document.addAuthor("Bruno Lowagie");
document.addSubject("This example explains how to add metadata.");
document.addKeywords("iText, Hello World, step 3, metadata");
document.addCreator("My program using iText"); BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN);
// 打开文档,将要写入内容
document.open();
Chunk chunk1 = new Chunk("ceshi1\n");
Chunk chunk2 = new Chunk("ceshi2");
Phrase phr = new Phrase();
phr.add(chunk1);
phr.add(chunk2);
Paragraph par = new Paragraph(chunk2);
par.setAlignment(2);
Paragraph para = new Paragraph(phr); para.setAlignment(1);
document.add(par);
document.add(para);
document.add(new Paragraph("Zhang San!"));
document.newPage();
Table table = new Table(3);
table.setBorderWidth(1);
table.setBorderColor(Color.green);
table.setPadding(5);
table.setSpacing(2);
Paragraph pa = new Paragraph("头部",fontChinese);
pa.setAlignment(2);
Cell cell = new Cell(pa);
cell.setBackgroundColor(new Color(255, 0, 255));
cell.setHeader(true);
cell.setColspan(3);
table.addCell(cell);
table.endHeaders();
cell = new Cell("example cell with colspan 1 and rowspan 2");
cell.setRowspan(2);
cell.setBorderColor(new Color(255, 0, 0));
table.addCell(cell);
Point p = new Point();
table.addCell("1.1");
table.addCell("2.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("cell test1");
cell = new Cell("big cell");
cell.setRowspan(2);
cell.setColspan(2);
table.addCell(cell);
table.addCell("cell test2");
document.add(table);
}catch(Exception e){
System.out.println("1111111111111111");
e.printStackTrace();
}
System.out.println("4444444444444444");
document.close();我是这样用itext生成一个PDF格式的文件的~~
就是说不让生成的这个PDF文件保存在服务器中,直接下载~
PdfWriter.getInstance(document, new FileOutputStream("D:\\测试.pdf"));
//把你这里的new FileOutputStream("D:\\测试.pdf")换成从response获取的response.getOutputStream();就可以了
PdfWriter.getInstance(document, response.getOutputStream());
public void writePdf(OutputStream os){
// 创建一个Document对象
System.out.println("&&&&&&&&&&&&&&");
Document document = new Document();
try{
PdfWriter.getInstance(document, os);
// 添加PDF文档的一些信息
document.addTitle("Hello World example");
document.addAuthor("Bruno Lowagie");
document.addSubject("This example explains how to add metadata.");
document.addKeywords("iText, Hello World, step 3, metadata");
document.addCreator("My program using iText"); BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN);
// 打开文档,将要写入内容
document.open();
Chunk chunk1 = new Chunk("ceshi1\n");
Chunk chunk2 = new Chunk("ceshi2");
Phrase phr = new Phrase();
phr.add(chunk1);
phr.add(chunk2);
Paragraph par = new Paragraph(chunk2);
par.setAlignment(2);
Paragraph para = new Paragraph(phr); para.setAlignment(1);
document.add(par);
document.add(para);
document.add(new Paragraph("Zhang San!"));
document.newPage();
Table table = new Table(3);
table.setBorderWidth(1);
table.setBorderColor(Color.green);
table.setPadding(5);
table.setSpacing(2);
Paragraph pa = new Paragraph("头部",fontChinese);
pa.setAlignment(2);
Cell cell = new Cell(pa);
cell.setBackgroundColor(new Color(255, 0, 255));
cell.setHeader(true);
cell.setColspan(3);
table.addCell(cell);
table.endHeaders();
cell = new Cell("example cell with colspan 1 and rowspan 2");
cell.setRowspan(2);
cell.setBorderColor(new Color(255, 0, 0));
table.addCell(cell);
Point p = new Point();
table.addCell("1.1");
table.addCell("2.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("cell test1");
cell = new Cell("big cell");
cell.setRowspan(2);
cell.setColspan(2);
table.addCell(cell);
table.addCell("cell test2");
document.add(table);
}catch(Exception e){
System.out.println("1111111111111111");
e.printStackTrace();
}
System.out.println("4444444444444444");
document.close();
}//然后在servlet或者action里边执行如下方法就可以了
writePdf(response.getOutputStream());
上面写的 format error: not a PDF or corrupted.
点击确定后弹出下载提示,下下来是错误的
另还有个小问题,如果下载这个文件的用户没有安装能够识别PDF文件的软件
则下载的文件默认后缀名为.do,请为有办法让她默认为.pdf不