完美的word转pdf
程序员文章站
2024-01-21 19:06:40
...
话不多说,首先导入依赖包
这篇文章是转载的别人的文章 仅用于做本地保存
原文链接
<!-- documents4j word转pdf -->
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-local</artifactId>
<version>1.1.5</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.1.5</version>
</dependency>
util工具类
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import java.io.*;
public class PdfUtil {
/**
* word转pdf
* @param wordFilePath word文件路径
* @param pdfFilePath pdf文件路径
* @return 成功或失败
*/
public static boolean docxToPdf(String wordFilePath, String pdfFilePath) {
boolean result = false;
File inputFile = new File(wordFilePath);
File outputFile = new File(pdfFilePath);
try {
InputStream inputStream = new FileInputStream(inputFile);
OutputStream outputStream = new FileOutputStream(outputFile);
IConverter converter = LocalConverter.builder().build();
converter.convert(inputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
outputStream.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
控制器层做测试,支持doc docx
// http://localhost:8080/convert/word2pdf
@RequestMapping("/word2pdf")
public void word2pdf(HttpServletResponse response) {
// String wordPath = "F:\\gxg\\ftpDownload\\test.docx";
// String pdfPath = "F:\\gxg\\ftpDownload\\test.pdf";
String wordPath = "F:\\gxg\\ftpDownload\\需求分析报告.xiexiaochuan.doc";
String pdfPath = "F:\\gxg\\ftpDownload\\需求分析报告.pdf";
Convert2Pdf.doc2Pdf(wordPath,pdfPath);
System.out.println("转换成功");
File file = new File(pdfPath);
ResponseUtil.onlineReader(file,response);
System.out.println("执行完毕");
}
在线预览,附件下载 工具包
在这里插入代码片package com.example.house.SYSDBA.util;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
public class ResponseUtil {
public static void responseBrowser(File file, HttpServletResponse response){
//下面的是进行响应客户端的测试代码
try {
FileInputStream fileInputStream = new FileInputStream(file);
//设置Http响应头告诉浏览器下载这个附件,下载的文件名也是在这里设置的
System.out.println("file.getName() >>>"+file.getName());
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(file.getName(), "UTF-8"));
// response.setHeader("Content-Disposition", "inline;fileName=" + URLEncoder.encode(file.getName(), "UTF-8"));
System.out.println("file.getPath() >>>>>>"+file.getPath());
OutputStream outputStream = null;
outputStream = response.getOutputStream();
byte[] bytes = new byte[2048];
int len = 0;
while ((len = fileInputStream.read(bytes))>0){
outputStream.write(bytes,0,len);
}
fileInputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件响应出错,读写出错");
}
}
public static void onlineReader(File file,HttpServletResponse response){
if (file.exists()){
byte[] data = null;
try {
response.setHeader("Content-Disposition", "inline;fileName=" + URLEncoder.encode(file.getName(), "UTF-8"));
FileInputStream input = new FileInputStream(file);
data = new byte[input.available()];
input.read(data);
response.getOutputStream().write(data);
input.close();
} catch (Exception e) {
System.out.println(e);
}
}else{
System.out.println("文件不存在");
}
}
}
上一篇: openssl函数在https下无法使用
下一篇: PHP:6种GET和POST请求发送方法