18 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
182 |
ilm |
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
18 |
ilm |
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.erp.core.finance.accounting.report;
|
|
|
15 |
|
182 |
ilm |
16 |
import org.openconcerto.erp.generationDoc.TemplateManager;
|
|
|
17 |
import org.openconcerto.ui.preferences.TemplateProps;
|
156 |
ilm |
18 |
|
18 |
ilm |
19 |
import java.awt.Color;
|
|
|
20 |
import java.io.File;
|
182 |
ilm |
21 |
import java.io.FileInputStream;
|
18 |
ilm |
22 |
import java.io.FileNotFoundException;
|
|
|
23 |
import java.io.FileOutputStream;
|
|
|
24 |
import java.io.IOException;
|
182 |
ilm |
25 |
import java.io.InputStream;
|
18 |
ilm |
26 |
import java.util.Calendar;
|
156 |
ilm |
27 |
import java.util.Date;
|
18 |
ilm |
28 |
import java.util.Map;
|
|
|
29 |
|
182 |
ilm |
30 |
import javax.swing.JOptionPane;
|
|
|
31 |
|
156 |
ilm |
32 |
import com.ibm.icu.text.SimpleDateFormat;
|
18 |
ilm |
33 |
import com.lowagie.text.Document;
|
|
|
34 |
import com.lowagie.text.DocumentException;
|
|
|
35 |
import com.lowagie.text.Rectangle;
|
|
|
36 |
import com.lowagie.text.pdf.BaseFont;
|
|
|
37 |
import com.lowagie.text.pdf.PdfContentByte;
|
|
|
38 |
import com.lowagie.text.pdf.PdfImportedPage;
|
|
|
39 |
import com.lowagie.text.pdf.PdfReader;
|
|
|
40 |
import com.lowagie.text.pdf.PdfWriter;
|
|
|
41 |
|
|
|
42 |
public abstract class PdfGenerator {
|
156 |
ilm |
43 |
|
|
|
44 |
private int offsetX;
|
|
|
45 |
private int offsetY;
|
|
|
46 |
private int templateOffsetX;
|
|
|
47 |
private int templateOffsetY;
|
18 |
ilm |
48 |
private PdfContentByte cb;
|
|
|
49 |
private Document document;
|
156 |
ilm |
50 |
private BaseFont bf;
|
|
|
51 |
private BaseFont bfb;
|
18 |
ilm |
52 |
private int width;
|
156 |
ilm |
53 |
private Map<String, String> map;
|
|
|
54 |
private String fileNameIn;
|
|
|
55 |
private String fileNameOut;
|
18 |
ilm |
56 |
private File directoryOut;
|
156 |
ilm |
57 |
private File fOut;
|
18 |
ilm |
58 |
|
|
|
59 |
public static void main(String[] args) {
|
|
|
60 |
new PdfGenerator_2033B().generateFrom(null);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
PdfGenerator(String fileNameIn, String fileNameOut, String directoryOut) {
|
182 |
ilm |
64 |
this.fileNameIn = fileNameIn;
|
18 |
ilm |
65 |
this.fileNameOut = fileNameOut;
|
|
|
66 |
this.directoryOut = new File(directoryOut, String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
|
|
|
67 |
}
|
|
|
68 |
|
156 |
ilm |
69 |
public void generateFrom(Map<String, String> m) {
|
18 |
ilm |
70 |
try {
|
|
|
71 |
if (m == null) {
|
156 |
ilm |
72 |
System.err.println(this + " : filling with defaults");
|
18 |
ilm |
73 |
}
|
|
|
74 |
this.map = m;
|
|
|
75 |
init();
|
|
|
76 |
this.cb.beginText();
|
|
|
77 |
generate();
|
|
|
78 |
this.cb.endText();
|
|
|
79 |
this.document.close();
|
|
|
80 |
} catch (FileNotFoundException e) {
|
182 |
ilm |
81 |
JOptionPane.showMessageDialog(null, "Impossible de générer le fichier.\nModèle PDF non trouvé : " + e.getMessage());
|
18 |
ilm |
82 |
}
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
private void init() throws FileNotFoundException {
|
|
|
86 |
|
|
|
87 |
// we create a reader for a certain document
|
|
|
88 |
PdfReader reader = null;
|
|
|
89 |
PdfWriter writer = null;
|
|
|
90 |
try {
|
182 |
ilm |
91 |
InputStream in = TemplateManager.getInstance().getTemplate(fileNameIn);
|
|
|
92 |
if (in == null) {
|
|
|
93 |
File defaultFile = new File("Configuration/Template/PDF/" + fileNameIn);
|
|
|
94 |
System.err.println("PdfGenerator.init() template " + fileNameIn + " not retrieved from TemplateManager");
|
|
|
95 |
if (defaultFile.exists()) {
|
|
|
96 |
in = new FileInputStream(defaultFile);
|
|
|
97 |
} else {
|
|
|
98 |
System.err.println("PdfGenerator.init() template " + defaultFile.getAbsolutePath() + " not found in program directory");
|
|
|
99 |
}
|
|
|
100 |
final String property = TemplateProps.getInstance().getProperty("LocationTemplate");
|
|
|
101 |
defaultFile = new File(property, fileNameIn);
|
18 |
ilm |
102 |
|
182 |
ilm |
103 |
if (defaultFile.exists()) {
|
|
|
104 |
in = new FileInputStream(defaultFile);
|
|
|
105 |
} else {
|
|
|
106 |
System.err.println("PdfGenerator.init() template " + defaultFile.getAbsolutePath() + " not found in template prefs directory");
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
if (in == null) {
|
|
|
110 |
throw new FileNotFoundException(fileNameIn);
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
reader = new PdfReader(in);
|
|
|
115 |
|
156 |
ilm |
116 |
final Rectangle psize = reader.getPageSize(1);
|
18 |
ilm |
117 |
psize.setRight(psize.getRight() - this.templateOffsetX);
|
|
|
118 |
psize.setTop(psize.getTop() - this.templateOffsetY);
|
|
|
119 |
|
|
|
120 |
this.width = (int) psize.getWidth();
|
|
|
121 |
|
156 |
ilm |
122 |
int margin = 32;
|
|
|
123 |
this.document = new Document(psize, margin, margin, margin, margin);
|
18 |
ilm |
124 |
if (!this.directoryOut.exists()) {
|
|
|
125 |
this.directoryOut.mkdirs();
|
|
|
126 |
}
|
156 |
ilm |
127 |
final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
|
|
|
128 |
final String computedFileName = sdf.format(new Date()) + "-" + this.fileNameOut;
|
18 |
ilm |
129 |
|
156 |
ilm |
130 |
fOut = new File(this.directoryOut, computedFileName);
|
|
|
131 |
System.err.println("Creation du fichier " + fOut.getCanonicalPath());
|
|
|
132 |
writer = PdfWriter.getInstance(this.document, new FileOutputStream(fOut));
|
18 |
ilm |
133 |
this.document.open();
|
|
|
134 |
this.cb = writer.getDirectContent();
|
|
|
135 |
|
|
|
136 |
this.document.newPage();
|
|
|
137 |
|
|
|
138 |
PdfImportedPage page1 = writer.getImportedPage(reader, 1);
|
|
|
139 |
|
|
|
140 |
this.cb.addTemplate(page1, -this.templateOffsetX, -this.templateOffsetY);
|
|
|
141 |
|
|
|
142 |
this.bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.EMBEDDED);
|
|
|
143 |
this.bfb = BaseFont.createFont(BaseFont.TIMES_BOLD, BaseFont.CP1252, BaseFont.EMBEDDED);
|
|
|
144 |
|
|
|
145 |
} catch (FileNotFoundException fE) {
|
|
|
146 |
throw fE;
|
156 |
ilm |
147 |
} catch (IOException | DocumentException e) {
|
|
|
148 |
throw new IllegalStateException(e);
|
18 |
ilm |
149 |
} finally {
|
|
|
150 |
if (reader != null) {
|
|
|
151 |
reader.close();
|
|
|
152 |
}
|
|
|
153 |
}
|
|
|
154 |
}
|
|
|
155 |
|
156 |
ilm |
156 |
public abstract void generate();
|
18 |
ilm |
157 |
|
156 |
ilm |
158 |
public File getGeneratedFile() {
|
|
|
159 |
return this.fOut;
|
18 |
ilm |
160 |
}
|
|
|
161 |
|
|
|
162 |
protected void setOffset(int i, int j) {
|
|
|
163 |
this.offsetX = i;
|
|
|
164 |
this.offsetY = j;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
protected void setTemplateOffset(int i, int j) {
|
|
|
168 |
this.templateOffsetX = i;
|
|
|
169 |
this.templateOffsetY = j;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
protected void addSplittedText(String code, String string, int fromx, int y, double deltax) {
|
156 |
ilm |
173 |
int x = fromx - this.offsetX - this.templateOffsetX;
|
18 |
ilm |
174 |
y = y - this.offsetY - this.templateOffsetY;
|
|
|
175 |
boolean error = false;
|
|
|
176 |
String s = string;
|
|
|
177 |
if (this.map != null) {
|
156 |
ilm |
178 |
s = this.map.get(code);
|
18 |
ilm |
179 |
}
|
|
|
180 |
if (s == null) {
|
|
|
181 |
s = code;
|
|
|
182 |
error = true;
|
|
|
183 |
this.cb.setColorFill(Color.RED);
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
for (int i = 0; i < s.length(); i++) {
|
|
|
187 |
char c = s.charAt(i);
|
|
|
188 |
String sub = String.valueOf(c);
|
|
|
189 |
this.cb.showTextAligned(PdfContentByte.ALIGN_LEFT, sub, x, y, 0);
|
|
|
190 |
x += deltax;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
if (error) {
|
|
|
194 |
this.cb.setColorStroke(Color.BLACK);
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
protected void setFontRoman(int i) {
|
|
|
200 |
this.cb.setFontAndSize(this.bf, i);
|
|
|
201 |
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
protected void setFontBold(int i) {
|
|
|
205 |
this.cb.setFontAndSize(this.bfb, i);
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
protected void addText(String code, String string, int i, int j) {
|
|
|
209 |
addText(code, string, i, j, 0);
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
protected void addText(String code, String string, int i, int j, int k) {
|
|
|
213 |
addText(PdfContentByte.ALIGN_LEFT, code, string, i, j, k);
|
|
|
214 |
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
protected void addTextRight(String code, String string, int i, int j) {
|
|
|
218 |
int a = PdfContentByte.ALIGN_RIGHT;
|
|
|
219 |
int k = 0;
|
|
|
220 |
|
|
|
221 |
if (this.map == null)
|
|
|
222 |
this.cb.showTextAligned(a, string, i, j, k);
|
|
|
223 |
else {
|
|
|
224 |
boolean error = false;
|
156 |
ilm |
225 |
String s = this.map.get(code);
|
18 |
ilm |
226 |
if (s == null) {
|
|
|
227 |
s = code;
|
|
|
228 |
error = true;
|
|
|
229 |
} else {
|
|
|
230 |
if (s.equalsIgnoreCase("-0.0")) {
|
|
|
231 |
s = "0.0";
|
|
|
232 |
}
|
|
|
233 |
s = insertCurrencySpaces(s);
|
|
|
234 |
}
|
|
|
235 |
this.cb.showTextAligned(a, s, i, j, k);
|
|
|
236 |
if (error) {
|
|
|
237 |
this.cb.setColorStroke(Color.BLACK);
|
|
|
238 |
}
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
private final void addText(int a, String code, String string, int i, int j, int k) {
|
|
|
244 |
if (this.map == null)
|
|
|
245 |
this.cb.showTextAligned(a, string, i, j, k);
|
|
|
246 |
else {
|
|
|
247 |
boolean error = false;
|
156 |
ilm |
248 |
String s = this.map.get(code);
|
18 |
ilm |
249 |
if (s == null) {
|
|
|
250 |
s = code;
|
|
|
251 |
error = true;
|
|
|
252 |
}
|
|
|
253 |
this.cb.showTextAligned(a, s, i, j, k);
|
|
|
254 |
if (error) {
|
|
|
255 |
this.cb.setColorStroke(Color.BLACK);
|
|
|
256 |
}
|
|
|
257 |
}
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
protected int getWidth() {
|
|
|
261 |
return this.width;
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
protected static String insertCurrencySpaces(String string) {
|
156 |
ilm |
265 |
final StringBuilder s = new StringBuilder();
|
18 |
ilm |
266 |
for (int i = string.length() - 1; i >= 0; i--) {
|
|
|
267 |
s.insert(0, string.charAt(i));
|
|
|
268 |
if ((i - string.length()) % 3 == 0) {
|
|
|
269 |
s.insert(0, " ");
|
|
|
270 |
}
|
|
|
271 |
}
|
|
|
272 |
return s.toString().trim();
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
}
|