OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 132 Rev 177
Line 17... Line 17...
17
import org.openconcerto.sql.model.DBRoot;
17
import org.openconcerto.sql.model.DBRoot;
18
import org.openconcerto.sql.preferences.SQLPreferences;
18
import org.openconcerto.sql.preferences.SQLPreferences;
19
import org.openconcerto.utils.ExceptionHandler;
19
import org.openconcerto.utils.ExceptionHandler;
20
import org.openconcerto.utils.FileUtils;
20
import org.openconcerto.utils.FileUtils;
21
 
21
 
22
import java.awt.Graphics2D;
-
 
23
import java.io.File;
22
import java.io.File;
24
import java.io.FileOutputStream;
23
import java.io.FileOutputStream;
25
import java.io.FilenameFilter;
24
import java.io.FilenameFilter;
26
import java.io.IOException;
25
import java.io.IOException;
27
import java.util.ArrayList;
26
import java.util.ArrayList;
-
 
27
import java.util.Calendar;
28
import java.util.Collections;
28
import java.util.Collections;
29
import java.util.Comparator;
29
import java.util.Comparator;
30
import java.util.List;
30
import java.util.List;
31
 
31
 
32
import javax.swing.JOptionPane;
32
import javax.swing.JOptionPane;
33
import javax.swing.SwingUtilities;
33
import javax.swing.SwingUtilities;
34
 
34
 
-
 
35
import org.apache.pdfbox.pdmodel.PDDocument;
-
 
36
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
-
 
37
import org.apache.pdfbox.pdmodel.PDPage;
-
 
38
import org.apache.pdfbox.pdmodel.PDPageContentStream;
-
 
39
import org.apache.pdfbox.pdmodel.common.PDRectangle;
-
 
40
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
-
 
41
import org.apache.pdfbox.util.Matrix;
35
import org.jopendocument.model.OpenDocument;
42
import org.jopendocument.model.OpenDocument;
36
import org.jopendocument.renderer.ODTRenderer;
43
import org.jopendocument.renderer.ODTRenderer;
37
 
44
 
38
import com.lowagie.text.Document;
-
 
39
import com.lowagie.text.PageSize;
45
import com.lowagie.text.PageSize;
-
 
46
 
40
import com.lowagie.text.pdf.PdfContentByte;
47
import de.rototor.pdfbox.graphics2d.PdfBoxGraphics2D;
41
import com.lowagie.text.pdf.PdfWriter;
48
import de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DFontTextDrawer;
-
 
49
import de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DFontTextDrawerDefaultFonts;
42
 
50
 
