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();
|
}
|
}
|
|
}
|