P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package foundation.io;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.spire.doc.FileFormat;
import com.spire.doc.PictureWatermark;
 
import foundation.token.IUser;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.freemarker.internal.XDocFreemarkerContext;
 
public class FileInjector {
    
    protected static Logger logger;
    private static int Default_WaterMark_Scaling = 140;
    private static License aposeLic = new License();
    
    static {
        logger = LogManager.getLogger(FileInjector.class);
    }
 
    public static List<File> createFiles(IUser user, List<String> templateNames, FileTypes type, Map<String, String> data) throws Exception {
        List<File> result = new ArrayList<File>();
        
        for (String templateName: templateNames) {
            File file = createOneFile(user, templateName, type, data);
            result.add(file);
        }
 
        return result;
    }
    
    public static File createOneFile(IUser user, File template, String targetName, FileTypes type, Map<String, String> data) throws Exception {
        //1. 创建下载临时文件
        File result = FileRepository.createInjectTempFile(user, targetName);
        
        //2. 注入数据
        result = createFile(template, result, type, data);
        
        //3. result
        return result;
    }
    
    public static File createOneFile(IUser user, String templateName, FileTypes type, Map<String, String> data) throws Exception {
        //1. 根据模板名称获取模板
        File template = FileRepository.getTemplateFile(templateName);
        
        //2. 创建下载临时文件
        File result = FileRepository.createInjectTempFile(user, templateName);
        
        //3. 注入数据
        result = createFile(template, result, type, data);
        
        //4.
        return result;
    }
    
    public static File createFile(File template, File target, FileTypes targetType, Map<String, String> data) throws Exception {
        return createFile(template, target, targetType, null, 0, data);
    }
    
    public static File createFile(File template, File target, FileTypes targetType, File waterMark, int waterMarkScaling, Map<String, String> data) throws Exception {
        if (template == null || !template.exists()) {
            logger.error("模板文件不存在:{}", template);
            return null;
        }
        
        long begin = System.currentTimeMillis();
        
        try {
            if (FileTypes.Word == targetType) {
                return createWord(template, target, waterMark, waterMarkScaling, data);
            }
            else if (FileTypes.PDF == targetType) {
                createWord(template, target, waterMark, waterMarkScaling, data);
                
                File pdfFile = translateFileName(target, FileTypes.PDF);
                if (!pdfFile.exists()) {
                    pdfFile.createNewFile();
                }
                
                return createPDF(target, pdfFile);
            }
            else if (FileTypes.XML == targetType) {
                return null;
            }
            else {
                return null;
            }
        }
        finally {
            long end = System.currentTimeMillis();
            System.out.println("pdf转换成功,共耗时:" + ((end - begin) / 1000.0) + "秒");
        }
    }
    
    public static File createWord(File template, File target, File waterMark, int waterMarkScaling, Map<String, String> data) throws Exception {
        if (template == null) {
            return null;
        }
        
        if (!target.exists()) {
            target.createNewFile();
        }
        
        FileInputStream inputStream = new FileInputStream(template);
        try {
            IXDocReport docReport = XDocReportRegistry.getRegistry().loadReport(inputStream, TemplateEngineKind.Freemarker);
            IContext context = docReport.createContext();
            ((XDocFreemarkerContext)context).setDataMap(data);
            
            //1. 注入数据,生成Word
            OutputStream outStream = new FileOutputStream(target);
            try {
                docReport.process(context, outStream);
                outStream.flush();
            }
            finally {
                try {
                    outStream.close();
                }
                catch (Exception e) {
                }
            }
            
            if (waterMark == null) {
                return target;
            }
            
            //2. 注入数据,生成Word
            com.spire.doc.Document document = new com.spire.doc.Document();
            InputStream docStream = new FileInputStream(target);
            try {
                document.loadFromStream(docStream, FileFormat.Docx);
                 
                PictureWatermark picture = new PictureWatermark();
                picture.setPicture(waterMark.getAbsolutePath());
                picture.setScaling(Default_WaterMark_Scaling);
                picture.isWashout(false);
                  
                document.setWatermark(picture);
                document.saveToFile(target.getAbsolutePath(), FileFormat.Docx);
                
                picture.close();
                return target;
            }
            finally {
                try {
                    docStream.close();
                }
                catch (Exception e) {
                }
                
                if (document != null) {
                    try {
                        document.close();
                    }
                    catch (Exception e) {
                    }
                }
            }            
        }
        finally {
            try {
                inputStream.close();
            }
            catch (Exception e) {
            }
        }
    }
    
    public static File createPDF(File sourceWordFile, File target) throws Exception {
        if (!setLicense()) {
               System.out.println("没有导出");
        }
        
        FileOutputStream outStream = new FileOutputStream(target);
        try {
            Document doc = new Document(sourceWordFile.getAbsolutePath());
            doc.save(outStream, SaveFormat.PDF);
            
            return target;
        }
        finally {
            try {
                outStream.flush();
            }
            catch (Exception e) {
            }
            
            try {
                outStream.close();
            }
            catch (Exception e) {
            }
        }
    }
    
    private static File translateFileName(File target, FileTypes word) {
        if (target == null) {
            return null;
        }
        
        String path = target.getAbsolutePath();
        path = path.replace("\\", "/");
        
        int pos_dot = path.lastIndexOf(".");
        
        if (pos_dot <= 0) {
            return null;
        }
        
        path = path.substring(0, pos_dot) + ".pdf";
        return new File(path);
    }
 
    public static boolean setLicense() {
        if (aposeLic.getIsLicensed()) {
            return aposeLic.getIsLicensed();
        }
        
        try {
            InputStream inputStream = new FileInputStream(new File("D:\\repository\\template\\license.xml"));
            aposeLic.setLicense(inputStream);
            
            return aposeLic.getIsLicensed();
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public static void init() {
        try{
            Class<?> aClass = Class.forName("com.aspose.words.zzXyu");
            java.lang.reflect.Field zzZXG = aClass.getDeclaredField("zzZXG");
            zzZXG.setAccessible(true);
 
            java.lang.reflect.Field modifiersField = zzZXG.getClass().getDeclaredField("modifiers");
            modifiersField.setAccessible(true);
            modifiersField.setInt(zzZXG, zzZXG.getModifiers() & ~Modifier.FINAL);
            zzZXG.set(null,new byte[]{76, 73, 67, 69, 78, 83, 69, 68});
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    
}