OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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;
156 ilm 17
 
18 ilm 18
import org.openconcerto.utils.ExceptionHandler;
19
 
20
import java.awt.Color;
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;
156 ilm 26
import java.util.Date;
18 ilm 27
import java.util.Map;
28
 
156 ilm 29
import com.ibm.icu.text.SimpleDateFormat;
18 ilm 30
import com.lowagie.text.Document;
31
import com.lowagie.text.DocumentException;
32
import com.lowagie.text.Rectangle;
33
import com.lowagie.text.pdf.BaseFont;
34
import com.lowagie.text.pdf.PdfContentByte;
35
import com.lowagie.text.pdf.PdfImportedPage;
36
import com.lowagie.text.pdf.PdfReader;
37
import com.lowagie.text.pdf.PdfWriter;
38
 
39
public abstract class PdfGenerator {
156 ilm 40
 
41
    private int offsetX;
42
    private int offsetY;
43
    private int templateOffsetX;
44
    private int templateOffsetY;
18 ilm 45
    private PdfContentByte cb;
46
    private Document document;
156 ilm 47
    private BaseFont bf;
48
    private BaseFont bfb;
18 ilm 49
    private int width;
156 ilm 50
    private Map<String, String> map;
51
    private String fileNameIn;
52
    private String fileNameOut;
18 ilm 53
    private File directoryOut;
156 ilm 54
    private File fOut;
18 ilm 55
 
56
    public static void main(String[] args) {
57
        new PdfGenerator_2033B().generateFrom(null);
58
    }
59
 
60
    PdfGenerator(String fileNameIn, String fileNameOut, String directoryOut) {
61
        this.fileNameIn = "/Configuration/Template/PDF/" + fileNameIn;
62
        this.fileNameOut = fileNameOut;
63
        this.directoryOut = new File(directoryOut, String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
64
    }
65
 
156 ilm 66
    public void generateFrom(Map<String, String> m) {
18 ilm 67
        try {
68
            if (m == null) {
156 ilm 69
                System.err.println(this + " : filling with defaults");
18 ilm 70
            }
71
            this.map = m;
72
            init();
73
            this.cb.beginText();
74
            generate();
75
            this.cb.endText();
76
            this.document.close();
77
        } catch (FileNotFoundException e) {
78
            ExceptionHandler.handle("Impossible de générer le fichier. \n" + e, e);
79
        }
80
    }
81
 
82
    private void init() throws FileNotFoundException {
83
 
84
        // we create a reader for a certain document
85
        PdfReader reader = null;
86
        PdfWriter writer = null;
87
        try {
88
            reader = new PdfReader(getStreamStatic(this.fileNameIn));
89
 
156 ilm 90
            final Rectangle psize = reader.getPageSize(1);
18 ilm 91
            psize.setRight(psize.getRight() - this.templateOffsetX);
92
            psize.setTop(psize.getTop() - this.templateOffsetY);
93
 
94
            this.width = (int) psize.getWidth();
95
 
156 ilm 96
            int margin = 32;
97
            this.document = new Document(psize, margin, margin, margin, margin);
18 ilm 98
            if (!this.directoryOut.exists()) {
99
                this.directoryOut.mkdirs();
100
            }
156 ilm 101
            final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
102
            final String computedFileName = sdf.format(new Date()) + "-" + this.fileNameOut;
18 ilm 103
 
156 ilm 104
            fOut = new File(this.directoryOut, computedFileName);
105
            System.err.println("Creation du fichier " + fOut.getCanonicalPath());
106
            writer = PdfWriter.getInstance(this.document, new FileOutputStream(fOut));
18 ilm 107
            this.document.open();
108
            this.cb = writer.getDirectContent();
109
 
110
            this.document.newPage();
111
 
112
            PdfImportedPage page1 = writer.getImportedPage(reader, 1);
113
 
114
            this.cb.addTemplate(page1, -this.templateOffsetX, -this.templateOffsetY);
115
 
116
            this.bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.EMBEDDED);
117
            this.bfb = BaseFont.createFont(BaseFont.TIMES_BOLD, BaseFont.CP1252, BaseFont.EMBEDDED);
118
 
119
        } catch (FileNotFoundException fE) {
120
            throw fE;
156 ilm 121
        } catch (IOException | DocumentException e) {
122
            throw new IllegalStateException(e);
18 ilm 123
        } finally {
124
            if (reader != null) {
125
                reader.close();
126
            }
127
        }
128
    }
129
 
156 ilm 130
    public abstract void generate();
18 ilm 131
 
156 ilm 132
    public File getGeneratedFile() {
133
        return this.fOut;
18 ilm 134
    }
135
 
136
    protected void setOffset(int i, int j) {
137
        this.offsetX = i;
138
        this.offsetY = j;
139
    }
140
 
141
    protected void setTemplateOffset(int i, int j) {
142
        this.templateOffsetX = i;
143
        this.templateOffsetY = j;
144
    }
145
 
146
    protected void addSplittedText(String code, String string, int fromx, int y, double deltax) {
156 ilm 147
        int x = fromx - this.offsetX - this.templateOffsetX;
18 ilm 148
        y = y - this.offsetY - this.templateOffsetY;
149
        boolean error = false;
150
        String s = string;
151
        if (this.map != null) {
156 ilm 152
            s = this.map.get(code);
18 ilm 153
        }
154
        if (s == null) {
155
            s = code;
156
            error = true;
157
            this.cb.setColorFill(Color.RED);
158
        }
159
 
160
        for (int i = 0; i < s.length(); i++) {
161
            char c = s.charAt(i);
162
            String sub = String.valueOf(c);
163
            this.cb.showTextAligned(PdfContentByte.ALIGN_LEFT, sub, x, y, 0);
164
            x += deltax;
165
        }
166
 
167
        if (error) {
168
            this.cb.setColorStroke(Color.BLACK);
169
        }
170
 
171
    }
172
 
173
    protected void setFontRoman(int i) {
174
        this.cb.setFontAndSize(this.bf, i);
175
 
176
    }
177
 
178
    protected void setFontBold(int i) {
179
        this.cb.setFontAndSize(this.bfb, i);
180
    }
181
 
182
    protected void addText(String code, String string, int i, int j) {
183
        addText(code, string, i, j, 0);
184
    }
185
 
186
    protected void addText(String code, String string, int i, int j, int k) {
187
        addText(PdfContentByte.ALIGN_LEFT, code, string, i, j, k);
188
 
189
    }
190
 
191
    protected void addTextRight(String code, String string, int i, int j) {
192
        int a = PdfContentByte.ALIGN_RIGHT;
193
        int k = 0;
194
 
195
        if (this.map == null)
196
            this.cb.showTextAligned(a, string, i, j, k);
197
        else {
198
            boolean error = false;
156 ilm 199
            String s = this.map.get(code);
18 ilm 200
            if (s == null) {
201
                s = code;
202
                error = true;
203
            } else {
204
                if (s.equalsIgnoreCase("-0.0")) {
205
                    s = "0.0";
206
                }
207
                s = insertCurrencySpaces(s);
208
            }
209
            this.cb.showTextAligned(a, s, i, j, k);
210
            if (error) {
211
                this.cb.setColorStroke(Color.BLACK);
212
            }
213
        }
214
 
215
    }
216
 
217
    private final void addText(int a, String code, String string, int i, int j, int k) {
218
        if (this.map == null)
219
            this.cb.showTextAligned(a, string, i, j, k);
220
        else {
221
            boolean error = false;
156 ilm 222
            String s = this.map.get(code);
18 ilm 223
            if (s == null) {
224
                s = code;
225
                error = true;
226
            }
227
            this.cb.showTextAligned(a, s, i, j, k);
228
            if (error) {
229
                this.cb.setColorStroke(Color.BLACK);
230
            }
231
        }
232
    }
233
 
234
    protected int getWidth() {
235
        return this.width;
236
    }
237
 
238
    protected static String insertCurrencySpaces(String string) {
156 ilm 239
        final StringBuilder s = new StringBuilder();
18 ilm 240
        for (int i = string.length() - 1; i >= 0; i--) {
241
            s.insert(0, string.charAt(i));
242
            if ((i - string.length()) % 3 == 0) {
243
                s.insert(0, " ");
244
            }
245
        }
246
        return s.toString().trim();
247
    }
248
 
249
}