feat: add download profile pdf
This commit is contained in:
parent
1730e29867
commit
d52a1d7914
BIN
lib/itextpdf-5.5.13.4.jar
Normal file
BIN
lib/itextpdf-5.5.13.4.jar
Normal file
Binary file not shown.
@ -45,10 +45,10 @@
|
|||||||
<td><h3>${wordBundle.getWord("photo")}</h3></td>
|
<td><h3>${wordBundle.getWord("photo")}</h3></td>
|
||||||
<td>
|
<td>
|
||||||
<c:if test="${profile.photo != null}">
|
<c:if test="${profile.photo != null}">
|
||||||
<img src="content/profiles/${requestScope.profile.id}/${requestScope.profile.photo}" alt="" height="300">
|
<img src="content${profile.photo}" height="500">
|
||||||
|
<br>
|
||||||
</c:if>
|
</c:if>
|
||||||
<br>
|
<input type="button" value="${wordBundle.getWord('upload')}" onclick="document.getElementById('file').click();" />
|
||||||
<input type="button" value="${wordBundle.getWord('update')}" onclick="document.getElementById('file').click();" />
|
|
||||||
<input type="file" name="photo" id="file" style="display:none;">
|
<input type="file" name="photo" id="file" style="display:none;">
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
@ -56,10 +56,13 @@
|
|||||||
<table>
|
<table>
|
||||||
<tr class="hiddenRow">
|
<tr class="hiddenRow">
|
||||||
<td>
|
<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>
|
||||||
<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>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|||||||
@ -1,5 +1,9 @@
|
|||||||
package ru.charm.back.controller;
|
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.ServletException;
|
||||||
import jakarta.servlet.annotation.MultipartConfig;
|
import jakarta.servlet.annotation.MultipartConfig;
|
||||||
import jakarta.servlet.annotation.WebServlet;
|
import jakarta.servlet.annotation.WebServlet;
|
||||||
@ -12,11 +16,12 @@ import ru.charm.back.mapper.RequestToProfileUpdateDtoMapper;
|
|||||||
import ru.charm.back.service.ProfileService;
|
import ru.charm.back.service.ProfileService;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static jakarta.servlet.http.HttpServletResponse.SC_NOT_FOUND;
|
import static jakarta.servlet.http.HttpServletResponse.SC_NOT_FOUND;
|
||||||
|
|
||||||
@WebServlet("/profile")
|
@WebServlet("/profile/*")
|
||||||
@MultipartConfig
|
@MultipartConfig
|
||||||
public class ProfileController extends HttpServlet {
|
public class ProfileController extends HttpServlet {
|
||||||
private final ProfileService service = ProfileService.getInstance();
|
private final ProfileService service = ProfileService.getInstance();
|
||||||
@ -26,22 +31,43 @@ public class ProfileController extends HttpServlet {
|
|||||||
@Override
|
@Override
|
||||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
String sId = req.getParameter("id");
|
String sId = req.getParameter("id");
|
||||||
String forwardUri = null;
|
|
||||||
if (sId != null) {
|
if (sId != null) {
|
||||||
Optional<ProfileGetDto> optProfileDto = service.findById(Long.parseLong(sId));
|
Optional<ProfileGetDto> optProfileDto = service.findById(Long.parseLong(sId));
|
||||||
System.out.println("Optional profile" + optProfileDto.toString());
|
|
||||||
if (optProfileDto.isPresent()) {
|
if (optProfileDto.isPresent()) {
|
||||||
req.setAttribute("profile", optProfileDto.get());
|
ProfileGetDto profileGetDto = optProfileDto.get();
|
||||||
forwardUri = "/WEB-INF/jsp/profile.jsp";
|
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 {
|
} else {
|
||||||
req.setAttribute("profiles", service.findAll());
|
req.setAttribute("profiles", service.findAll());
|
||||||
forwardUri = "/WEB-INF/jsp/profiles.jsp";
|
req.getRequestDispatcher("/WEB-INF/jsp/profiles.jsp").forward(req, resp);
|
||||||
}
|
|
||||||
if (forwardUri == null) {
|
|
||||||
resp.sendError(SC_NOT_FOUND);
|
|
||||||
} else {
|
|
||||||
req.getRequestDispatcher(forwardUri).forward(req, resp);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user