| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- package pl.sudra.service;
- import com.itextpdf.text.Chunk;
- import com.itextpdf.text.Document;
- import com.itextpdf.text.DocumentException;
- import com.itextpdf.text.Paragraph;
- import com.itextpdf.text.pdf.PdfPTable;
- import com.itextpdf.text.pdf.PdfWriter;
- import jakarta.servlet.http.HttpServletResponse;
- import org.springframework.stereotype.Service;
- import pl.sudra.domain.User;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- @Service
- public class PdfServiceImpl implements PdfService{
- @Override
- public OutputStream generatePDF(User user, HttpServletResponse response) {
- try{
- OutputStream o = response.getOutputStream();
- response.setCharacterEncoding("UTF-8");
- response.setContentType("application/pdf");
- response.setHeader("Content-Disposition", "inline; filename=" + user.getUsername() + ".pdf");
- Document pdf = new Document();
- PdfWriter.getInstance(pdf, o);
- pdf.open();
- pdf.add(new Paragraph("Pdf example - Spring Framework & iText library"));
- pdf.add(new Paragraph(Chunk.NEWLINE));
- PdfPTable table = new PdfPTable(2);
- table.addCell("FirsName");
- table.addCell("appUser.getFirstName()");
- table.addCell("LastName");
- table.addCell("appUser.getLastName()");
- table.addCell("PESEL");
- table.addCell("appUser.getPesel().getPESEL()");
- table.addCell("Login");
- table.addCell("appUser.getLogin()");
- table.addCell("Email");
- table.addCell("appUser.getEmail()");
- table.addCell("Active");
- table.addCell("String.valueOf(appUser.isEnabled())");
- pdf.add(table);
- pdf.close();
- o.close();
- return o;
- }
- catch (IOException| DocumentException e){
- e.printStackTrace();
- }
- return null;
- }
- @Override
- public ByteArrayOutputStream generatePDF(User user) {
- try{
- ByteArrayOutputStream o = new ByteArrayOutputStream();
- Document pdf = new Document();
- PdfWriter.getInstance(pdf, o);
- pdf.open();
- pdf.add(new Paragraph("Pdf example - Spring Framework & iText library"));
- pdf.add(new Paragraph(Chunk.NEWLINE));
- PdfPTable table = new PdfPTable(2);
- table.addCell("FirsName");
- table.addCell("appUser.getFirstName()");
- table.addCell("LastName");
- table.addCell("appUser.getLastName()");
- table.addCell("PESEL");
- table.addCell("appUser.getPesel().getPESEL()");
- table.addCell("Login");
- table.addCell("appUser.getLogin()");
- table.addCell("Email");
- table.addCell("appUser.getEmail()");
- table.addCell("Active");
- table.addCell("String.valueOf(appUser.isEnabled())");
- pdf.add(table);
- pdf.close();
- o.close();
- return o;
- }
- catch (IOException| DocumentException e){
- e.printStackTrace();
- }
- return null;
- }
- }
|