16
16
17
17
import java .awt .image .BufferedImage ;
18
18
import java .io .ByteArrayInputStream ;
19
+ import java .io .File ;
19
20
import java .io .IOException ;
20
21
import java .io .StringWriter ;
22
+ import java .nio .file .FileVisitOption ;
23
+ import java .nio .file .Files ;
21
24
import java .nio .file .Path ;
22
25
import java .nio .file .Paths ;
26
+ import java .util .Comparator ;
23
27
import java .util .List ;
24
28
import java .util .Map ;
29
+ import java .util .stream .Stream ;
25
30
26
31
import javax .xml .parsers .ParserConfigurationException ;
27
32
import javax .xml .transform .Transformer ;
30
35
import javax .xml .transform .stream .StreamResult ;
31
36
import javax .xml .transform .stream .StreamSource ;
32
37
38
+ import org .apache .pdfbox .io .MemoryUsageSetting ;
39
+ import org .apache .pdfbox .multipdf .PDFMergerUtility ;
33
40
import org .apache .poi .UnsupportedFileFormatException ;
34
41
import org .eclipse .set .basis .FreeFieldInfo ;
35
42
import org .eclipse .set .basis .OverwriteHandling ;
63
70
public class SiteplanPdfExportBuilder extends FopPdfExportBuilder {
64
71
65
72
private static final String SITEPLAN_EXPORT_NAME = "SI" ; //$NON-NLS-1$
73
+ private static final String SITEPLAN_TMP_DIR = "siteplanTmp" ; //$NON-NLS-1$
66
74
67
75
/**
68
76
* @param enumTranslationService
@@ -85,13 +93,13 @@ public void setFopService(final FopService fopService) {
85
93
this .fopService = fopService ;
86
94
}
87
95
88
- private static String createImageDocumentText (
89
- final List < BufferedImage > imagesData , final Titlebox titleBox ,
90
- final FreeFieldInfo freeFieldInfo , final double ppm )
96
+ private static String createImageDocumentText (final BufferedImage imageData ,
97
+ final Titlebox titleBox , final FreeFieldInfo freeFieldInfo ,
98
+ final double ppm )
91
99
throws ParserConfigurationException , TransformerException {
92
100
final TableToTableDocument tableToXmlFo = TableToTableDocument
93
101
.createTransformation ();
94
- final Document document = tableToXmlFo .transformToDocument (imagesData ,
102
+ final Document document = tableToXmlFo .transformToDocument (imageData ,
95
103
titleBox , freeFieldInfo , ppm );
96
104
final Transformer documentToString = newTransformerFactory ()
97
105
.newTransformer ();
@@ -132,45 +140,80 @@ public void exportSiteplanPdf(final List<BufferedImage> imagesData,
132
140
final double ppm , final String outputDir ,
133
141
final ToolboxPaths toolboxPaths , final TableType tableType ,
134
142
final OverwriteHandling overwriteHandling ) {
135
-
143
+ final Path exportTmpDir = Path .of (outputDir , SITEPLAN_TMP_DIR );
144
+ if (!exportTmpDir .toFile ().exists ()) {
145
+ exportTmpDir .toFile ().mkdirs ();
146
+ }
147
+ final PDFMergerUtility pdfMergerUtility = new PDFMergerUtility ();
148
+ pdfMergerUtility .setDestinationFileName (toolboxPaths
149
+ .getTableExportPath (SITEPLAN_EXPORT_NAME , Paths .get (outputDir ),
150
+ ExportType .PLANNING_RECORDS , TABLE_PDF_EXPORT_EXTENSION )
151
+ .toString ());
136
152
try {
137
- final String imageDocumentText = createImageDocumentText (imagesData ,
138
- titleBox , freeFieldInfo , ppm );
139
-
140
- if (ToolboxConfiguration .isDebugMode ()) {
141
- exportTableDocument (
142
- Paths .get (outputDir ,
143
- getFilename (SITEPLAN_EXPORT_NAME , "xml" )), //$NON-NLS-1$
144
- imageDocumentText );
153
+ for (int i = 0 ; i < imagesData .size (); i ++) {
154
+ final String imageDocumentText = createImageDocumentText (
155
+ imagesData .get (i ), titleBox , freeFieldInfo , ppm );
156
+ final String siteplanExportName = SITEPLAN_EXPORT_NAME + "_" //$NON-NLS-1$
157
+ + i ;
158
+ if (ToolboxConfiguration .isDebugMode ()) {
159
+ exportTableDocument (
160
+ Paths .get (exportTmpDir .toString (),
161
+ getFilename (siteplanExportName , "xml" )), //$NON-NLS-1$
162
+ imageDocumentText );
163
+ }
164
+ final ByteArrayInputStream tableDocumentStream = new ByteArrayInputStream (
165
+ imageDocumentText .getBytes (UTF_8 ));
166
+ final StreamSource imageDocumentSource = new StreamSource (
167
+ tableDocumentStream );
168
+ final String exportFileName = getFilename (siteplanExportName ,
169
+ "xsl" ); //$NON-NLS-1$
170
+ final String pagePostFix = i == imagesData .size () - 1 ? "-" //$NON-NLS-1$
171
+ : "+" ; //$NON-NLS-1$
172
+ final Pair <String , StreamSource > xslStreamSource = getSiteplanXSLTemplate (
173
+ imagesData .get (i ), ppm , tableType ,
174
+ exportTmpDir .toString (), exportFileName , i + 1 ,
175
+ pagePostFix );
176
+ final Path outputPath = toolboxPaths .getTableExportPath (
177
+ String .format ("%s_%s_%d" , SITEPLAN_EXPORT_NAME , //$NON-NLS-1$
178
+ xslStreamSource .getFirst (), Integer .valueOf (i )),
179
+ exportTmpDir , ExportType .PLANNING_RECORDS ,
180
+ TABLE_PDF_EXPORT_EXTENSION );
181
+ fopService .fop (OutputFormat .PDF , xslStreamSource .getSecond (),
182
+ imageDocumentSource , outputPath , PdfAMode .PDF_A_3a ,
183
+ overwriteHandling , null );
184
+ pdfMergerUtility .addSource (outputPath .toFile ());
145
185
}
146
- final ByteArrayInputStream tableDocumentStream = new ByteArrayInputStream (
147
- imageDocumentText .getBytes (UTF_8 ));
148
- final StreamSource imageDocumentSource = new StreamSource (
149
- tableDocumentStream );
150
- final Pair <String , StreamSource > xslStreamSource = getSiteplanXSLTemplate (
151
- imagesData , ppm , tableType , outputDir );
152
- final Path outputPath = toolboxPaths .getTableExportPath (
153
- SITEPLAN_EXPORT_NAME + "_" + xslStreamSource .getFirst (), //$NON-NLS-1$
154
- Paths .get (outputDir ), ExportType .PLANNING_RECORDS ,
155
- TABLE_PDF_EXPORT_EXTENSION );
156
- fopService .fop (OutputFormat .PDF , xslStreamSource .getSecond (),
157
- imageDocumentSource , outputPath , PdfAMode .PDF_A_3a ,
158
- overwriteHandling , null );
159
-
186
+ pdfMergerUtility
187
+ .mergeDocuments (MemoryUsageSetting .setupMainMemoryOnly ());
160
188
} catch (final Exception e ) {
161
189
throw new FileExportException (
162
190
Path .of (outputDir , SITEPLAN_EXPORT_NAME ), e );
163
191
}
192
+
193
+ if (!ToolboxConfiguration .isDevelopmentMode ()
194
+ && exportTmpDir .toFile ().exists ()) {
195
+ try (Stream <Path > paths = Files .walk (exportTmpDir ,
196
+ FileVisitOption .values ())) {
197
+ paths .sorted (Comparator .reverseOrder ())
198
+ .map (Path ::toFile )
199
+ .forEach (File ::delete );
200
+ } catch (final IOException e ) {
201
+ throw new FileExportException (
202
+ Path .of (outputDir , SITEPLAN_EXPORT_NAME ), e );
203
+ }
204
+ }
205
+
164
206
}
165
207
166
208
private Pair <String , StreamSource > getSiteplanXSLTemplate (
167
- final List <BufferedImage > imagesData , final double ppm ,
168
- final TableType tableType , final String outputDir )
169
- throws ParserConfigurationException , SAXException , IOException ,
170
- NullPointerException , TransformerException ,
171
- UnsupportedFileFormatException {
172
- final SiteplanXSL siteplanXSL = new SiteplanXSL (imagesData , ppm ,
173
- translationTableType (tableType ));
209
+ final BufferedImage imageData , final double ppm ,
210
+ final TableType tableType , final String outputDir ,
211
+ final String exportFileName , final int pagePosition ,
212
+ final String pagePostfix ) throws ParserConfigurationException ,
213
+ SAXException , IOException , NullPointerException ,
214
+ TransformerException , UnsupportedFileFormatException {
215
+ final SiteplanXSL siteplanXSL = new SiteplanXSL (imageData , ppm ,
216
+ translationTableType (tableType ), pagePosition , pagePostfix );
174
217
final Document xslDoc = siteplanXSL .getXSLDocument ();
175
218
final String pageDIN = siteplanXSL .getPageStyle ()
176
219
.getPageDIN ()
@@ -182,13 +225,9 @@ private Pair<String, StreamSource> getSiteplanXSLTemplate(
182
225
final StringWriter writer = new StringWriter ();
183
226
final StreamResult result = new StreamResult (writer );
184
227
documentToString .transform (source , result );
185
- exportTableDocument (
186
- Path .of (outputDir ,
187
- getFilename (SITEPLAN_EXPORT_NAME + "_" + pageDIN , //$NON-NLS-1$
188
- "xsl" )), //$NON-NLS-1$
228
+ exportTableDocument (Path .of (outputDir , exportFileName ),
189
229
writer .toString ());
190
230
}
191
231
return new Pair <>(pageDIN , toStreamSource (xslDoc ));
192
232
}
193
-
194
233
}
0 commit comments