一、环境及工具:
环境:Jfinal4.9
工具:spire.pdf
spire.pdf maven坐标:
<dependency> <groupId> e-iceblue </groupId> <artifactId>spire.pdf</artifactId> <version>3.11.6</version> </dependency>
<!-- PDF转换工具 仓库 --> <repository> <id>com.e-iceblue</id> <url>http://repo.e-iceblue.cn/repository/maven-public/</url> </repository> <!-- PDF转换工具 仓库 -->
spire.pdf帮助文档链接
https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html
二、控制层
public class PdfToolsController extends Controller { @ActionKey("/pdf/testPdfToWord") public void testPdfToWord() throws InterruptedException { UploadFile file = getFile(); String downLoadPath=PathKit.getWebRootPath()+"/download/change.docx"; CompletableFuture<File> fileCompletableFuture = CompletableFuture.supplyAsync(() -> { Optional<File> target_file = PdfTask(file.getFile(),downLoadPath); return target_file.get(); }); mainTask(); File download_file=null; try { download_file = fileCompletableFuture.get(100, TimeUnit.SECONDS); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } renderFile(download_file); } private void mainTask() throws InterruptedException { List<Record> records = Db.find(" select * from org_user"); System.out.println(records); Thread.sleep(1000); } public Optional<File> PdfTask(File file, String target_name){ FileInputStream fio = null; Optional<File> target_file=Optional.empty(); try (FileInputStream fio=new FileInputStream(file)){ PdfDocument pdf = new PdfDocument(); pdf.loadFromStream(fio); pdf.saveToFile(target_name, FileFormat.DOCX); target_file=Optional.ofNullable(new File(target_name)); } catch (FileNotFoundException e) { e.printStackTrace(); }finally { return target_file; } } }
三、路由配置
public void configRoute(Routes routes) { //配置路由 routes.add(new AdminRoutes()); }
public class AdminRoutes extends Routes { @Override public void config() { setBaseViewPath("/admin"); add("/", PdfToolsController.class,""); } }
四、启动项目,并用PostMan测试接口。
至此,转换成功