blob: eb5939089e3c0d932936b0fd8c21f02f06716b37 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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);
}
}
}
|