feat: add download profile pdf

This commit is contained in:
Sergey Krylov 2026-02-24 05:17:38 +03:00
parent 1730e29867
commit d52a1d7914
3 changed files with 45 additions and 16 deletions

BIN
lib/itextpdf-5.5.13.4.jar Normal file

Binary file not shown.

View File

@ -45,10 +45,10 @@
<td><h3>${wordBundle.getWord("photo")}</h3></td>
<td>
<c:if test="${profile.photo != null}">
<img src="content/profiles/${requestScope.profile.id}/${requestScope.profile.photo}" alt="" height="300">
</c:if>
<img src="content${profile.photo}" height="500">
<br>
<input type="button" value="${wordBundle.getWord('update')}" onclick="document.getElementById('file').click();" />
</c:if>
<input type="button" value="${wordBundle.getWord('upload')}" onclick="document.getElementById('file').click();" />
<input type="file" name="photo" id="file" style="display:none;">
</td>
</tr>
@ -56,10 +56,13 @@
<table>
<tr class="hiddenRow">
<td>
<input type="image" src="content/app/img/floppy-disk.png" width="75" alt="submit" class="icon" />
<input type="image" src="content/app/img/floppy-disk.png" width="75" alt="submit" class="icon"/>
</td>
<td>
<a href="/email?id=${profile.id}"><img src="content/app/img/key.png" alt="" width="75"></a>
<a href="/credentials?id=${profile.id}"><img src="content/app/img/at-sign.png" width="75"></a>
</td>
<td>
<a href="/profile/pdf?id=${profile.id}"><img src="content/app/img/id.png" alt="" width="75"></a>
</td>
</tr>
</table>

View File

@ -1,5 +1,9 @@
package ru.charm.back.controller;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
@ -12,11 +16,12 @@ import ru.charm.back.mapper.RequestToProfileUpdateDtoMapper;
import ru.charm.back.service.ProfileService;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Optional;
import static jakarta.servlet.http.HttpServletResponse.SC_NOT_FOUND;
@WebServlet("/profile")
@WebServlet("/profile/*")
@MultipartConfig
public class ProfileController extends HttpServlet {
private final ProfileService service = ProfileService.getInstance();
@ -26,22 +31,43 @@ public class ProfileController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String sId = req.getParameter("id");
String forwardUri = null;
if (sId != null) {
Optional<ProfileGetDto> optProfileDto = service.findById(Long.parseLong(sId));
System.out.println("Optional profile" + optProfileDto.toString());
if (optProfileDto.isPresent()) {
req.setAttribute("profile", optProfileDto.get());
forwardUri = "/WEB-INF/jsp/profile.jsp";
ProfileGetDto profileGetDto = optProfileDto.get();
if (req.getRequestURI().equals("/profile/pdf")) {
resp.setHeader("Content-Disposition", "attachment; filename=\"profile.pdf\"");
resp.setContentType("application/pdf");
resp.setCharacterEncoding("UTF-8");
try(OutputStream os = resp.getOutputStream()) {
Document document = new Document();
PdfWriter.getInstance(document, os);
document.open();
PdfPTable table = new PdfPTable(2);
table.addCell("Email");
table.addCell(profileGetDto.getEmail());
table.addCell("Name");
table.addCell(profileGetDto.getName());
table.addCell("Surname");
table.addCell(profileGetDto.getSurname());
document.add(table);
document.close();
} catch (DocumentException e) {
throw new IOException(e);
}
} else {
req.setAttribute("profile", profileGetDto);
req.getRequestDispatcher("/WEB-INF/jsp/profile.jsp").forward(req, resp);
}
} else {
resp.sendError(SC_NOT_FOUND);
}
} else {
req.setAttribute("profiles", service.findAll());
forwardUri = "/WEB-INF/jsp/profiles.jsp";
}
if (forwardUri == null) {
resp.sendError(SC_NOT_FOUND);
} else {
req.getRequestDispatcher(forwardUri).forward(req, resp);
req.getRequestDispatcher("/WEB-INF/jsp/profiles.jsp").forward(req, resp);
}
}