Line 1... |
Line 1... |
1 |
/*
|
1 |
/*
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
3 |
*
|
3 |
*
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
5 |
*
|
5 |
*
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
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
|
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
|
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.
|
9 |
* language governing permissions and limitations under the License.
|
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.Color;
|
- |
|
23 |
import java.awt.Desktop;
|
- |
|
24 |
import java.io.ByteArrayInputStream;
|
22 |
import java.io.File;
|
25 |
import java.io.File;
|
23 |
import java.io.FileOutputStream;
|
26 |
import java.io.FileOutputStream;
|
24 |
import java.io.FilenameFilter;
|
27 |
import java.io.FilenameFilter;
|
25 |
import java.io.IOException;
|
28 |
import java.io.IOException;
|
- |
|
29 |
import java.io.InputStream;
|
26 |
import java.util.ArrayList;
|
30 |
import java.util.ArrayList;
|
27 |
import java.util.Calendar;
|
31 |
import java.util.Calendar;
|
28 |
import java.util.Collections;
|
32 |
import java.util.Collections;
|
29 |
import java.util.Comparator;
|
33 |
import java.util.Comparator;
|
- |
|
34 |
import java.util.GregorianCalendar;
|
- |
|
35 |
import java.util.HashMap;
|
30 |
import java.util.List;
|
36 |
import java.util.List;
|
- |
|
37 |
import java.util.Map;
|
31 |
|
38 |
|
32 |
import javax.swing.JOptionPane;
|
39 |
import javax.swing.JOptionPane;
|
33 |
import javax.swing.SwingUtilities;
|
40 |
import javax.swing.SwingUtilities;
|
34 |
|
41 |
|
35 |
import org.apache.pdfbox.pdmodel.PDDocument;
|
42 |
import org.apache.pdfbox.pdmodel.PDDocument;
|
36 |
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
|
43 |
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
|
- |
|
44 |
import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary;
|
- |
|
45 |
import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
|
37 |
import org.apache.pdfbox.pdmodel.PDPage;
|
46 |
import org.apache.pdfbox.pdmodel.PDPage;
|
38 |
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
47 |
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
39 |
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
48 |
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
- |
|
49 |
import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
|
- |
|
50 |
import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
|
40 |
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
|
51 |
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
|
41 |
import org.apache.pdfbox.util.Matrix;
|
52 |
import org.apache.pdfbox.util.Matrix;
|
42 |
import org.jopendocument.model.OpenDocument;
|
53 |
import org.jopendocument.model.OpenDocument;
|
43 |
import org.jopendocument.renderer.ODTRenderer;
|
54 |
import org.jopendocument.renderer.ODTRenderer;
|
44 |
|
55 |
|
Line 137... |
Line 148... |
137 |
});
|
148 |
});
|
138 |
return result;
|
149 |
return result;
|
139 |
}
|
150 |
}
|
140 |
|
151 |
|
141 |
public static void convert2PDF(final OpenDocument doc, final File pdfFileToCreate) throws Exception {
|
152 |
public static void convert2PDF(final OpenDocument doc, final File pdfFileToCreate) throws Exception {
|
- |
|
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 {
|
142 |
assert (!SwingUtilities.isEventDispatchThread());
|
157 |
assert (!SwingUtilities.isEventDispatchThread());
|
- |
|
158 |
System.out.println("SheetUtils convert2PDF " + doc.getLoadedFile().getAbsolutePath() + " -> " + pdfFileToCreate.getAbsolutePath());
|
143 |
try {
|
159 |
try {
|
144 |
PDDocument document = new PDDocument();
|
160 |
PDDocument document = new PDDocument();
|
145 |
PDDocumentInformation info = new PDDocumentInformation();
|
161 |
PDDocumentInformation info = new PDDocumentInformation();
|
146 |
info.setCreator("OpenConcerto");
|
162 |
info.setCreator("OpenConcerto");
|
147 |
info.setProducer("OpenConcerto");
|
163 |
info.setProducer("OpenConcerto");
|
148 |
info.setCreationDate(Calendar.getInstance());
|
164 |
info.setCreationDate(Calendar.getInstance());
|
149 |
info.setModificationDate(Calendar.getInstance());
|
165 |
info.setModificationDate(Calendar.getInstance());
|
150 |
document.setDocumentInformation(info);
|
166 |
document.setDocumentInformation(info);
|
151 |
|
167 |
|
- |
|
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());
|
152 |
FileOutputStream fileOutputStream = new FileOutputStream(pdfFileToCreate);
|
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 |
}
|
- |
|
184 |
|
- |
|
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);
|
153 |
|
190 |
|
154 |
PdfBoxGraphics2DFontTextDrawer fontTextDrawer = new PdfBoxGraphics2DFontTextDrawerDefaultFonts();
|
191 |
PdfBoxGraphics2DFontTextDrawer fontTextDrawer = new PdfBoxGraphics2DFontTextDrawerDefaultFonts();
|
155 |
final File dir = new File("Fonts");
|
192 |
final File dir = new File("Fonts");
|
156 |
if (dir.exists()) {
|
193 |
if (dir.exists()) {
|
157 |
System.out.println("Using fonts dir : " + dir.getAbsolutePath());
|
194 |
System.out.println("Using fonts dir : " + dir.getAbsolutePath());
|
Line 167... |
Line 204... |
167 |
}
|
204 |
}
|
168 |
|
205 |
|
169 |
// Configure the renderer
|
206 |
// Configure the renderer
|
170 |
ODTRenderer renderer = new ODTRenderer(doc);
|
207 |
ODTRenderer renderer = new ODTRenderer(doc);
|
171 |
renderer.setIgnoreMargins(false);
|
208 |
renderer.setIgnoreMargins(false);
|
- |
|
209 |
renderer.setResizeFactor(100);
|
172 |
renderer.setPaintMaxResolution(true);
|
210 |
renderer.setPaintMaxResolution(true);
|
173 |
PDRectangle pageSize = PDRectangle.A4;
|
211 |
PDRectangle pageSize = PDRectangle.A4;
|
174 |
|
- |
|
175 |
// Scale the renderer to fit width or height
|
212 |
// Scale the renderer to fit width or height
|
176 |
final double widthFactor = renderer.getPrintWidth() / pageSize.getWidth();
|
213 |
final double widthFactor = renderer.getPrintWidth() / pageSize.getWidth();
|
177 |
final double heightFactor = renderer.getPrintHeight() / pageSize.getHeight();
|
214 |
final double heightFactor = renderer.getPrintHeight() / pageSize.getHeight();
|
178 |
renderer.setResizeFactor(Math.max(widthFactor, heightFactor));
|
215 |
final int resizeFactor = (int) Math.ceil(Math.max(widthFactor, heightFactor));
|
179 |
|
- |
|
- |
|
216 |
renderer.setResizeFactor(resizeFactor);
|
180 |
// Print pages
|
217 |
// Print pages
|
181 |
for (int i = 0; i < renderer.getPrintedPagesNumber(); i++) {
|
218 |
for (int i = 0; i < renderer.getPrintedPagesNumber(); i++) {
|
182 |
PDPage page = new PDPage(pageSize);
|
219 |
final PDPage page = new PDPage(pageSize);
|
183 |
document.addPage(page);
|
220 |
document.addPage(page);
|
184 |
PdfBoxGraphics2D g2 = new PdfBoxGraphics2D(document, pageSize.getWidth(), pageSize.getHeight());
|
221 |
PdfBoxGraphics2D g2 = new PdfBoxGraphics2D(document, pageSize.getWidth(), pageSize.getHeight());
|
185 |
g2.setFontTextDrawer(fontTextDrawer);
|
222 |
g2.setFontTextDrawer(fontTextDrawer);
|
186 |
|
223 |
|
187 |
// centrage horizontal, alignement vertical en haut
|
224 |
// centrage horizontal, alignement vertical en haut
|
188 |
g2.translate((PageSize.A4.getWidth() - renderer.getPrintWidthInPixel()) / 2.0, 0);
|
225 |
final double hMargin = (pageSize.getWidth() - renderer.getPageWidthInPixel()) / 2.0;
|
- |
|
226 |
g2.translate(hMargin, 0);
|
189 |
|
227 |
|
190 |
// Render
|
228 |
// Render
|
191 |
renderer.setCurrentPage(i);
|
229 |
renderer.setCurrentPage(i);
|
192 |
renderer.paintComponent(g2);
|
230 |
renderer.paintComponent(g2);
|
193 |
g2.dispose();
|
231 |
g2.dispose();
|
Line 197... |
Line 235... |
197 |
matrix.translate(0, 0);
|
235 |
matrix.translate(0, 0);
|
198 |
final PDPageContentStream contentStream = new PDPageContentStream(document, page);
|
236 |
final PDPageContentStream contentStream = new PDPageContentStream(document, page);
|
199 |
contentStream.transform(matrix);
|
237 |
contentStream.transform(matrix);
|
200 |
contentStream.drawForm(xform);
|
238 |
contentStream.drawForm(xform);
|
201 |
contentStream.close();
|
239 |
contentStream.close();
|
- |
|
240 |
|
202 |
}
|
241 |
}
|
203 |
document.save(fileOutputStream);
|
242 |
document.save(pdfFileToCreate);
|
204 |
// Close the PDF document
|
243 |
// Close the PDF document
|
205 |
document.close();
|
244 |
document.close();
|
206 |
|
245 |
|
207 |
} catch (Exception originalExn) {
|
246 |
} catch (Exception originalExn) {
|
208 |
ExceptionHandler.handle("Impossible de créer le PDF " + pdfFileToCreate.getAbsolutePath(), originalExn);
|
247 |
ExceptionHandler.handle("Impossible de créer le PDF " + pdfFileToCreate.getAbsolutePath(), originalExn);
|
Line 224... |
Line 263... |
224 |
int i = name.lastIndexOf(".");
|
263 |
int i = name.lastIndexOf(".");
|
225 |
name = name.substring(0, i) + extension;
|
264 |
name = name.substring(0, i) + extension;
|
226 |
final File f = new File(file.getParent(), name);
|
265 |
final File f = new File(file.getParent(), name);
|
227 |
return f;
|
266 |
return f;
|
228 |
}
|
267 |
}
|
- |
|
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 |
}
|
229 |
}
|
276 |
}
|