Wednesday, August 19, 2015

Create and Download pdf from server using spring java and angular

11:45 PM By

Create Temporay file and then delete that file doesnot work.
So, We need to be carefull about FileInputStream and FileOutputStreams.
They must be closed before delete the file.
Here is complete example of how to create pdf and download pdf in two step:
---------------------------------------------------------------
@RequestMapping(value = "createPdf" , method=RequestMethod.POST)
    public @ResponseBody JSONResult createPdf(){
      JSONResult result = new JSONResult();
      File f = null;
      FileOutputStream out = null;
      try{
            f = File. createTempFile( "tmpPdf", ".pdf" , new File("D:\\temp" ));
            out = new FileOutputStream(f);
            IOUtils. copy( new FileInputStream( "E:\\fs.pdf" ), out );
            out.flush( );
            out.close( );
            result.setData( f.getAbsolutePath( ) );
            result.setSuccess( true );
          } catch ( Exception e ) {
                  e.printStackTrace( );
                  result.setSuccess( false );
                   if(null != out){
                         try {
                              out.close( );
                        } catch ( IOException e1 ) {
                              e1.printStackTrace();
                        }
                  }
                   if(null != f){
                        f.delete( );
                  }
            } finally {
                   if(null != out){
                         try {
                              out.close( );
                        } catch ( IOException e1 ) {
                              e1.printStackTrace();
                        }
                  }
            }
      return result;
    }
   
    @RequestMapping(value = "getPDF", method=RequestMethod.GET)
    public void getPdf(@QueryParam ("filePath" ) String filePath, HttpServletResponse response) throws IOException{
      File f = new File(filePath);
      if(f.exists( )){
            FileInputStream inputStream = new FileInputStream( filePath);
            IOUtils. copy( inputStream, response.getOutputStream( ) );
            response.setContentType( "application/pdf" );
            response.setHeader( "Content-Disposition", "attachment; filename=somefile.pdf");
            response.flushBuffer( );
            inputStream.close( );
            f.delete( );
      } else {
             throw new IOException();
      }
    }

=================================

$scope.downloadPdf = function(){
      var win = window.open( '', '_blank');
      try {
            $http.post(getContext() + '/documents/createPdf').success(function (data, status, headers, config){
                  win.location.href = getContext() + '/documents/getPDF?filePath=' +encodeURIComponent(data.data);
                  win.focus();
                   });
      } catch(e) {
            win.close();
             return false ;
      }
};

================================


0 comments:

Post a Comment