fix(build): create sub-app make_pdf
Some checks failed
Deploy Production Server / if_merged (pull_request) Failing after 18s
Some checks failed
Deploy Production Server / if_merged (pull_request) Failing after 18s
This commit is contained in:
@@ -1,10 +1,5 @@
|
||||
package fr.titionfire.ffsaf.domain.service;
|
||||
|
||||
import com.lowagie.text.*;
|
||||
import com.lowagie.text.pdf.BaseFont;
|
||||
import com.lowagie.text.pdf.PdfPCell;
|
||||
import com.lowagie.text.pdf.PdfPTable;
|
||||
import com.lowagie.text.pdf.PdfWriter;
|
||||
import fr.titionfire.ffsaf.data.model.ClubModel;
|
||||
import fr.titionfire.ffsaf.data.model.LicenceModel;
|
||||
import fr.titionfire.ffsaf.data.model.MembreModel;
|
||||
@@ -38,18 +33,20 @@ import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||
import org.hibernate.reactive.mutiny.Mutiny;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
@WithSession
|
||||
@ApplicationScoped
|
||||
public class MembreService {
|
||||
private static final Logger LOGGER = Logger.getLogger(MembreService.class);
|
||||
|
||||
@Inject
|
||||
CombRepository repository;
|
||||
@@ -66,6 +63,9 @@ public class MembreService {
|
||||
@ConfigProperty(name = "upload_dir")
|
||||
String media;
|
||||
|
||||
@ConfigProperty(name = "pdf-maker.jar-path")
|
||||
String pdfMakerJarPath;
|
||||
|
||||
public SimpleCombModel find(int licence, String np) throws Throwable {
|
||||
return VertxContextSupport.subscribeAndAwait(() -> Panache.withTransaction(() ->
|
||||
repository.find("licence = ?1 AND (lname ILIKE ?2 OR fname ILIKE ?2)",
|
||||
@@ -315,11 +315,11 @@ public class MembreService {
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new DNotFoundException("Pas de licence pour la saison en cours"));
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
try {
|
||||
make_pdf(m, out, licence);
|
||||
byte[] buff = make_pdf(m, licence);
|
||||
if (buff == null)
|
||||
throw new IOException("Error making pdf");
|
||||
|
||||
byte[] buff = out.toByteArray();
|
||||
String mimeType = "application/pdf";
|
||||
|
||||
Response.ResponseBuilder resp = Response.ok(buff);
|
||||
@@ -336,185 +336,79 @@ public class MembreService {
|
||||
}));
|
||||
}
|
||||
|
||||
private void make_pdf(MembreModel m, ByteArrayOutputStream out, LicenceModel licence) throws IOException {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
|
||||
Document document = new Document();
|
||||
PdfWriter.getInstance(document, out);
|
||||
document.open();
|
||||
private byte[] make_pdf(MembreModel m, LicenceModel licence) throws IOException, InterruptedException {
|
||||
List<String> cmd = new ArrayList<>();
|
||||
cmd.add("java");
|
||||
cmd.add("-jar");
|
||||
cmd.add(pdfMakerJarPath);
|
||||
|
||||
document.addCreator("FFSAF");
|
||||
document.addTitle(
|
||||
"Attestation d'adhésion " + Utils.getSaison() + "-" + (Utils.getSaison() + 1) + " de " + m.getLname() + " " + m.getFname());
|
||||
document.addCreationDate();
|
||||
document.addProducer("https://www.ffsaf.fr");
|
||||
UUID uuid = UUID.randomUUID();
|
||||
|
||||
InputStream fontStream = MembreService.class.getClassLoader().getResourceAsStream("asset/DMSans-Regular.ttf");
|
||||
if (fontStream == null) {
|
||||
throw new IOException("Font file not found");
|
||||
}
|
||||
BaseFont customFont = BaseFont.createFont("asset/DMSans-Regular.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, true,
|
||||
null, fontStream.readAllBytes());
|
||||
cmd.add("/tmp/" + uuid + ".pdf");
|
||||
cmd.add(m.getFname());
|
||||
cmd.add(m.getLname());
|
||||
cmd.add(m.getGenre().str);
|
||||
cmd.add(m.getCategorie().getName());
|
||||
cmd.add(licence.getCertificate() == null ? "" : licence.getCertificate());
|
||||
cmd.add(Utils.getSaison() + "");
|
||||
cmd.add(m.getLicence() + "");
|
||||
cmd.add(m.getClub().getName());
|
||||
cmd.add(m.getClub().getNo_affiliation() + "");
|
||||
cmd.add(m.getBirth_date() == null ? "--" : new SimpleDateFormat("dd/MM/yyyy").format(m.getBirth_date()));
|
||||
|
||||
// Adding font
|
||||
Font headerFont = new Font(customFont, 26, Font.BOLD);
|
||||
Font subHeaderFont = new Font(customFont, 16, Font.BOLD);
|
||||
Font bigFont = new Font(customFont, 18, Font.BOLD);
|
||||
Font bodyFont = new Font(customFont, 15, Font.BOLD);
|
||||
Font smallFont = new Font(customFont, 10, Font.NORMAL);
|
||||
|
||||
// Creating the main table
|
||||
PdfPTable mainTable = new PdfPTable(2);
|
||||
mainTable.setWidthPercentage(100);
|
||||
mainTable.setSpacingBefore(20f);
|
||||
mainTable.setSpacingAfter(0f);
|
||||
mainTable.setWidths(new float[]{120, 300});
|
||||
mainTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
|
||||
|
||||
// Adding logo
|
||||
Image logo = Image.getInstance(
|
||||
Objects.requireNonNull(
|
||||
getClass().getClassLoader().getResource("asset/FFSSAF-bord-blanc-fond-transparent.png")));
|
||||
logo.scaleToFit(120, 120);
|
||||
PdfPCell logoCell = new PdfPCell(logo);
|
||||
logoCell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
||||
logoCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
|
||||
logoCell.setPadding(0);
|
||||
logoCell.setRowspan(1);
|
||||
logoCell.setBorder(PdfPCell.NO_BORDER);
|
||||
mainTable.addCell(logoCell);
|
||||
|
||||
// Adding header
|
||||
PdfPCell headerCell = new PdfPCell(new Phrase("FEDERATION FRANCE\nSOFT ARMORED FIGHTING", headerFont));
|
||||
headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
||||
headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
|
||||
headerCell.setBorder(PdfPCell.NO_BORDER);
|
||||
mainTable.addCell(headerCell);
|
||||
|
||||
document.add(mainTable);
|
||||
|
||||
Paragraph addr = new Paragraph("5 place de la Barreyre\n63320 Champeix", subHeaderFont);
|
||||
addr.setAlignment(Element.ALIGN_CENTER);
|
||||
addr.setSpacingAfter(2f);
|
||||
document.add(addr);
|
||||
|
||||
Paragraph association = new Paragraph("Association loi 1901 W633001595\nSIRET 829 458 355 00015", smallFont);
|
||||
association.setAlignment(Element.ALIGN_CENTER);
|
||||
document.add(association);
|
||||
|
||||
// Adding spacing
|
||||
document.add(new Paragraph("\n\n"));
|
||||
|
||||
// Adding attestation
|
||||
PdfPTable attestationTable = new PdfPTable(1);
|
||||
attestationTable.setWidthPercentage(60);
|
||||
PdfPCell attestationCell = new PdfPCell(
|
||||
new Phrase("ATTESTATION D'ADHESION\nSaison " + Utils.getSaison() + "-" + (Utils.getSaison() + 1),
|
||||
bigFont));
|
||||
attestationCell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
||||
attestationCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
|
||||
attestationCell.setPadding(20f);
|
||||
attestationTable.addCell(attestationCell);
|
||||
document.add(attestationTable);
|
||||
|
||||
// Adding spacing
|
||||
document.add(new Paragraph("\n\n"));
|
||||
|
||||
// Adding member details table
|
||||
PdfPTable memberTable = new PdfPTable(2);
|
||||
memberTable.setWidthPercentage(100);
|
||||
memberTable.setWidths(new float[]{130, 300});
|
||||
memberTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
|
||||
|
||||
// Adding member photo
|
||||
Image memberPhoto;
|
||||
FilenameFilter filter = (directory, filename) -> filename.startsWith(m.getId() + ".");
|
||||
File[] files = new File(media, "ppMembre").listFiles(filter);
|
||||
if (files != null && files.length > 0) {
|
||||
File file = files[0];
|
||||
memberPhoto = Image.getInstance(Files.readAllBytes(file.toPath()));
|
||||
cmd.add(file.getAbsolutePath());
|
||||
} else {
|
||||
memberPhoto = Image.getInstance(
|
||||
Objects.requireNonNull(getClass().getClassLoader().getResource("asset/blank-profile-picture.png")));
|
||||
cmd.add("/dev/null");
|
||||
}
|
||||
memberPhoto.scaleToFit(120, 150);
|
||||
PdfPCell photoCell = new PdfPCell(memberPhoto);
|
||||
photoCell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
||||
photoCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
|
||||
photoCell.setRowspan(5);
|
||||
photoCell.setBorder(PdfPCell.NO_BORDER);
|
||||
memberTable.addCell(photoCell);
|
||||
|
||||
String[] cert;
|
||||
if (licence.getCertificate() != null && !licence.getCertificate().isBlank()) {
|
||||
cert = licence.getCertificate().split("¤");
|
||||
if (cert.length <= 1){
|
||||
cert = new String[]{licence.getCertificate(), "--"};
|
||||
}else{
|
||||
try {
|
||||
cert[1] = sdf.format(new SimpleDateFormat("yyyy-MM-dd").parse(cert[1]));
|
||||
} catch (ParseException e) {
|
||||
cert[1] = "--";
|
||||
}
|
||||
ProcessBuilder processBuilder = new ProcessBuilder(cmd);
|
||||
processBuilder.redirectErrorStream(true);
|
||||
Process process = processBuilder.start();
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
Thread t = new Thread(() -> {
|
||||
try {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null)
|
||||
builder.append(line).append("\n");
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
|
||||
int code = -1;
|
||||
if (!process.waitFor(30, TimeUnit.SECONDS)) {
|
||||
process.destroy();
|
||||
builder.append("Timeout...");
|
||||
} else {
|
||||
cert = new String[]{"--", "--"};
|
||||
code = process.exitValue();
|
||||
}
|
||||
|
||||
// Adding member details
|
||||
memberTable.addCell(new Phrase("NOM : " + m.getLname().toUpperCase(), bodyFont));
|
||||
memberTable.addCell(new Phrase("Prénom : " + m.getFname(), bodyFont));
|
||||
memberTable.addCell(new Phrase("Licence n° : " + m.getLicence(), bodyFont));
|
||||
memberTable.addCell(new Phrase("Certificat médical par " + cert[0] + ", le " + cert[1], bodyFont));
|
||||
memberTable.addCell(new Phrase("")); // Empty cell for spacing
|
||||
if (t.isAlive())
|
||||
t.interrupt();
|
||||
|
||||
document.add(memberTable);
|
||||
LOGGER.debug("PDF maker: " + builder);
|
||||
|
||||
// Adding spacing
|
||||
document.add(new Paragraph("\n"));
|
||||
|
||||
Paragraph memberClub = new Paragraph("CLUB : " + m.getClub().getName().toUpperCase(), bodyFont);
|
||||
document.add(memberClub);
|
||||
|
||||
Paragraph memberClubNumber = new Paragraph("N° club : " + m.getClub().getNo_affiliation(), bodyFont);
|
||||
document.add(memberClubNumber);
|
||||
|
||||
// Adding spacing
|
||||
document.add(new Paragraph("\n"));
|
||||
|
||||
Paragraph memberBirthdate = new Paragraph(
|
||||
"Date de naissance : " + ((m.getBirth_date() == null) ? "--" : sdf.format(m.getBirth_date())),
|
||||
bodyFont);
|
||||
document.add(memberBirthdate);
|
||||
|
||||
Paragraph memberGender = new Paragraph("Sexe : " + m.getGenre().str, bodyFont);
|
||||
document.add(memberGender);
|
||||
|
||||
Paragraph memberAgeCategory = new Paragraph("Catégorie d'âge : " + m.getCategorie().getName(), bodyFont);
|
||||
document.add(memberAgeCategory);
|
||||
|
||||
// Adding spacing
|
||||
document.add(new Paragraph("\n\n"));
|
||||
|
||||
// Adding attestation text
|
||||
PdfPTable textTable = new PdfPTable(1);
|
||||
textTable.setWidthPercentage(100);
|
||||
PdfPCell textCell = new PdfPCell(new Phrase(
|
||||
"""
|
||||
Ce document atteste que l’adhérent
|
||||
- est valablement enregistré auprès de la FFSAF,
|
||||
- est assuré dans sa pratique du Béhourd Léger et du Battle Arc en entraînement et en compétition.
|
||||
|
||||
Il peut donc s’inscrire à tout tournoi organisé sous l’égide de la FFSAF s’il remplit les éventuelles
|
||||
conditions de qualification.
|
||||
Il peut participer à tout entraînement dans un club affilié si celui ci autorise les visiteurs.""",
|
||||
smallFont));
|
||||
textCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
||||
textCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
|
||||
textCell.setBorder(PdfPCell.NO_BORDER);
|
||||
textTable.addCell(textCell);
|
||||
document.add(textTable);
|
||||
|
||||
// Close the document
|
||||
document.close();
|
||||
if (code != 0) {
|
||||
throw new IOException("Error code: " + code);
|
||||
} else {
|
||||
File file = new File("/tmp/" + uuid + ".pdf");
|
||||
try (FileInputStream fis = new FileInputStream(file)) {
|
||||
byte[] buff = fis.readAllBytes();
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
file.delete();
|
||||
return buff;
|
||||
} catch (IOException e) {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
file.delete();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user