Skip to content

Commit

Permalink
feat: 增加docx转pdf功能
Browse files Browse the repository at this point in the history
  • Loading branch information
TomShiDi committed Jan 19, 2024
1 parent 8e9fafc commit 8e71ddf
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 0 deletions.
16 changes: 16 additions & 0 deletions core-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@
<artifactId>commons-compress</artifactId>
<version>1.21</version>
</dependency>
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.12.0</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.converter.docx.xwpf</artifactId>
<version>2.0.4</version>
<exclusions>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.tomshidi.demo.controller;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.tomshidi.demo.utils.WordToPDFConverter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
* @author tangshili
* @since 2024/1/19 9:29
*/
@RestController
@RequestMapping("/convert")
public class FileConvertController {

@GetMapping("/pdf")
public String word2Pdf() throws IOException {
LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
Configure config = Configure.builder()
.bind("applicationDatas", policy).build();
XWPFTemplate template = XWPFTemplate.compile("/applicationForm.docx", config)
.render("ssssss");
template.writeAndClose(Files.newOutputStream(Paths.get("/测试模板导出.docx")));
File wordFile = new File("/测试模板导出.docx");
File pdfFile = WordToPDFConverter.word2pdf(wordFile);
return pdfFile.getPath();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.tomshidi.demo.utils;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
* @author tangshili
* @since 2023/12/11 13:59
*/
public class WordToPDFConverter {

private static final Logger LOGGER = LoggerFactory.getLogger(WordToPDFConverter.class);

public static File word2pdf(File wordFile) throws IOException {
FileInputStream inputStream = new FileInputStream(wordFile);
XWPFDocument document = new XWPFDocument(inputStream);
PdfOptions options = PdfOptions.create();
options.fontEncoding("Identity-H");
LOGGER.info("使用编码为:{}", options.getFontEncoding());
String wordFilePath = wordFile.getPath();
String pdfFilePath = wordFilePath.substring(0, wordFilePath.lastIndexOf(".")) + ".pdf";
File pdfFile = new File(pdfFilePath);
FileOutputStream outputStream = new FileOutputStream(pdfFile);
PdfConverter.getInstance().convert(document, outputStream, options);
return pdfFile;
}

public static void main(String[] args) throws IOException {
LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
Configure config = Configure.builder()
.bind("applicationDatas", policy).build();
XWPFTemplate template = XWPFTemplate.compile("E:\\applicationForm.docx", config)
.render("ssssss");
template.writeAndClose(Files.newOutputStream(Paths.get("E:\\测试模板导出.docx")));
File wordFile = new File("E:\\测试模板导出.docx");
File pdfFile = word2pdf(wordFile);
}
}
Binary file added core-demo/src/main/resources/fonts/simfang.ttf
Binary file not shown.
Binary file added core-demo/src/main/resources/fonts/simhei.ttf
Binary file not shown.
Binary file added core-demo/src/main/resources/fonts/simkai.ttf
Binary file not shown.
Binary file added core-demo/src/main/resources/fonts/simsun.ttc
Binary file not shown.
Binary file added core-demo/src/main/resources/fonts/simsunb.ttf
Binary file not shown.

0 comments on commit 8e71ddf

Please sign in to comment.