43
public class SheetUtils {
51
public class SheetUtils {
44
 
52
 
45
    public static File convertToOldFile(DBRoot root, String fileName, File pathDest, File fDest) {
53
    public static File convertToOldFile(DBRoot root, String fileName, File pathDest, File fDest) {
46
        // FIXME: !!!!!!!!
54
        // FIXME: !!!!!!!!
Line 130... Line 138...
130
        return result;
138
        return result;
131
    }
139
    }
132
 
140
 
133
    public static void convert2PDF(final OpenDocument doc, final File pdfFileToCreate) throws Exception {
141
    public static void convert2PDF(final OpenDocument doc, final File pdfFileToCreate) throws Exception {
134
        assert (!SwingUtilities.isEventDispatchThread());
142
        assert (!SwingUtilities.isEventDispatchThread());
135
        // Open the PDF document
-
 
136
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);
-
 
137
 
-
 
138
        try {
143
        try {
-
 
144
            PDDocument document = new PDDocument();
-
 
145
            PDDocumentInformation info = new PDDocumentInformation();
-
 
146
            info.setCreator("OpenConcerto");
-
 
147
            info.setProducer("OpenConcerto");
-
 
148
            info.setCreationDate(Calendar.getInstance());
-
 
149
            info.setModificationDate(Calendar.getInstance());
-
 
150
            document.setDocumentInformation(info);
139
 
151
 
140
            FileOutputStream fileOutputStream = new FileOutputStream(pdfFileToCreate);
152
            FileOutputStream fileOutputStream = new FileOutputStream(pdfFileToCreate);
141
 
153
 
142
            // Create the writer
-
 
143
            PdfWriter writer = PdfWriter.getInstance(document, fileOutputStream);
154
            PdfBoxGraphics2DFontTextDrawer fontTextDrawer = new PdfBoxGraphics2DFontTextDrawerDefaultFonts();
144
            writer.setPdfVersion(PdfWriter.VERSION_1_6);
-
 
145
            writer.setFullCompression();
155
            final File dir = new File("Fonts");
146
 
-
 
147
            document.open();
156
            if (dir.exists()) {
148
 
-
 
149
            PdfContentByte cb = writer.getDirectContent();
157
                fontTextDrawer.registerFontFromDirectory(dir);
-
 
158
            }
150
 
159
 
151
            // Configure the renderer
160
            // Configure the renderer
152
            ODTRenderer renderer = new ODTRenderer(doc);
161
            ODTRenderer renderer = new ODTRenderer(doc);
153
            renderer.setIgnoreMargins(false);
162
            renderer.setIgnoreMargins(false);
154
            renderer.setPaintMaxResolution(true);
163
            renderer.setPaintMaxResolution(true);
-
 
164
            PDRectangle pageSize = PDRectangle.A4;
155
 
165
 
156
            // Scale the renderer to fit width or height
166
            // Scale the renderer to fit width or height
157
            final double widthFactor = renderer.getPrintWidth() / document.getPageSize().getWidth();
167
            final double widthFactor = renderer.getPrintWidth() / pageSize.getWidth();
158
            final double heightFactor = renderer.getPrintHeight() / document.getPageSize().getHeight();
168
            final double heightFactor = renderer.getPrintHeight() / pageSize.getHeight();
159
            renderer.setResizeFactor(Math.max(widthFactor, heightFactor));
169
            renderer.setResizeFactor(Math.max(widthFactor, heightFactor));
160
 
170
 
161
            // Print pages
171
            // Print pages
162
            for (int i = 0; i < renderer.getPrintedPagesNumber(); i++) {
172
            for (int i = 0; i < renderer.getPrintedPagesNumber(); i++) {
163
 
-
 
164
                Graphics2D g2 = cb.createGraphics(PageSize.A4.getWidth(), PageSize.A4.getHeight());
173
                PDPage page = new PDPage(pageSize);
165
 
-
 
166
                // If you want to prevent copy/paste, you can use
174
                document.addPage(page);
-
 
175
                PdfBoxGraphics2D g2 = new PdfBoxGraphics2D(document, pageSize.getWidth(), pageSize.getHeight());
167
                // g2 = tp.createGraphicsShapes(w, h, true, 0.9f);
176
                g2.setFontTextDrawer(fontTextDrawer);
168
 
177
 
169
                // centrage horizontal, alignement vertical en haut
178
                // centrage horizontal, alignement vertical en haut
170
                g2.translate((PageSize.A4.getWidth() - renderer.getPrintWidthInPixel()) / 2.0, 0);
179
                g2.translate((PageSize.A4.getWidth() - renderer.getPrintWidthInPixel()) / 2.0, 0);
171
 
180
 
172
                // Render
181
                // Render
173
                renderer.setCurrentPage(i);
182
                renderer.setCurrentPage(i);
174
                renderer.paintComponent(g2);
183
                renderer.paintComponent(g2);
175
                g2.dispose();
184
                g2.dispose();
176
 
185
 
177
                // Add our spreadsheet in the middle of the page
186
                final PDFormXObject xform = g2.getXFormObject();
178
                if (i < renderer.getPrintedPagesNumber() - 1)
187
                final Matrix matrix = new Matrix();
-
 
188
                matrix.translate(0, 0);
-
 
189
                final PDPageContentStream contentStream = new PDPageContentStream(document, page);
-
 
190
                contentStream.transform(matrix);
-
 
191
                contentStream.drawForm(xform);
179
                    document.newPage();
192
                contentStream.close();
180
 
-
 
181
            }
193
            }
-
 
194
            document.save(fileOutputStream);
182
            // Close the PDF document
195
            // Close the PDF document
183
            document.close();
196
            document.close();
184
            // writer.close();
-
 
185
            fileOutputStream.close();
-
 
186
 
197
 
187
        } catch (Exception originalExn) {
198
        } catch (Exception originalExn) {
188
            ExceptionHandler.handle("Impossible de créer le PDF " + pdfFileToCreate.getAbsolutePath(), originalExn);
199
            ExceptionHandler.handle("Impossible de créer le PDF " + pdfFileToCreate.getAbsolutePath(), originalExn);
189
        }
200
        }
190
 
201