-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
core-demo/src/main/java/com/tomshidi/demo/controller/FileConvertController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
core-demo/src/main/java/com/tomshidi/demo/utils/WordToPDFConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.