18 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
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 |
|
|
|
16 |
import static org.openconcerto.task.config.ComptaBasePropsConfiguration.getStreamStatic;
|
|
|
17 |
import org.openconcerto.utils.ExceptionHandler;
|
|
|
18 |
|
|
|
19 |
import java.awt.Color;
|
|
|
20 |
import java.awt.Desktop;
|
|
|
21 |
import java.io.File;
|
|
|
22 |
import java.io.FileNotFoundException;
|
|
|
23 |
import java.io.FileOutputStream;
|
|
|
24 |
import java.io.IOException;
|
|
|
25 |
import java.util.Calendar;
|
|
|
26 |
import java.util.HashMap;
|
|
|
27 |
import java.util.Map;
|
|
|
28 |
|
|
|
29 |
import javax.swing.JOptionPane;
|
|
|
30 |
|
|
|
31 |
import com.lowagie.text.Document;
|
|
|
32 |
import com.lowagie.text.DocumentException;
|
|
|
33 |
import com.lowagie.text.Rectangle;
|
|
|
34 |
import com.lowagie.text.pdf.BaseFont;
|
|
|
35 |
import com.lowagie.text.pdf.PdfContentByte;
|
|
|
36 |
import com.lowagie.text.pdf.PdfImportedPage;
|
|
|
37 |
import com.lowagie.text.pdf.PdfReader;
|
|
|
38 |
import com.lowagie.text.pdf.PdfWriter;
|
|
|
39 |
|
|
|
40 |
public abstract class PdfGenerator {
|
|
|
41 |
private int marginX, marginY;
|
|
|
42 |
private int offsetX, offsetY;
|
|
|
43 |
private int templateOffsetX, templateOffsetY;
|
|
|
44 |
private PdfContentByte cb;
|
|
|
45 |
private Document document;
|
|
|
46 |
private BaseFont bf, bfb;
|
|
|
47 |
private int width;
|
|
|
48 |
private Map map;
|
|
|
49 |
private String fileNameIn, fileNameOut;
|
|
|
50 |
private File directoryOut;
|
|
|
51 |
|
|
|
52 |
public static void main(String[] args) {
|
|
|
53 |
new PdfGenerator_2033B().generateFrom(null);
|
142 |
ilm |
54 |
// HashMap h = new HashMap();
|
18 |
ilm |
55 |
// h.put("NOM","Front Software");
|
|
|
56 |
// new PdfGenerator_2033A().generateFrom(h);
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
PdfGenerator(String fileNameIn, String fileNameOut, String directoryOut) {
|
|
|
60 |
|
|
|
61 |
this.fileNameIn = "/Configuration/Template/PDF/" + fileNameIn;
|
|
|
62 |
this.fileNameOut = fileNameOut;
|
|
|
63 |
|
|
|
64 |
System.err.println("First folder " + directoryOut);
|
|
|
65 |
this.directoryOut = new File(directoryOut, String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
public void open(File f) {
|
|
|
69 |
if (Desktop.isDesktopSupported()) {
|
|
|
70 |
Desktop d = Desktop.getDesktop();
|
|
|
71 |
if (d.isSupported(Desktop.Action.OPEN)) {
|
|
|
72 |
|
|
|
73 |
try {
|
|
|
74 |
d.open(f.getCanonicalFile());
|
|
|
75 |
} catch (IOException e) {
|
|
|
76 |
// TODO Auto-generated catch block
|
|
|
77 |
e.printStackTrace();
|
|
|
78 |
}
|
|
|
79 |
} else {
|
|
|
80 |
JOptionPane.showMessageDialog(null, "Cette action n'est pas supporté par votre système d'exploitation.");
|
|
|
81 |
}
|
|
|
82 |
} else {
|
|
|
83 |
JOptionPane.showMessageDialog(null, "Votre système d'exploitation n'est pas supporté.");
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
public void generateFrom(Map m) {
|
|
|
88 |
try {
|
|
|
89 |
if (m == null) {
|
|
|
90 |
System.out.println("Filling with defaults");
|
|
|
91 |
}
|
|
|
92 |
this.map = m;
|
|
|
93 |
|
|
|
94 |
init();
|
|
|
95 |
|
|
|
96 |
this.cb.beginText();
|
|
|
97 |
generate();
|
|
|
98 |
this.cb.endText();
|
|
|
99 |
|
|
|
100 |
// step 5: we close the document
|
|
|
101 |
// FIXME ASK IF CLOSE WRITER
|
|
|
102 |
this.document.close();
|
|
|
103 |
System.out.println("done!");
|
|
|
104 |
} catch (FileNotFoundException e) {
|
|
|
105 |
ExceptionHandler.handle("Impossible de générer le fichier. \n" + e, e);
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
private void init() throws FileNotFoundException {
|
|
|
110 |
|
|
|
111 |
// we create a reader for a certain document
|
|
|
112 |
PdfReader reader = null;
|
|
|
113 |
PdfWriter writer = null;
|
|
|
114 |
try {
|
|
|
115 |
reader = new PdfReader(getStreamStatic(this.fileNameIn));
|
|
|
116 |
|
|
|
117 |
// we retrieve the total number of pages
|
|
|
118 |
int n = reader.getNumberOfPages();
|
|
|
119 |
// we retrieve the size of the first page
|
|
|
120 |
Rectangle psize = reader.getPageSize(1);
|
|
|
121 |
|
|
|
122 |
psize.setRight(psize.getRight() - this.templateOffsetX);
|
|
|
123 |
psize.setTop(psize.getTop() - this.templateOffsetY);
|
|
|
124 |
|
|
|
125 |
this.width = (int) psize.getWidth();
|
|
|
126 |
float height = psize.getHeight();
|
|
|
127 |
|
|
|
128 |
// step 1: creation of a document-object
|
|
|
129 |
int MARGIN = 32;
|
|
|
130 |
this.document = new Document(psize, MARGIN, MARGIN, MARGIN, MARGIN);
|
|
|
131 |
// step 2: we create a writer that listens to the document
|
|
|
132 |
if (!this.directoryOut.exists()) {
|
|
|
133 |
this.directoryOut.mkdirs();
|
|
|
134 |
}
|
|
|
135 |
System.err.println("Directory out " + this.directoryOut.getAbsolutePath());
|
|
|
136 |
File f = new File(this.directoryOut, this.fileNameOut);
|
|
|
137 |
if (f.exists()) {
|
|
|
138 |
f.renameTo(new File(this.directoryOut, "Old" + this.fileNameOut));
|
|
|
139 |
f = new File(this.directoryOut, this.fileNameOut);
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
System.err.println("Creation du fichier " + f.getAbsolutePath());
|
|
|
143 |
|
|
|
144 |
writer = PdfWriter.getInstance(this.document, new FileOutputStream(f));
|
|
|
145 |
|
|
|
146 |
this.document.open();
|
|
|
147 |
// step 4: we add content
|
|
|
148 |
this.cb = writer.getDirectContent();
|
|
|
149 |
|
|
|
150 |
System.out.println("There are " + n + " pages in the document.");
|
|
|
151 |
|
|
|
152 |
this.document.newPage();
|
|
|
153 |
|
|
|
154 |
PdfImportedPage page1 = writer.getImportedPage(reader, 1);
|
|
|
155 |
|
|
|
156 |
this.cb.addTemplate(page1, -this.templateOffsetX, -this.templateOffsetY);
|
|
|
157 |
|
|
|
158 |
this.bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.EMBEDDED);
|
|
|
159 |
this.bfb = BaseFont.createFont(BaseFont.TIMES_BOLD, BaseFont.CP1252, BaseFont.EMBEDDED);
|
|
|
160 |
|
|
|
161 |
} catch (FileNotFoundException fE) {
|
|
|
162 |
throw fE;
|
|
|
163 |
} catch (IOException e) {
|
|
|
164 |
e.printStackTrace();
|
|
|
165 |
} catch (DocumentException e) {
|
|
|
166 |
e.printStackTrace();
|
|
|
167 |
} finally {
|
|
|
168 |
if (reader != null) {
|
|
|
169 |
reader.close();
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
abstract public void generate();
|
|
|
175 |
|
|
|
176 |
protected void setMargin(int i, int j) {
|
|
|
177 |
this.marginX = i;
|
|
|
178 |
this.marginY = j;
|
|
|
179 |
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
protected void setOffset(int i, int j) {
|
|
|
183 |
this.offsetX = i;
|
|
|
184 |
this.offsetY = j;
|
|
|
185 |
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
protected void setTemplateOffset(int i, int j) {
|
|
|
189 |
this.templateOffsetX = i;
|
|
|
190 |
this.templateOffsetY = j;
|
|
|
191 |
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
protected void addSplittedText(String code, String string, int fromx, int y, double deltax) {
|
|
|
195 |
float x = fromx - this.offsetX - this.templateOffsetX;
|
|
|
196 |
y = y - this.offsetY - this.templateOffsetY;
|
|
|
197 |
boolean error = false;
|
|
|
198 |
String s = string;
|
|
|
199 |
if (this.map != null) {
|
|
|
200 |
s = (String) this.map.get(code);
|
|
|
201 |
|
|
|
202 |
}
|
|
|
203 |
if (s == null) {
|
|
|
204 |
s = code;
|
|
|
205 |
error = true;
|
|
|
206 |
this.cb.setColorFill(Color.RED);
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
for (int i = 0; i < s.length(); i++) {
|
|
|
210 |
char c = s.charAt(i);
|
|
|
211 |
String sub = String.valueOf(c);
|
|
|
212 |
|
|
|
213 |
this.cb.showTextAligned(PdfContentByte.ALIGN_LEFT, sub, x, y, 0);
|
|
|
214 |
x += deltax;
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
if (error) {
|
|
|
218 |
|
|
|
219 |
this.cb.setColorStroke(Color.BLACK);
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
protected void setFontRoman(int i) {
|
|
|
225 |
this.cb.setFontAndSize(this.bf, i);
|
|
|
226 |
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
protected void setFontBold(int i) {
|
|
|
230 |
this.cb.setFontAndSize(this.bfb, i);
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
protected void addText(String code, String string, int i, int j) {
|
|
|
234 |
addText(code, string, i, j, 0);
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
protected void addText(String code, String string, int i, int j, int k) {
|
|
|
238 |
addText(PdfContentByte.ALIGN_LEFT, code, string, i, j, k);
|
|
|
239 |
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
protected void addTextRight(String code, String string, int i, int j) {
|
|
|
243 |
// addText(PdfContentByte.ALIGN_RIGHT, code, string, i, j, 0);
|
|
|
244 |
|
|
|
245 |
int a = PdfContentByte.ALIGN_RIGHT;
|
|
|
246 |
int k = 0;
|
|
|
247 |
|
|
|
248 |
if (this.map == null)
|
|
|
249 |
this.cb.showTextAligned(a, string, i, j, k);
|
|
|
250 |
else {
|
|
|
251 |
boolean error = false;
|
|
|
252 |
String s = (String) this.map.get(code);
|
|
|
253 |
if (s == null) {
|
|
|
254 |
System.out.println("Impossibe de trouver: " + code + " Set color red");
|
|
|
255 |
s = code;
|
|
|
256 |
error = true;
|
|
|
257 |
|
|
|
258 |
// cb.setColorFill(Color.RED);
|
|
|
259 |
} else {
|
|
|
260 |
if (s.equalsIgnoreCase("-0.0")) {
|
|
|
261 |
s = "0.0";
|
|
|
262 |
}
|
|
|
263 |
s = insertCurrencySpaces(s);
|
|
|
264 |
}
|
|
|
265 |
System.out.println("print " + s);
|
|
|
266 |
this.cb.showTextAligned(a, s, i, j, k);
|
|
|
267 |
if (error) {
|
|
|
268 |
System.out.println(" Set color black");
|
|
|
269 |
this.cb.setColorStroke(Color.BLACK);
|
|
|
270 |
}
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
private final void addText(int a, String code, String string, int i, int j, int k) {
|
|
|
276 |
|
|
|
277 |
if (this.map == null)
|
|
|
278 |
this.cb.showTextAligned(a, string, i, j, k);
|
|
|
279 |
else {
|
|
|
280 |
boolean error = false;
|
|
|
281 |
String s = (String) this.map.get(code);
|
|
|
282 |
if (s == null) {
|
|
|
283 |
System.out.println("Impossibe de trouver: " + code + " Set color red");
|
|
|
284 |
s = code;
|
|
|
285 |
error = true;
|
|
|
286 |
|
|
|
287 |
// cb.setColorFill(Color.RED);
|
|
|
288 |
}
|
|
|
289 |
System.out.println("print " + s);
|
|
|
290 |
this.cb.showTextAligned(a, s, i, j, k);
|
|
|
291 |
if (error) {
|
|
|
292 |
System.out.println(" Set color black");
|
|
|
293 |
this.cb.setColorStroke(Color.BLACK);
|
|
|
294 |
}
|
|
|
295 |
}
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
protected int getWidth() {
|
|
|
299 |
return this.width;
|
|
|
300 |
}
|
|
|
301 |
|
|
|
302 |
protected static String insertCurrencySpaces(String string) {
|
|
|
303 |
StringBuffer s = new StringBuffer();
|
|
|
304 |
for (int i = string.length() - 1; i >= 0; i--) {
|
|
|
305 |
|
|
|
306 |
s.insert(0, string.charAt(i));
|
|
|
307 |
if ((i - string.length()) % 3 == 0) {
|
|
|
308 |
s.insert(0, " ");
|
|
|
309 |
}
|
|
|
310 |
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
return s.toString().trim();
|
|
|
314 |
}
|
|
|
315 |
|
|
|
316 |
}
|