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();
}