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.generationDoc;
|
|
|
15 |
|
57 |
ilm |
16 |
import org.openconcerto.erp.preferences.GenerationDocGlobalPreferencePanel;
|
|
|
17 |
import org.openconcerto.sql.model.DBRoot;
|
|
|
18 |
import org.openconcerto.sql.preferences.SQLPreferences;
|
25 |
ilm |
19 |
import org.openconcerto.utils.ExceptionHandler;
|
73 |
ilm |
20 |
import org.openconcerto.utils.FileUtils;
|
25 |
ilm |
21 |
|
182 |
ilm |
22 |
import java.awt.Color;
|
|
|
23 |
import java.awt.Desktop;
|
|
|
24 |
import java.io.ByteArrayInputStream;
|
18 |
ilm |
25 |
import java.io.File;
|
|
|
26 |
import java.io.FileOutputStream;
|
|
|
27 |
import java.io.FilenameFilter;
|
|
|
28 |
import java.io.IOException;
|
182 |
ilm |
29 |
import java.io.InputStream;
|
18 |
ilm |
30 |
import java.util.ArrayList;
|
177 |
ilm |
31 |
import java.util.Calendar;
|
18 |
ilm |
32 |
import java.util.Collections;
|
|
|
33 |
import java.util.Comparator;
|
182 |
ilm |
34 |
import java.util.GregorianCalendar;
|
|
|
35 |
import java.util.HashMap;
|
18 |
ilm |
36 |
import java.util.List;
|
182 |
ilm |
37 |
import java.util.Map;
|
18 |
ilm |
38 |
|
|
|
39 |
import javax.swing.JOptionPane;
|
|
|
40 |
import javax.swing.SwingUtilities;
|
|
|
41 |
|
177 |
ilm |
42 |
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
43 |
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
|
182 |
ilm |
44 |
import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary;
|
|
|
45 |
import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
|
177 |
ilm |
46 |
import org.apache.pdfbox.pdmodel.PDPage;
|
|
|
47 |
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
|
|
48 |
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
182 |
ilm |
49 |
import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
|
|
|
50 |
import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
|
177 |
ilm |
51 |
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
|
|
|
52 |
import org.apache.pdfbox.util.Matrix;
|
18 |
ilm |
53 |
import org.jopendocument.model.OpenDocument;
|
|
|
54 |
import org.jopendocument.renderer.ODTRenderer;
|
|
|
55 |
|
|
|
56 |
import com.lowagie.text.PageSize;
|
|
|
57 |
|
177 |
ilm |
58 |
import de.rototor.pdfbox.graphics2d.PdfBoxGraphics2D;
|
|
|
59 |
import de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DFontTextDrawer;
|
|
|
60 |
import de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DFontTextDrawerDefaultFonts;
|
|
|
61 |
|
18 |
ilm |
62 |
public class SheetUtils {
|
|
|
63 |
|
57 |
ilm |
64 |
public static File convertToOldFile(DBRoot root, String fileName, File pathDest, File fDest) {
|
25 |
ilm |
65 |
// FIXME: !!!!!!!!
|
57 |
ilm |
66 |
return convertToOldFile(root, fileName, pathDest, fDest, ".ods");
|
18 |
ilm |
67 |
}
|
|
|
68 |
|
73 |
ilm |
69 |
public static File convertToOldFile(DBRoot root, String fileName, File pathDest, File fDest, String extension) {
|
132 |
ilm |
70 |
return convertToOldFile(root, fileName, pathDest, fDest, extension, true);
|
73 |
ilm |
71 |
}
|
|
|
72 |
|
18 |
ilm |
73 |
/**
|
|
|
74 |
* Déplace le fichier, si il existe, dans le répertoire.
|
|
|
75 |
*
|
|
|
76 |
* @param fileName nom du fichier sans extension
|
|
|
77 |
* @param pathDest
|
|
|
78 |
* @param fDest
|
|
|
79 |
* @return
|
|
|
80 |
*/
|
73 |
ilm |
81 |
public static File convertToOldFile(DBRoot root, String fileName, File pathDest, File fDest, String extension, boolean move) {
|
57 |
ilm |
82 |
SQLPreferences prefs = new SQLPreferences(root);
|
|
|
83 |
if (prefs.getBoolean(GenerationDocGlobalPreferencePanel.HISTORIQUE, true) && fDest.exists()) {
|
18 |
ilm |
84 |
int i = 0;
|
|
|
85 |
String destName = fileName;
|
|
|
86 |
File pathOld = new File(pathDest, "Historique");
|
|
|
87 |
pathOld.mkdirs();
|
|
|
88 |
while (fDest.exists()) {
|
|
|
89 |
destName = fileName + "_" + i;
|
|
|
90 |
fDest = new File(pathOld, destName + extension);
|
|
|
91 |
i++;
|
|
|
92 |
}
|
|
|
93 |
File fTmp = new File(pathDest, fileName + extension);
|
|
|
94 |
|
73 |
ilm |
95 |
if (move) {
|
|
|
96 |
if (!fTmp.renameTo(fDest)) {
|
|
|
97 |
final File finalFile = fDest;
|
|
|
98 |
System.err.println("Unable to rename:" + fTmp.getAbsolutePath());
|
|
|
99 |
System.err.println("To:" + fDest.getAbsolutePath());
|
|
|
100 |
SwingUtilities.invokeLater(new Runnable() {
|
|
|
101 |
public void run() {
|
|
|
102 |
try {
|
18 |
ilm |
103 |
|
73 |
ilm |
104 |
JOptionPane.showMessageDialog(null, "Le fichier " + finalFile.getCanonicalPath()
|
|
|
105 |
+ " n'a pu être créé. \n Impossible de déplacer le fichier existant dans l'historique.\n Vérifier que le document n'est pas déjà ouvert.");
|
|
|
106 |
} catch (IOException e) {
|
|
|
107 |
e.printStackTrace();
|
|
|
108 |
}
|
18 |
ilm |
109 |
}
|
73 |
ilm |
110 |
});
|
|
|
111 |
return fTmp;
|
|
|
112 |
}
|
|
|
113 |
} else {
|
|
|
114 |
try {
|
|
|
115 |
FileUtils.copyFile(fTmp, fDest);
|
|
|
116 |
} catch (IOException exn) {
|
|
|
117 |
// TODO Bloc catch auto-généré
|
|
|
118 |
ExceptionHandler.handle("Une erreur est survenue lors de la copie du fichier.", exn);
|
|
|
119 |
}
|
18 |
ilm |
120 |
}
|
|
|
121 |
fDest = new File(pathDest, fileName + extension);
|
|
|
122 |
}
|
|
|
123 |
return fDest;
|
|
|
124 |
}
|
|
|
125 |
|
25 |
ilm |
126 |
public static List<File> getHistorique(final String fileName, File pathDest) {
|
18 |
ilm |
127 |
File pathOld = new File(pathDest, "Historique");
|
|
|
128 |
File[] files = pathOld.listFiles(new FilenameFilter() {
|
|
|
129 |
|
|
|
130 |
@Override
|
|
|
131 |
public boolean accept(File dir, String name) {
|
|
|
132 |
return name.startsWith(fileName);
|
|
|
133 |
|
|
|
134 |
}
|
|
|
135 |
});
|
|
|
136 |
List<File> result = new ArrayList<File>();
|
|
|
137 |
if (files != null) {
|
|
|
138 |
for (int i = 0; i < files.length; i++) {
|
|
|
139 |
result.add(files[i]);
|
|
|
140 |
}
|
|
|
141 |
}
|
|
|
142 |
Collections.sort(result, new Comparator<File>() {
|
|
|
143 |
@Override
|
|
|
144 |
public int compare(File o1, File o2) {
|
|
|
145 |
// TODO Auto-generated method stub
|
|
|
146 |
return o1.getName().compareTo(o2.getName());
|
|
|
147 |
}
|
|
|
148 |
});
|
|
|
149 |
return result;
|
|
|
150 |
}
|
|
|
151 |
|
25 |
ilm |
152 |
public static void convert2PDF(final OpenDocument doc, final File pdfFileToCreate) throws Exception {
|
182 |
ilm |
153 |
convert2PDF(doc, pdfFileToCreate, Collections.emptyList());
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
public static void convert2PDF(final OpenDocument doc, final File pdfFileToCreate, final List<PDFAttachment> attachments) throws Exception {
|
65 |
ilm |
157 |
assert (!SwingUtilities.isEventDispatchThread());
|
182 |
ilm |
158 |
System.out.println("SheetUtils convert2PDF " + doc.getLoadedFile().getAbsolutePath() + " -> " + pdfFileToCreate.getAbsolutePath());
|
65 |
ilm |
159 |
try {
|
177 |
ilm |
160 |
PDDocument document = new PDDocument();
|
|
|
161 |
PDDocumentInformation info = new PDDocumentInformation();
|
|
|
162 |
info.setCreator("OpenConcerto");
|
|
|
163 |
info.setProducer("OpenConcerto");
|
|
|
164 |
info.setCreationDate(Calendar.getInstance());
|
|
|
165 |
info.setModificationDate(Calendar.getInstance());
|
|
|
166 |
document.setDocumentInformation(info);
|
18 |
ilm |
167 |
|
182 |
ilm |
168 |
// Attachments
|
|
|
169 |
final PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();
|
|
|
170 |
final Map<String, PDComplexFileSpecification> efMap = new HashMap<>();
|
|
|
171 |
for (final PDFAttachment att : attachments) {
|
|
|
172 |
// first create the file specification, which holds the embedded file
|
|
|
173 |
final PDComplexFileSpecification fs = new PDComplexFileSpecification();
|
|
|
174 |
fs.setFile(att.getFileName());
|
|
|
175 |
final InputStream is = new ByteArrayInputStream(att.getBytes());
|
|
|
176 |
final PDEmbeddedFile ef = new PDEmbeddedFile(document, is);
|
|
|
177 |
// set some of the attributes of the embedded file
|
|
|
178 |
ef.setSubtype(att.getMimetype());
|
|
|
179 |
ef.setSize(att.getBytes().length);
|
|
|
180 |
ef.setCreationDate(new GregorianCalendar());
|
|
|
181 |
fs.setEmbeddedFile(ef);
|
|
|
182 |
efMap.put(att.getName(), fs);
|
|
|
183 |
}
|
18 |
ilm |
184 |
|
182 |
ilm |
185 |
efTree.setNames(efMap);
|
|
|
186 |
// attachments are stored as part of the "names" dictionary in the document catalog
|
|
|
187 |
final PDDocumentNameDictionary names = new PDDocumentNameDictionary(document.getDocumentCatalog());
|
|
|
188 |
names.setEmbeddedFiles(efTree);
|
|
|
189 |
document.getDocumentCatalog().setNames(names);
|
|
|
190 |
|
177 |
ilm |
191 |
PdfBoxGraphics2DFontTextDrawer fontTextDrawer = new PdfBoxGraphics2DFontTextDrawerDefaultFonts();
|
|
|
192 |
final File dir = new File("Fonts");
|
|
|
193 |
if (dir.exists()) {
|
180 |
ilm |
194 |
System.out.println("Using fonts dir : " + dir.getAbsolutePath());
|
|
|
195 |
for (File f : dir.listFiles()) {
|
|
|
196 |
if (f.isFile() && f.getName().toLowerCase().endsWith(".ttf")) {
|
|
|
197 |
System.out.println("Registering font : " + f.getAbsolutePath());
|
|
|
198 |
fontTextDrawer.registerFont(f);
|
|
|
199 |
}
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
} else {
|
|
|
203 |
System.out.println("No custom fonts dir found : " + dir.getAbsolutePath());
|
177 |
ilm |
204 |
}
|
18 |
ilm |
205 |
|
65 |
ilm |
206 |
// Configure the renderer
|
|
|
207 |
ODTRenderer renderer = new ODTRenderer(doc);
|
|
|
208 |
renderer.setIgnoreMargins(false);
|
182 |
ilm |
209 |
renderer.setResizeFactor(100);
|
65 |
ilm |
210 |
renderer.setPaintMaxResolution(true);
|
177 |
ilm |
211 |
PDRectangle pageSize = PDRectangle.A4;
|
132 |
ilm |
212 |
// Scale the renderer to fit width or height
|
177 |
ilm |
213 |
final double widthFactor = renderer.getPrintWidth() / pageSize.getWidth();
|
|
|
214 |
final double heightFactor = renderer.getPrintHeight() / pageSize.getHeight();
|
182 |
ilm |
215 |
final int resizeFactor = (int) Math.ceil(Math.max(widthFactor, heightFactor));
|
|
|
216 |
renderer.setResizeFactor(resizeFactor);
|
65 |
ilm |
217 |
// Print pages
|
|
|
218 |
for (int i = 0; i < renderer.getPrintedPagesNumber(); i++) {
|
182 |
ilm |
219 |
final PDPage page = new PDPage(pageSize);
|
177 |
ilm |
220 |
document.addPage(page);
|
|
|
221 |
PdfBoxGraphics2D g2 = new PdfBoxGraphics2D(document, pageSize.getWidth(), pageSize.getHeight());
|
|
|
222 |
g2.setFontTextDrawer(fontTextDrawer);
|
18 |
ilm |
223 |
|
132 |
ilm |
224 |
// centrage horizontal, alignement vertical en haut
|
182 |
ilm |
225 |
final double hMargin = (pageSize.getWidth() - renderer.getPageWidthInPixel()) / 2.0;
|
|
|
226 |
g2.translate(hMargin, 0);
|
132 |
ilm |
227 |
|
65 |
ilm |
228 |
// Render
|
|
|
229 |
renderer.setCurrentPage(i);
|
|
|
230 |
renderer.paintComponent(g2);
|
|
|
231 |
g2.dispose();
|
21 |
ilm |
232 |
|
177 |
ilm |
233 |
final PDFormXObject xform = g2.getXFormObject();
|
|
|
234 |
final Matrix matrix = new Matrix();
|
|
|
235 |
matrix.translate(0, 0);
|
|
|
236 |
final PDPageContentStream contentStream = new PDPageContentStream(document, page);
|
|
|
237 |
contentStream.transform(matrix);
|
|
|
238 |
contentStream.drawForm(xform);
|
|
|
239 |
contentStream.close();
|
182 |
ilm |
240 |
|
65 |
ilm |
241 |
}
|
182 |
ilm |
242 |
document.save(pdfFileToCreate);
|
65 |
ilm |
243 |
// Close the PDF document
|
|
|
244 |
document.close();
|
21 |
ilm |
245 |
|
65 |
ilm |
246 |
} catch (Exception originalExn) {
|
|
|
247 |
ExceptionHandler.handle("Impossible de créer le PDF " + pdfFileToCreate.getAbsolutePath(), originalExn);
|
|
|
248 |
}
|
19 |
ilm |
249 |
|
18 |
ilm |
250 |
}
|
25 |
ilm |
251 |
|
|
|
252 |
/**
|
|
|
253 |
* Get a new file with an other extension
|
|
|
254 |
*
|
|
|
255 |
* @param the file (ex: Test.ods)
|
|
|
256 |
* @param the extension (ex: pdf)
|
132 |
ilm |
257 |
*/
|
25 |
ilm |
258 |
static File getFileWithExtension(File file, String extension) {
|
|
|
259 |
if (!extension.startsWith(".")) {
|
|
|
260 |
extension = "." + extension;
|
|
|
261 |
}
|
|
|
262 |
String name = file.getName();
|
|
|
263 |
int i = name.lastIndexOf(".");
|
|
|
264 |
name = name.substring(0, i) + extension;
|
|
|
265 |
final File f = new File(file.getParent(), name);
|
|
|
266 |
return f;
|
|
|
267 |
}
|
182 |
ilm |
268 |
|
|
|
269 |
public static void main(String[] args) throws Exception {
|
|
|
270 |
// final OpenDocument doc = new OpenDocument(new File("Documents/Facture_FACT128.ods"));
|
|
|
271 |
final OpenDocument doc = new OpenDocument(new File("../ODSViewer/documents/bug_marges_pdf/Devis.ods"));
|
|
|
272 |
final File file = new File("out.pdf");
|
|
|
273 |
convert2PDF(doc, file);
|
|
|
274 |
Desktop.getDesktop().open(file);
|
|
|
275 |
}
|
18 |
ilm |
276 |
}
|