Maven Dependency :
Add following dependency in your pom.xml
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.3.4</version>
</dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.3.4</version>
</dependency>
Controller code :
@RequestMapping(value = "/generatePDF/{empId}",method = RequestMethod.GET)
private void downloadPDF(@PathVariable Integer empId,
private void downloadPDF(@PathVariable Integer empId,
HttpServletResponse response,
HttpServletRequest request, ModelMap model)
HttpServletRequest request, ModelMap model)
throws IOException {
List<TaskEntry> taskEntryList = taskEntryService.retrieveAllTask(empId);\
//here TaskEntry is your Domain of respective class
//here taskEntryService is your service layer from which you are accessing Data.
String orignalFileName="sample.pdf";
try {
Document document = new Document();
response.setHeader("Content-Disposition", "outline;filename=\"" +orignalFileName+ "\"");
PdfWriter.getInstance(document, response.getOutputStream());
document.open();
document.add(createFirstTable(taskEntryList ));
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//here taskEntryService is your service layer from which you are accessing Data.
String orignalFileName="sample.pdf";
try {
Document document = new Document();
response.setHeader("Content-Disposition", "outline;filename=\"" +orignalFileName+ "\"");
PdfWriter.getInstance(document, response.getOutputStream());
document.open();
document.add(createFirstTable(taskEntryList ));
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public PdfPTable createFirstTable(List<TaskEntry> taskEntryList ) throws ParseException {
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String fromDate=sdf.format(pdfForm.getFromDate());
String toDate=sdf.format(pdfForm.getToDate());
// a table with three columns
PdfPTable table = new PdfPTable(2);
// the cell object
PdfPCell cell;
// we add a cell with colspan 3
cell = new PdfPCell(new Phrase("TASK DETAILS"));
cell.setColspan(2);
table.addCell(cell);
cell = new PdfPCell(new Phrase("From"+fromDate+"TO"+toDate));
cell.setColspan(2);
table.addCell(cell);
table.addCell("DATE:");
table.addCell("TASK:");
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String fromDate=sdf.format(pdfForm.getFromDate());
String toDate=sdf.format(pdfForm.getToDate());
// a table with three columns
PdfPTable table = new PdfPTable(2);
// the cell object
PdfPCell cell;
// we add a cell with colspan 3
cell = new PdfPCell(new Phrase("TASK DETAILS"));
cell.setColspan(2);
table.addCell(cell);
cell = new PdfPCell(new Phrase("From"+fromDate+"TO"+toDate));
cell.setColspan(2);
table.addCell(cell);
table.addCell("DATE:");
table.addCell("TASK:");
for(TaskEntry taskEntry: taskEntryList)
{
table.addCell(taskEntry.task);}
return table;
}
Return generated pdf using spring MVC
I am using Spring MVC .I have to write a service that would take input from the request body, add the data to the pdf and returns the pdf file to the browser. The pdf document is generated using itextpdf. How can I do this using Spring MVC. I have tried using this
showhelp function that generates the pdf. I am just putting some random data in the pdf for time being.
I am sure this is wrong. I want that pdf to be generated and save/open dialog box to be opened through the browser, so that it can be stored in the client's file system. Please help me out.
|
You were on the good track with that
response.getOutputStream(), but you're not using it in your code. Essentially what you need to do is to stream the PDF file's bytes directly to the output stream and flush the response. In Spring you can do this like this:
Notes:
|
Thanks for sharing this informative content , Great work
ReplyDeleteLeanpitch provides online training in Product prototyping during this lockdown period everyone can use it wisely.
icp-cat training