diff options
Diffstat (limited to 'src/main/java')
6 files changed, 438 insertions, 0 deletions
diff --git a/src/main/java/it/alessandroiezzi/simplyreports/pdf/PDFDocument.java b/src/main/java/it/alessandroiezzi/simplyreports/pdf/PDFDocument.java new file mode 100644 index 0000000..eb59390 --- /dev/null +++ b/src/main/java/it/alessandroiezzi/simplyreports/pdf/PDFDocument.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2022 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it> + * + * This file is part of Simply Reports. + * + * Simply Reports is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Simply Reports is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Simply Reports. If not, see <https://www.gnu.org/licenses/>. + */ + +package it.alessandroiezzi.simplyreports.pdf; + +import com.lowagie.text.Document; +import com.lowagie.text.DocumentException; +import com.lowagie.text.Element; +import com.lowagie.text.pdf.PdfWriter; +import lombok.Getter; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +public class PDFDocument { + private final Document document; + private final PdfWriter writer; + private final List<Object> elements = new ArrayList<>(); + private final @Getter String fileDestination; + + public PDFDocument(String fileDestination) throws IOException, DocumentException { + this.fileDestination = fileDestination; + document = new Document(); + writer = PdfWriter.getInstance(document, Files.newOutputStream(Paths.get(fileDestination))); + } + + private void openDocument() { + if (document.isOpen()) return; + document.open(); + } + + private void add(Element element) { + elements.add(element); + } + + public void add(PDFTable<?> table) { + elements.add(table); + } + + public void saveToFile() { + try { + openDocument(); + + for (Object element : elements) { + if (element instanceof Element) { + document.add((Element) element); + } else if (element instanceof PDFTable) { + document.add(((PDFTable<?>) element).generate(writer)); + } + } + + if (document.isOpen()) + document.close(); + } catch (DocumentException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/it/alessandroiezzi/simplyreports/pdf/PDFEntity.java b/src/main/java/it/alessandroiezzi/simplyreports/pdf/PDFEntity.java new file mode 100644 index 0000000..fd87310 --- /dev/null +++ b/src/main/java/it/alessandroiezzi/simplyreports/pdf/PDFEntity.java @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2022 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it> + * + * This file is part of Simply Reports. + * + * Simply Reports is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Simply Reports is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Simply Reports. If not, see <https://www.gnu.org/licenses/>. + */ + +package it.alessandroiezzi.simplyreports.pdf; + +import com.lowagie.text.pdf.PdfWriter; + +public abstract class PDFEntity<T> { + protected abstract T generate(PdfWriter writer); +} diff --git a/src/main/java/it/alessandroiezzi/simplyreports/pdf/PDFTable.java b/src/main/java/it/alessandroiezzi/simplyreports/pdf/PDFTable.java new file mode 100644 index 0000000..20cac75 --- /dev/null +++ b/src/main/java/it/alessandroiezzi/simplyreports/pdf/PDFTable.java @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2022 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it> + * + * This file is part of Simply Reports. + * + * Simply Reports is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Simply Reports is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Simply Reports. If not, see <https://www.gnu.org/licenses/>. + */ + +package it.alessandroiezzi.simplyreports.pdf; + +import com.lowagie.text.DocumentException; +import com.lowagie.text.pdf.*; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.function.Supplier; + +public abstract class PDFTable<E> extends PDFEntity<PdfPTable> { + private final List<E> entities; + private final List<PdfTableCell> cells = new ArrayList<>(); + private int[] widths = null; + private PDFTableHeader header; + private PDFTableFooter footer; + + public PDFTable(List<E> entities, int...widths) { + this.entities = entities; + this.widths = widths; + generateSignature(); + } + + private void generateSignature() { + SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); + addFooter(new PDFTableFooter(null, + "Generato con Simply Reports il " + sdf.format(new Date()), + null, + null, + 0)); + } + + protected abstract int getNumberOfColumns(); + protected abstract void mapRow(E entity); + + public void addCell(PdfTableCell cell) { + cells.add(cell); + } + + public void addCell(Supplier<String> value) { + addCell(new PdfTableCell(value)); + } + + public void addHeader(PDFTableHeader header) { + this.header = header; + } + + public void addFooter(PDFTableFooter footer) { + this.footer = footer; + } + + @Override + protected PdfPTable generate(PdfWriter writer) { + try { + PdfPTable table = new PdfPTable(getNumberOfColumns()); + table.setWidthPercentage(100); + + if (widths != null && widths.length > 0) + table.setWidths(widths); + + for (E entity : this.entities) { + mapRow(entity); + } + + if (header != null && writer != null) { + if (header.getColspan() == null) { + header.setColspan(widths.length); + } + table.addCell(header.generate(writer)); + } + + for (PdfTableCell cell : cells) { + table.addCell(cell.getValue()); + } + + if (footer != null && writer != null) { + if (footer.getColspan() == null) { + footer.setColspan(widths.length); + } + table.addCell(footer.generate(writer)); + } + + return table; + } catch (DocumentException ex) { + throw new RuntimeException(ex); + } + } +} diff --git a/src/main/java/it/alessandroiezzi/simplyreports/pdf/PDFTableFooter.java b/src/main/java/it/alessandroiezzi/simplyreports/pdf/PDFTableFooter.java new file mode 100644 index 0000000..f6ce55e --- /dev/null +++ b/src/main/java/it/alessandroiezzi/simplyreports/pdf/PDFTableFooter.java @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2022 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it> + * + * This file is part of Simply Reports. + * + * Simply Reports is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Simply Reports is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Simply Reports. If not, see <https://www.gnu.org/licenses/>. + */ + +package it.alessandroiezzi.simplyreports.pdf; + +import com.lowagie.text.*; +import com.lowagie.text.Font; +import com.lowagie.text.Image; +import com.lowagie.text.pdf.*; +import java.awt.*; +import java.io.IOException; +import lombok.*; + +public class PDFTableFooter extends PDFEntity<PdfPCell> { + private PdfReader reader; + private @Getter @Setter Integer colspan; + private @Getter @Setter Integer height; + private @Getter @Setter Integer border; + private @Getter @Setter String title; + + public PDFTableFooter(String logoLocation) { + this(logoLocation, null, null, null, null); + } + + public PDFTableFooter(String logoLocation, String title, Integer colspan, Integer height, Integer border) { + if (logoLocation != null) { + try { + this.reader = new PdfReader(logoLocation); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + this.colspan = colspan; + this.height = height; + this.border = border; + this.title = title; + } + + @Override + protected PdfPCell generate(PdfWriter writer) { + try { + PdfPCell headerCell = new PdfPCell(); + headerCell.setPadding(5f); + + if (reader != null) { + PdfImportedPage header = writer.getImportedPage(reader, 1); + /* PdfPCell cell = new PdfPCell(Image.getInstance(header)); */ + Image headerImage = Image.getInstance(header); + headerImage.setWidthPercentage(20f); + headerCell.addElement(headerImage); + } + + if (height != null) { + headerCell.setFixedHeight(height); + } + + if (border != null) { + headerCell.setBorder(border); + } + + if (colspan != null) { + headerCell.setColspan(colspan); + } + + if (title != null) { + Font f = new Font(Font.HELVETICA, 8.0f, Font.ITALIC, Color.BLACK); +// you created a font, but you never used it: + Chunk c = new Chunk(title, f); + /* c.setBackground(Color.RED); */ +// you changed the alignment AFTER adding p1 to the document + Paragraph p = new Paragraph(c); + + + + p.setAlignment(Element.ALIGN_CENTER); + headerCell.addElement(p); + } + + return headerCell; + } catch (BadElementException ex) { + throw new RuntimeException(ex); + } + } +} diff --git a/src/main/java/it/alessandroiezzi/simplyreports/pdf/PDFTableHeader.java b/src/main/java/it/alessandroiezzi/simplyreports/pdf/PDFTableHeader.java new file mode 100644 index 0000000..0df5515 --- /dev/null +++ b/src/main/java/it/alessandroiezzi/simplyreports/pdf/PDFTableHeader.java @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2022 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it> + * + * This file is part of Simply Reports. + * + * Simply Reports is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Simply Reports is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Simply Reports. If not, see <https://www.gnu.org/licenses/>. + */ + +package it.alessandroiezzi.simplyreports.pdf; + +import com.lowagie.text.BadElementException; +import com.lowagie.text.Element; +import com.lowagie.text.Image; +import com.lowagie.text.Paragraph; +import com.lowagie.text.pdf.PdfImportedPage; +import com.lowagie.text.pdf.PdfPCell; +import com.lowagie.text.pdf.PdfReader; +import com.lowagie.text.pdf.PdfWriter; +import java.io.IOException; + +import lombok.*; + +public class PDFTableHeader extends PDFEntity<PdfPCell> { + private PdfReader reader; + private @Getter @Setter Integer colspan; + private @Getter @Setter Integer height; + private @Getter @Setter Integer border; + private @Getter @Setter String title; + + public PDFTableHeader(String logoLocation) { + this(logoLocation, null, null, null, null); + } + + public PDFTableHeader(String logoLocation, String title, Integer colspan, Integer height, Integer border) { + try { + this.reader = new PdfReader(logoLocation); + } catch (IOException e) { + throw new RuntimeException(e); + } + this.colspan = colspan; + this.height = height; + this.border = border; + this.title = title; + } + + @Override + protected PdfPCell generate(PdfWriter writer) { + try { + PdfPCell headerCell = new PdfPCell(); + headerCell.setPadding(5f); + + if (reader != null) { + PdfImportedPage header = writer.getImportedPage(reader, 1); + /* PdfPCell cell = new PdfPCell(Image.getInstance(header)); */ + Image headerImage = Image.getInstance(header); + headerImage.setWidthPercentage(20f); + headerCell.addElement(headerImage); + } + + if (height != null) { + headerCell.setFixedHeight(height); + } + + if (border != null) { + headerCell.setBorder(border); + } + + if (colspan != null) { + headerCell.setColspan(colspan); + } + + if (title != null) { + Paragraph p = new Paragraph(title); + p.setAlignment(Element.ALIGN_CENTER); + headerCell.addElement(p); + } + + return headerCell; + } catch (BadElementException ex) { + throw new RuntimeException(ex); + } + } +} diff --git a/src/main/java/it/alessandroiezzi/simplyreports/pdf/PdfTableCell.java b/src/main/java/it/alessandroiezzi/simplyreports/pdf/PdfTableCell.java new file mode 100644 index 0000000..f98870d --- /dev/null +++ b/src/main/java/it/alessandroiezzi/simplyreports/pdf/PdfTableCell.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2022 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it> + * + * This file is part of Simply Reports. + * + * Simply Reports is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Simply Reports is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Simply Reports. If not, see <https://www.gnu.org/licenses/>. + */ + +package it.alessandroiezzi.simplyreports.pdf; + +import java.util.function.Supplier; + +public class PdfTableCell { + private final Supplier<String> value; + + public PdfTableCell(Supplier<String> value) { + this.value = value; + } + + public String getValue() { + return value.get(); + } +} |