PdfServiceImpl.java 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package pl.sudra.service;
  2. import com.itextpdf.text.Chunk;
  3. import com.itextpdf.text.Document;
  4. import com.itextpdf.text.DocumentException;
  5. import com.itextpdf.text.Paragraph;
  6. import com.itextpdf.text.pdf.PdfPTable;
  7. import com.itextpdf.text.pdf.PdfWriter;
  8. import jakarta.servlet.http.HttpServletResponse;
  9. import org.springframework.stereotype.Service;
  10. import pl.sudra.domain.User;
  11. import java.io.ByteArrayOutputStream;
  12. import java.io.IOException;
  13. import java.io.OutputStream;
  14. @Service
  15. public class PdfServiceImpl implements PdfService{
  16. @Override
  17. public OutputStream generatePDF(User user, HttpServletResponse response) {
  18. try{
  19. OutputStream o = response.getOutputStream();
  20. response.setCharacterEncoding("UTF-8");
  21. response.setContentType("application/pdf");
  22. response.setHeader("Content-Disposition", "inline; filename=" + user.getUsername() + ".pdf");
  23. Document pdf = new Document();
  24. PdfWriter.getInstance(pdf, o);
  25. pdf.open();
  26. pdf.add(new Paragraph("Pdf example - Spring Framework & iText library"));
  27. pdf.add(new Paragraph(Chunk.NEWLINE));
  28. PdfPTable table = new PdfPTable(2);
  29. table.addCell("FirsName");
  30. table.addCell("appUser.getFirstName()");
  31. table.addCell("LastName");
  32. table.addCell("appUser.getLastName()");
  33. table.addCell("PESEL");
  34. table.addCell("appUser.getPesel().getPESEL()");
  35. table.addCell("Login");
  36. table.addCell("appUser.getLogin()");
  37. table.addCell("Email");
  38. table.addCell("appUser.getEmail()");
  39. table.addCell("Active");
  40. table.addCell("String.valueOf(appUser.isEnabled())");
  41. pdf.add(table);
  42. pdf.close();
  43. o.close();
  44. return o;
  45. }
  46. catch (IOException| DocumentException e){
  47. e.printStackTrace();
  48. }
  49. return null;
  50. }
  51. @Override
  52. public ByteArrayOutputStream generatePDF(User user) {
  53. try{
  54. ByteArrayOutputStream o = new ByteArrayOutputStream();
  55. Document pdf = new Document();
  56. PdfWriter.getInstance(pdf, o);
  57. pdf.open();
  58. pdf.add(new Paragraph("Pdf example - Spring Framework & iText library"));
  59. pdf.add(new Paragraph(Chunk.NEWLINE));
  60. PdfPTable table = new PdfPTable(2);
  61. table.addCell("FirsName");
  62. table.addCell("appUser.getFirstName()");
  63. table.addCell("LastName");
  64. table.addCell("appUser.getLastName()");
  65. table.addCell("PESEL");
  66. table.addCell("appUser.getPesel().getPESEL()");
  67. table.addCell("Login");
  68. table.addCell("appUser.getLogin()");
  69. table.addCell("Email");
  70. table.addCell("appUser.getEmail()");
  71. table.addCell("Active");
  72. table.addCell("String.valueOf(appUser.isEnabled())");
  73. pdf.add(table);
  74. pdf.close();
  75. o.close();
  76. return o;
  77. }
  78. catch (IOException| DocumentException e){
  79. e.printStackTrace();
  80. }
  81. return null;
  82. }
  83. }