0

iText (latest version) appears to work slightly better as non-Acrobat pdf viewers can see it. Skim & Preview using PDFBox only show blank page w/o prompting for pass. That said I have an issue with iText after encrypting an existing file - Android, when Content-Disposition = inline shows garble. I'm still testing CD = attachment.

What I want in either iText or PDFBox is to grab a file from the filesystem/db(future), encrypt it, then send it back through the web app server (jBoss 7.1.1.Final). The browser should then determine whether to inline viewer if it has a pdf viewer or download it for another app. This works fine in iOS, btw. Below is my code for both iText & PDFBox tests.

    @RequestMapping (value = "/pdf/{page}.pdf", method = RequestMethod.GET)
    public void doDownload(@PathVariable("page") String paramString, 
    HttpServletRequest request, HttpServletResponse response) throws IOException {
      // get absolute path of the application
      ServletContext context = request.getServletContext();
      String appPath = context.getRealPath("");
      System.out.println("appPath = " + appPath);

       // construct the complete absolute path of the file
            String fullPath = appPath + "/pdf/" + paramString + ".pdf";       
            File downloadFile = new File(fullPath);
            PDDocument mypdf = PDDocument.load(downloadFile);
    //        try {
    //          mypdf.encrypt("demo", "demo");
    //      } catch (CryptographyException e) {
    //          // TODO Auto-generated catch block
    //          e.printStackTrace();
    //      }

    //        FileInputStream inputStream = new FileInputStream(downloadFile);

       // get MIME type of the file
            String mimeType = context.getMimeType(fullPath);
            if (mimeType == null) {
                // set to binary type if MIME mapping not found
                mimeType = "application/pdf";
            }
            System.out.println("MIME type: " + mimeType);

    // set content attributes for the response
    //        response.setContentType(mimeType);
    //        response.setContentLength((int) downloadFile.length());
    // 
            // set headers for the response
            response.addHeader("Content-Disposition", "inline; filename=notice.pdf");
    //        response.addHeader("Content-Transfer-Encoding", "binary");

     // get output stream of the response
            OutputStream outStream = response.getOutputStream();
            try {
                mypdf.save(outStream);
                mypdf.close();

    } catch (COSVisitorException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    //        byte[] buffer = new byte[BUFFER_SIZE];
    //        int bytesRead = -1;
    // 
    //        // write bytes read from the input stream into the output stream
    //        while ((bytesRead = inputStream.read(buffer)) != -1) {
    //            outStream.write(buffer, 0, bytesRead);
    //        }


    //        inputStream.close();
    //        outStream.flush();
    //        outStream.close();



    //        return "pages-authenticated/pdf";

    }
//}


// PDFBox
@RequestMapping (value = "pages-authenticated/pdf.html", method = RequestMethod.POST)
public String doDownload(Model model, @ModelAttribute("PDFForm") PDFForm form,
        HttpServletRequest request, HttpServletResponse response) throws IOException {
    // get absolute path of the application
    ServletContext context = request.getServletContext();
    String appPath = context.getRealPath("");
    String fullPath;
    System.out.println("appPath = " + appPath);


    System.out.println("pass:" + form.getPass());

    // Return if no password given
    if (form.getPass() == null || form.getPass().equalsIgnoreCase("")) {
        model.addAttribute("noPass", true);     
        return "pages-authenticated/pdf";

    }
    // construct the complete absolute path of the file
    fullPath = appPath + "/pdf/notice.pdf";       
    File downloadFile = new File(fullPath);
    PDDocument mypdf = PDDocument.load(downloadFile);

    try {
        mypdf.encrypt(form.getPass(),form.getPass());
    } catch (CryptographyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

//    FileInputStream inputStream = new FileInputStream(downloadFile);

    // get MIME type of the file
    String mimeType = context.getMimeType(fullPath);
    if (mimeType == null) {
        // set to binary type if MIME mapping not found
        mimeType = "application/pdf";
    }
    System.out.println("MIME type: " + mimeType);

    // set headers for the response
    response.setContentType("application/pdf");
//    response.addHeader("Content-Disposition", "inline; filename=notice.pdf");

    // get output stream of the response
    OutputStream outStream = response.getOutputStream();

    try {
        mypdf.save(outStream);
        outStream.flush();
        outStream.close();
        mypdf.close();

    } catch (COSVisitorException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

   return null;
  }




   // iText
    //@RequestMapping (value = "pages-authenticated/pdf.html", method = RequestMethod.POST)
    //public String doDownload(Model model, @ModelAttribute("PDFForm") PDFForm form,
    //      HttpServletRequest request, HttpServletResponse response) throws IOException {
    //    // get absolute path of the application
    //    ServletContext context = request.getServletContext();
    //    String appPath = context.getRealPath("");
    //    String fullPath;
    //    System.out.println("appPath = " + appPath);
    //
    // 
    //  System.out.println("pass:" + form.getPass());
    //  
    ////  // set headers for the response
    //  response.setContentType("application/pdf");
    //  response.addHeader("Content-Disposition", "inline; filename=notice.pdf");
    //
    //
    //  // Return if no password given
    //    if (form.getPass() == null || form.getPass().equalsIgnoreCase("")) {
    //      model.addAttribute("noPass", true);
    ////        
    ////        fullPath = appPath + "/WEB-INF/pages-authenticated/pdf.html";
    ////        File downloadFile = new File(fullPath);
    ////
    ////        FileInputStream inputStream = new FileInputStream(downloadFile);
    ////        
    //////         set content attributes for the response
    ////      response.setContentType("text/html");
    ////      response.setContentLength((int) downloadFile.length());
    ////
    ////      byte[] buffer = new byte[BUFFER_SIZE];
    ////      int bytesRead = -1;
    ////
    ////      // write bytes read from the input stream into the output stream
    ////      while ((bytesRead = inputStream.read(buffer)) != -1) {
    ////          outStream.write(buffer, 0, bytesRead);
    ////      }
    ////
    ////      
    ////      inputStream.close();
    ////      outStream.flush();
    ////      outStream.close();
    //      
    //      return "pages-authenticated/pdf";
    //      
    //    }
    //    // construct the complete absolute path of the file
    //    fullPath = appPath + "/pdf/notice.pdf";      
    //    
    //    PdfReader mypdf = new PdfReader(fullPath);
    //    PdfStamper myPdfStamper;
    //  try {
    //      myPdfStamper = new PdfStamper(mypdf, response.getOutputStream());
    //      myPdfStamper.setEncryption(form.getPass().getBytes(), form.getPass().getBytes(),
    //                PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_256);
    //      myPdfStamper.close();
    //      mypdf.close();
    //  } catch (DocumentException e1) {
    //      // TODO Auto-generated catch block
    //      e1.printStackTrace();
    //  }
    //  
    //    
    ////    FileInputStream inputStream = new FileInputStream(downloadFile);
    //
    //    // get MIME type of the file
    ////    String mimeType = context.getMimeType(fullPath);
    ////    if (mimeType == null) {
    ////        // set to binary type if MIME mapping not found
    ////        mimeType = "application/pdf";
    ////    }
    ////    System.out.println("MIME type: " + mimeType);
    ////
    ////    // set headers for the response
    ////    response.setContentType("application/pdf");
    ////    response.addHeader("Content-Disposition", "inline; filename=notice.pdf");
    ////
    ////    // get output stream of the response
    ////    OutputStream outStream = response.getOutputStream();
    //// 
    ////    try {
    ////        mypdf.save(outStream);
    ////        outStream.flush();
    ////        mypdf.close();
    ////
    ////    } catch (COSVisitorException e) {
    ////        // TODO Auto-generated catch block
    ////        e.printStackTrace();
    ////    }
    ////    
    //   return null;
    //  }
    }
BillS
  • 1
  • 2

1 Answers1

0

Here is what I finally got to work (Android issues):
1. Cannot use AES 256, but AES 128 works
2. Cannot use POST, must use GET

Community
  • 1
  • 1
BillS
  • 1
  • 2