public List<Attachment> generateWord(Long caseId) {
try {
ClassPathResource resource = new ClassPathResource("templates/sltzs.docx");
InputStream ins = resource.getInputStream();
// InputStream ins = new FileInputStream(Constant.sltzs); // 受理通知书模板
//注册xdocreport实例并加载FreeMarker模板引擎
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins,
TemplateEngineKind.Freemarker);
//创建xdocreport上下文对象
IContext context = report.createContext();
//创建字段元数据
FieldsMetadata fm = report.createFieldsMetadata();
fm.addFieldAsImage("img");
//获取图片
InputStream img1 = getInputStream("http://192.168.9.4:81/ftproot/zhzc/idCardBack/2023/07/06/1676848681912967169.png");
// InputStream img1 = this.getClass().getResourceAsStream("http://192.168.9.4:81/ftproot/zhzc/idCardBack/2023/07/06/1676848681912967169.png");
//结果集
//创建要替换的文本变量
context.put("number", "咸劳人仲案字〔2023〕1号");
context.put("slyear", "2023");
context.put("slmonth", "07");
context.put("slday", "05");
context.put("img", img1);
//输出到本地目录 临时保存
File file = new File("E:\\bb.docx");
FileOutputStream out = new FileOutputStream(file);
report.process(context, out);
// createPdf(report,out);
//实例化Document类的对象
Document doc = new Document();
//加载Word
doc.loadFromFile("E:\\bb.docx");
//保存为PDF格式
doc.saveToFile("E:\\cc.pdf", FileFormat.PDF);
// FileInputStream fileInputStream = new FileInputStream(file);
// MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
// ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
// List<MultipartFile> multipartFiles = new ArrayList<>();
// multipartFiles.add(multipartFile);
// // 上传ftp
// List<Attachment> attachments = attachmentService.upload(multipartFiles, ftpProperties.getSltzs(), caseId, null);
// 删除临时文件
// file.delete();
return null;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static InputStream getInputStream(String urlPath) {
InputStream inputStream = null;
HttpURLConnection httpURLConnection = null;
try {
URL url = new URL(urlPath);
httpURLConnection = (HttpURLConnection) url.openConnection();
// 设置网络连接超时时间
httpURLConnection.setConnectTimeout(3000);
// 设置应用程序要从网络连接读取数据
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");
int responseCode = httpURLConnection.getResponseCode();
System.out.println("responseCode is:" + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
// 从服务器返回一个输入流
inputStream = httpURLConnection.getInputStream();
} else {
inputStream = httpURLConnection.getErrorStream();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return inputStream;
}
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>docx</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls</artifactId>
<version>2.6.0</version>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.core</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.document</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.template</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.document.docx</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.6</version>
</dependency>
spire.doc.free-5.2.0.jar