feat: add affiliation pdf generator

This commit is contained in:
2025-07-07 13:16:42 +02:00
parent 280a8fa3f3
commit 34be19fbed
16 changed files with 429 additions and 78 deletions

View File

@@ -1,11 +1,11 @@
package fr.titionfire;
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 com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.*;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -13,37 +13,231 @@ import java.io.InputStream;
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;
public class Main {
public static void main(String[] args) {
PdfData pdfData = new PdfData();
File dest = new File(args[0]);
pdfData.fname = args[1];
pdfData.lname = args[2];
pdfData.genre = args[3];
pdfData.categorie = args[4];
pdfData.certificate = args[5];
pdfData.saison = Integer.parseInt(args[6]);
pdfData.licence = Integer.parseInt(args[7]);
pdfData.club = args[8];
pdfData.club_no = Integer.parseInt(args[9]);
pdfData.birth_date = args[10];
pdfData.photo_file = new File(args[11]);
try {
FileOutputStream out = new FileOutputStream(dest);
new Main().make_pdf(pdfData, out);
} catch (IOException ignored) {
ignored.printStackTrace();
System.exit(1);
if (args[1].equals("membre")) {
PdfData pdfData = new PdfData();
pdfData.fname = args[2];
pdfData.lname = args[3];
pdfData.genre = args[4];
pdfData.categorie = args[5];
pdfData.certificate = args[6];
pdfData.saison = Integer.parseInt(args[7]);
pdfData.licence = Integer.parseInt(args[8]);
pdfData.club = args[9];
pdfData.club_no = Integer.parseInt(args[10]);
pdfData.birth_date = args[11];
pdfData.photo_file = new File(args[12]);
try {
FileOutputStream out = new FileOutputStream(dest);
new Main().make_pdf(pdfData, out);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
} else {
PdfDataClub pdfData = new PdfDataClub();
pdfData.name = args[2];
pdfData.saison = Integer.parseInt(args[3]);
pdfData.club_no = Integer.parseInt(args[4]);
pdfData.sign_file = new File(args[5]);
try {
FileOutputStream out = new FileOutputStream(dest);
new Main().make_pdf_club(pdfData, out);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
System.out.println("PDF generated successfully");
System.exit(0);
}
public static class BorderEvent implements PdfPTableEvent {
public void tableLayout(PdfPTable table, float[][] widths,
float[] heights, int headerRows, int rowStart,
PdfContentByte[] canvases) {
float[] width = widths[0];
float x1 = width[0];
float x2 = width[width.length - 1];
float y1 = heights[0];
float y2 = heights[heights.length - 1];
PdfContentByte cb = canvases[PdfPTable.LINECANVAS];
cb.setLineWidth(3);
cb.rectangle(x1, y1, x2 - x1, y2 - y1);
cb.stroke();
cb.setLineWidth(1);
cb.rectangle(x1 + 4, y1 - 4, x2 - x1 - 8, y2 - y1 + 8);
cb.stroke();
cb.resetRGBColorStroke();
}
}
private void make_pdf_club(PdfDataClub pdfData, FileOutputStream out) throws IOException {
Document document = new Document(PageSize.A4.rotate());
PdfWriter.getInstance(document, out);
document.open();
document.addCreator("FFSAF");
document.addTitle(
"Attestation d'affiliation " + pdfData.saison + "-" + (pdfData.saison + 1) + " - " + pdfData.name);
document.addCreationDate();
document.addProducer("https://www.ffsaf.fr");
InputStream fontStream = Main.class.getClassLoader().getResourceAsStream("Helvetica.ttf");
if (fontStream == null)
throw new IOException("Font file not found");
BaseFont customFont = BaseFont.createFont("Helvetica.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, true,
null, fontStream.readAllBytes());
InputStream fontStream2 = Main.class.getClassLoader().getResourceAsStream("Helvetica-Bold.ttf");
if (fontStream2 == null)
throw new IOException("Font file not found");
BaseFont customFontBold = BaseFont.createFont("Helvetica-Bold.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, true,
null, fontStream2.readAllBytes());
InputStream fontStream3 = Main.class.getClassLoader().getResourceAsStream("helvetica-light.ttf");
if (fontStream3 == null)
throw new IOException("Font file not found");
BaseFont customFontLight = BaseFont.createFont("helvetica-light.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, true,
null, fontStream3.readAllBytes());
// Creating the main table
PdfPTable mainTable = new PdfPTable(1);
mainTable.setWidthPercentage(100);
mainTable.setSpacingBefore(0f);
mainTable.setSpacingAfter(0f);
mainTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
mainTable.setExtendLastRow(true);
mainTable.setTableEvent(new BorderEvent());
// Creating the main table
PdfPTable headerRow = new PdfPTable(2);
headerRow.setWidths(new float[]{1, 2});
headerRow.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
// Adding logo
Image logo = Image.getInstance(
Objects.requireNonNull(
getClass().getClassLoader().getResource("Logo-FFSAF-2023.png")));
logo.scaleToFit(160, 160);
PdfPCell logoCell = new PdfPCell(logo);
logoCell.setHorizontalAlignment(Element.ALIGN_CENTER);
logoCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
logoCell.setPadding(0);
logoCell.setRowspan(1);
logoCell.setPaddingTop(20);
logoCell.setPaddingBottom(20);
logoCell.setBorder(PdfPCell.NO_BORDER);
headerRow.addCell(logoCell);
List<Paragraph> pList = new ArrayList<>();
pList.add(new Paragraph("Fédération", new Font(customFontBold, 16, Font.NORMAL)));
pList.add(new Paragraph("FRANCE SOFT ARMORED FIGHTING", new Font(customFontBold, 18, Font.NORMAL)));
pList.add(new Paragraph("Association loi 1901 n° W633001595\n ", new Font(customFontLight, 11, Font.NORMAL)));
pList.add(new Paragraph("5 place de la Barreyre", new Font(customFont, 11, Font.NORMAL)));
pList.add(new Paragraph("63320 Champeix\n ", new Font(customFont, 11, Font.NORMAL)));
pList.add(new Paragraph("ffsaf.fr", new Font(customFontBold, 14, Font.NORMAL)));
PdfPCell headerCell = new PdfPCell();
pList.forEach(p -> p.setAlignment(Element.ALIGN_CENTER));
pList.forEach(headerCell::addElement);
headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
headerCell.setBorder(PdfPCell.NO_BORDER);
headerRow.addCell(headerCell);
mainTable.addCell(headerRow);
pList.clear();
pList.add(new Paragraph("CLUB AFFILIÉ SAISON " + pdfData.saison + "-" + (pdfData.saison + 1) + "\n ",
new Font(customFontBold, 24, Font.NORMAL)));
pList.add(new Paragraph(pdfData.name, new Font(customFontBold, 20, Font.NORMAL, new Color(0, 112, 192))));
pList.add(new Paragraph(String.format("- association n°%03d -\n ", pdfData.club_no),
new Font(customFont, 18, Font.TIMES_ROMAN)));
PdfPCell bodyCell = new PdfPCell();
pList.forEach(p -> p.setAlignment(Element.ALIGN_CENTER));
pList.forEach(bodyCell::addElement);
bodyCell.setHorizontalAlignment(Element.ALIGN_CENTER);
bodyCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
bodyCell.setBorder(PdfPCell.NO_BORDER);
mainTable.addCell(bodyCell);
PdfPTable footerRow = new PdfPTable(2);
footerRow.setWidths(new float[]{2, 1});
footerRow.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
PdfPTable footerSubRow = new PdfPTable(2);
footerSubRow.setWidths(new float[]{1, 5});
footerSubRow.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
Image logo2 = Image.getInstance(
Objects.requireNonNull(
getClass().getClassLoader().getResource("safef.jpg")));
logo2.scaleToFit(65, 65);
PdfPCell logo2Cell = new PdfPCell(logo2);
logo2Cell.setHorizontalAlignment(Element.ALIGN_LEFT);
logo2Cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
logo2Cell.setPadding(0);
logo2Cell.setRowspan(1);
logo2Cell.setPaddingLeft(7);
logo2Cell.setBorder(PdfPCell.NO_BORDER);
footerSubRow.addCell(logo2Cell);
pList.clear();
pList.add(new Paragraph("La FFSAF est fondatrice et affilié à la SAFE Federation.",
new Font(customFontBold, 10, Font.NORMAL)));
pList.add(new Paragraph("Soft Armored Fighting European Federation",
new Font(customFontLight, 10, Font.TIMES_ROMAN)));
PdfPCell footerCell = new PdfPCell();
pList.forEach(p -> p.setAlignment(Element.ALIGN_LEFT));
pList.forEach(footerCell::addElement);
footerCell.setHorizontalAlignment(Element.ALIGN_LEFT);
footerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
footerCell.setBorder(PdfPCell.NO_BORDER);
footerSubRow.addCell(footerCell);
footerRow.addCell(footerSubRow);
pList.clear();
pList.add(new Paragraph("Le président de la FFSAF,", new Font(customFont, 13, Font.NORMAL)));
pList.add(new Paragraph("Guillaume Andrieux", new Font(customFontBold, 13, Font.NORMAL)));
PdfPCell footerCell2 = new PdfPCell();
pList.forEach(p -> p.setAlignment(Element.ALIGN_LEFT));
pList.forEach(footerCell2::addElement);
Image sign = Image.getInstance(Files.readAllBytes(pdfData.sign_file.toPath()));
sign.scaleToFit(120, 60);
sign.setSpacingBefore(10);
footerCell2.addElement(sign);
footerCell2.setHorizontalAlignment(Element.ALIGN_LEFT);
footerCell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
footerCell2.setBorder(PdfPCell.NO_BORDER);
footerRow.addCell(footerCell2);
mainTable.addCell(footerRow);
document.add(mainTable);
// Close the document
document.close();
}
private void make_pdf(PdfData pdfData, FileOutputStream out) throws IOException {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Document document = new Document();
@@ -81,7 +275,7 @@ public class Main {
// Adding logo
Image logo = Image.getInstance(
Objects.requireNonNull(
getClass().getClassLoader().getResource("FFSSAF-bord-blanc-fond-transparent.png")));
getClass().getClassLoader().getResource("Logo-FFSAF-2023.png")));
logo.scaleToFit(120, 120);
PdfPCell logoCell = new PdfPCell(logo);
logoCell.setHorizontalAlignment(Element.ALIGN_CENTER);
@@ -135,7 +329,8 @@ public class Main {
// Adding member photo
Image memberPhoto;
if (pdfData.photo_file != null && pdfData.photo_file.exists() && pdfData.photo_file.getAbsolutePath().equals("/dev/null")) {
if (pdfData.photo_file != null && pdfData.photo_file.exists() && !pdfData.photo_file.getAbsolutePath()
.equals("/dev/null")) {
memberPhoto = Image.getInstance(Files.readAllBytes(pdfData.photo_file.toPath()));
} else {
memberPhoto = Image.getInstance(
@@ -168,7 +363,7 @@ public class Main {
// Adding member details
memberTable.addCell(new Phrase("NOM : " + pdfData.lname.toUpperCase(), bodyFont));
memberTable.addCell(new Phrase("Prénom : " + pdfData.fname, bodyFont));
memberTable.addCell(new Phrase("Licence n° : " + pdfData.licence, bodyFont));
memberTable.addCell(new Phrase(String.format("Licence n° : %05d", pdfData.licence), bodyFont));
memberTable.addCell(new Phrase("Certificat médical par " + cert[0] + ", le " + cert[1], bodyFont));
memberTable.addCell(new Phrase("")); // Empty cell for spacing
@@ -180,7 +375,7 @@ public class Main {
Paragraph memberClub = new Paragraph("CLUB : " + pdfData.club.toUpperCase(), bodyFont);
document.add(memberClub);
Paragraph memberClubNumber = new Paragraph("N° club : " + pdfData.club_no, bodyFont);
Paragraph memberClubNumber = new Paragraph(String.format("N° club : %03d ", pdfData.club_no), bodyFont);
document.add(memberClubNumber);
// Adding spacing

View File

@@ -0,0 +1,10 @@
package fr.titionfire;
import java.io.File;
public class PdfDataClub {
int saison;
int club_no;
String name;
public File sign_file;
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 63 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 155 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB