0

I am attempting to edit an existing PDF document using PDFbox and a servlet. I have a JSP page where the user enters data and then clicks on a button to add the data to the PDF and download it. I'm trying to do something similar to what is being done in this post , but I haven't been able to get it to work using those suggestions. I've done some debugging and determined that it runs through both functions, but the browser never downloads the file. Any help would be greatly appreciated.

public static void downloadPDF(HttpServletRequest request, HttpServletResponse resp) throws IOException{
       try{
           //variables declared and assigned values here

           //Function to create PDF called here and variables passed
            byte[] output = createPDF(firstName, middleName, lastName, houseNumber, city, state, zip, mstatus, withholdings);

            String mimeType = "application/pdf";
            resp.setContentType(mimeType);
            resp.setHeader("Content-Disposition", "attachment; filename=" + lastName + ".pdf");
            resp.setContentLength(output.length);

            ServletOutputStream servletOut = resp.getOutputStream();
            servletOut.write(output, 0, output.length);
            servletOut.flush();
            servletOut.close();

        } catch(Exception ex){
            System.out.println("download() error: " + ex.toString());

        }

    }

       public static byte[] createPDF(String firstName, String middleName, String lastName, String houseNumber, String city, String state, String zip, String mstatus, String withholdings) throws IOException {

             PDDocument document = null;
             ByteArrayOutputStream output;

            output = new ByteArrayOutputStream();

               //Loading existing document
              File file = new File("filepath");
              document = PDDocument.load(file);


              //Retrieving the pages of the document 
              PDPage page = document.getPage(0);
              PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true);
              //Begin the Content stream 
              contentStream.beginText(); 
              //Setting the font to the Content stream  
              contentStream.setFont(PDType1Font.HELVETICA, 8);
              //Setting the position for the line 
              contentStream.newLineAtOffset(35, 235);

              //Adding text in the form of string 
              contentStream.showText(firstName + " " + middleName);  
              contentStream.newLineAtOffset(190, 0);
              contentStream.showText(lastName); 
              contentStream.newLineAtOffset(-190, -25);
              contentStream.showText(houseNumber); 
              contentStream.newLineAtOffset(0, -25);
              contentStream.showText(city + " " + state + " " + zip); 

              contentStream.newLineAtOffset(480, -14);
              contentStream.showText(withholdings); 
              System.out.println(mstatus);
              String text = "X";
              if(mstatus.equals("single")) {
                contentStream.newLineAtOffset(-190, 48);
                contentStream.showText(text);
              } else if (mstatus.equals("married")) {
                  contentStream.newLineAtOffset(-148, 48);
                  contentStream.showText(text);
              } else if (mstatus.equals("marriedsingle")) {
                  contentStream.newLineAtOffset(-97, 48);
                  contentStream.showText(text);
              }               
              //Ending the content stream
              contentStream.endText();

              //Closing the content stream
              contentStream.close();

              document.save(output);
              document.close();

              //Closing the document
              document.close();
              return output.toByteArray();

       }
Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
leflowers
  • 1
  • 1
  • 2
    "The browser never downloads the file". So what happens instead? Do you get any exceptions? – Thilo Feb 14 '20 at 00:12
  • Is the PDF file OK (have you tried saving it locally and then open it)? Also, I noticed that the other person used quotes in the filename in the Content-Disposition response header. From my memory on working with servlets, you should not close the output stream (indeed: https://stackoverflow.com/questions/1159168/ ). And in your code, you're closing the PDDocument twice. Although this is harmless, I wonder if this is because your actual code is different than what you posted? – Tilman Hausherr Feb 14 '20 at 03:50
  • To narrow the problem: what happens if you return the original PDF file as a stream? Do you have the same (undisclosed) problem, or does it work? – Tilman Hausherr Feb 14 '20 at 03:54
  • 1
    That will happen when you try to download a file using Ajax instead of a normal request. Is this true? – BalusC Feb 14 '20 at 05:27
  • @BalusC That was my problem. I was using Ajax instead of a normal request. I changed it and it is now working. Thank you for your help! – leflowers Feb 14 '20 at 15:14

0 Answers0