亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用Spring MVC返回生成的pdf

使用Spring MVC返回生成的pdf

千萬里不及你 2019-11-13 14:04:28
我正在使用Spring MVC。我必須編寫一個服務,該服務將從請求主體中獲取輸入,將數據添加到pdf中,然后將pdf文件返回到瀏覽器。pdf文檔是使用itextpdf生成的。如何使用Spring MVC做到這一點。我試過使用這個@RequestMapping(value="/getpdf", method=RequestMethod.POST)public Document getPDF(HttpServletRequest request , HttpServletResponse response,       @RequestBody String json) throws Exception {    response.setContentType("application/pdf");    response.setHeader("Content-Disposition", "attachment:filename=report.pdf");    OutputStream out = response.getOutputStream();    Document doc = PdfUtil.showHelp(emp);    return doc;}生成pdf的showhelp函數。我只是暫時將一些隨機數據放入pdf中。public static Document showHelp(Employee emp) throws Exception {    Document document = new Document();    PdfWriter.getInstance(document, new FileOutputStream("C:/tmp/report.pdf"));    document.open();    document.add(new Paragraph("table"));    document.add(new Paragraph(new Date().toString()));    PdfPTable table=new PdfPTable(2);    PdfPCell cell = new PdfPCell (new Paragraph ("table"));    cell.setColspan (2);    cell.setHorizontalAlignment (Element.ALIGN_CENTER);    cell.setPadding (10.0f);    cell.setBackgroundColor (new BaseColor (140, 221, 8));                                      table.addCell(cell);                                        ArrayList<String[]> row=new ArrayList<String[]>();    String[] data=new String[2];    data[0]="1";    data[1]="2";    String[] data1=new String[2];    data1[0]="3";    data1[1]="4";    row.add(data);    row.add(data1);    for(int i=0;i<row.size();i++) {      String[] cols=row.get(i);      for(int j=0;j<cols.length;j++){        table.addCell(cols[j]);      }    }    document.add(table);    document.close();    return document;   }我確定這是錯誤的。我希望生成pdf并通過瀏覽器打開“保存/打開”對話框,以便可以將其存儲在客戶端的文件系統中。請幫幫我。
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 609 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號