2015-05-18 33 views
3

我有一个现有的servlet,给了我一个PDF字节数组:生成一个包含Servlet的PDF文件,并显示PDF在新浏览器标签 - PDF文件是空

byte[] pdf = myService.getPdf(myArgs); 
response.setContentType("application/pdf"); 
response.setHeader("Expires", "0"); 
response.setHeader("Cache-Control", "must-revalidate, postcheck=0, pre-check=0"); 
response.setHeader("Pragma", "public"); 
response.setContentLength(pdf.length); 
ServletOutputStream out = response.getOutputStream(); 
out.write(pdf); 
out.flush(); 
out.close(); 

// for test: byte[] pdf 
File testfile = new File("C:\\mytestpath\\mytest.pdf"); 
FileOutputStream fos = new FileOutputStream(testfile); 
fos.write(pdf); 
fos.flush(); 
fos.close(); 

我在我的servlet一个写测试pdf文件以检查myService.getPdf()的返回值。测试文件没问题,我可以在acrobat reader中打开这个有效的文件。

现在我的jQuery的JavaScript代码:

function generatePDF(url) { 
    currentRequest = $.ajax({ 
    url: url, 
    type: "GET", 
    success: function(data) { 
     var blob = new Blob([data], {type: 'application/pdf'}); 
     var fileURL = window.URL.createObjectURL(blob); 
     window.open(fileURL, "_blank");    
     }, 
    error: function() {   
     console.error("test in generatePDF error"); 
     // use here other jquery mobile functions 
     } 
    }); 
} 

我想生成我的servlet一个PDF字节数组,并显示在新的浏览器选项卡中的PDF。 在我的实现中,打开了一个新的浏览器选项卡(地址栏:blob:http%3A // localhost%3A8080/3fd5808b-758b-4076-94c9-af9884f631a3)。但PDF文件是空的,PDF文件的页面大小是可以的。 我该如何解决这个问题?我需要一个名为myid.pdf的有效PDF在新的浏览器标签中。

感谢对尤尔提示,托马斯

回答

1

你可以只在新窗口中打开您的服务器URL。不需要AJAX调用或JQuery。

在你的servlet中,如果他选择保存文件,你可以添加头部提示浏览器提示用户的文件名。

response.setHeader("Content-Disposition", "attachment;filename=" + pdfName); 
+0

我完全同意,也没有必要使用AJAX或jQuery的请求。您只需浏览标题,向浏览器建议如何处理文件... +1 – MaVRoSCy

+0

是的,我选择了一个没有AJAX的解决方案。 –

0
public class PdfSheetGen extends HttpServlet { 
    private static final long serialVersionUID = 1L; 



    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     response.setContentType("appliction/pdf"); 
     Document document=new Document(); 
     Connection con=null; 
      PreparedStatement ps=null; 
      String sql=null; 
      ResultSet rs=null; 

      try{ 
       PdfWriter.getInstance(document, new FileOutputStream("D:/Details.pdf")); 
       document.addAuthor("Management"); 
       document.addCreationDate(); 
       document.addProducer(); 
       document.addCreator("tech"); 
       document.addTitle("Inventory"); 
       document.setPageSize(PageSize.LETTER); 
       document.open(); 
       Image image=Image.getInstance("D:\\branch.png"); 
       image.scaleAbsolute(120f, 120f); 
       document.add(image); 

       PdfPTable table=new PdfPTable(5); 
       PdfPCell cell = new PdfPCell (new Paragraph ("Details")); 

       cell.setColspan (5); 
       cell.setHorizontalAlignment (Element.ALIGN_CENTER); 
       cell.setPadding (10.0f); 
       cell.setBackgroundColor (new BaseColor (140, 221, 8)); 
       table.addCell(cell); 
       table.addCell("ID"); 
       table.addCell("Branch_Name"); 
       table.addCell("Address"); 
       table.addCell("Contact_p"); 
       table.addCell("Email"); 

       con=DbConnection.getCon(); 
       sql="select * from branch"; 
       ps=con.prepareStatement(sql); 
       rs=ps.executeQuery(); 

       while (rs.next()) { 
        table.addCell(rs.getString("id")); 
       table.addCell(rs.getString("Branch_name")); 
       table.addCell(rs.getString("address")); 
       table.addCell(rs.getString("contactp")); 
       table.addCell(rs.getString("email")); 

      } 
       document.add(table); 
       document.add(new Paragraph("Branch Details Generated On - "+new Date().toString())); 
       document.close(); 

      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
      finally{ 
       try{ 
        if(con!=null){ 
         con.close(); 
        } 
        if(ps!=null){ 
         ps.close(); 
        } 
        if(rs!=null){ 
         rs.close(); 
        } 
       }catch(Exception e){ 
        e.printStackTrace(); 
       } 
      } 


    } 
相关问题