Word 、Excel 转 PDF
word 转 pdf
引入 jar 依赖
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>19.2</version>
</dependency>
工具类方法
import com.aspose.words.SaveFormat;
import java.io.File;
import java.io.FileOutputStream;
/**
* word 转 pdf
*/
public class Word2Pdf {
/**
* word转pdf
* inpath: 输入word的路径
* outpath: 输出pdf的路径
* fileName: 输出pdf的名称
*/
public static String wordTOPdf(String inPath, String outPath,String fileName) throws Exception {
if(!new File(outPath).exists()){
new File(outPath).mkdirs();
}
// pdf 路径
String pdfPath = outPath + "\\" + fileName + ".pdf";
File file = new File(pdfPath);
// 生成pdf os
FileOutputStream os = new FileOutputStream(file);
//解决乱码
//如果是windows执行,不需要加这个
//TODO 如果是linux执行,需要添加这个*****
//FontSettings.setFontsFolder("/usr/share/fonts",true);
com.aspose.words.Document doc = new com.aspose.words.Document(inPath);
//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
doc.save(os, SaveFormat.PDF);
// long now = System.currentTimeMillis();
// System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
return pdfPath;
}
例子:
public class Test {
public static void main(String[] args) {
String word_path = "F:\\test_path\\test\\wordPath.docx";
// 暂定pdf地址 "F:\\test_path\\test\\pdfPath.docx";
String pdf_path = "F:\\test_path\\test";
try {
String outPath = Word2Pdf.wordTOPdf(word_path,pdf_path,"pdfPath");
System.out.println(outPath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
excel 转 pdf
引入相关jar包依赖
<!--jacob本地配置(excel转pdf)st-->
<!-- https://mvnrepository.com/artifact/com.hynnet/jacob -->
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>jacob</artifactId>
<version>1.18</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-spring-boot-starter</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>4.2.2</version>
</dependency>
<!--jacob本地配置(excel转pdf)et-->
工具类方法
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jodconverter.DocumentConverter;
import org.jodconverter.office.OfficeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;
@Service
@Slf4j
public class OpenOfficeToPdfService {
/**
* 转换器注入
*/
@Autowired
private DocumentConverter converter;
//excel转换-需要进行样式调整
public boolean convertExcelToPDFByFitColumn(File sourceFile,File pdfFile) {
String uuid = UUID.randomUUID().toString();
File tempExcel = new File(sourceFile.getParentFile(), uuid.concat("_").concat(sourceFile.getName()));
try {
setExcelPrintParameter(sourceFile, tempExcel);
return this.convert(tempExcel, pdfFile);
} catch (IOException | InvalidFormatException e) {
e.printStackTrace();
System.out.println("转换office文档失败");
} finally {
if (tempExcel.exists() && !tempExcel.delete())
System.out.println("删除临时文件失败 file={}"+ tempExcel.getPath());
}
return false;
}
private boolean convert(File sourceFile, File targetFile) {
try {
converter.convert(sourceFile).to(targetFile).execute();
return true;
} catch (OfficeException e) {
e.printStackTrace();
System.out.println("转换office文档失败");
}
return false;
}
/**
* 设置Excel打印参数
*
* @param sourceFile
* @param targetFile
* @throws IOException
* @throws InvalidFormatException
*/
private void setExcelPrintParameter(File sourceFile, File targetFile) throws IOException, InvalidFormatException {
Workbook workbook = new XSSFWorkbook(sourceFile);
for (int i = 0, j = workbook.getNumberOfSheets(); i < j; i++) {
Sheet sheet = workbook.getSheetAt(i);
sheet.getPrintSetup().setPaperSize(PrintSetup.A4_PAPERSIZE);
// FitHeight=1, 将所有行都缩放显示在一页上(设置1表示一页显示完,如果设置2表示分2页显示完)
// FitWidth=1, 将所有列都缩放显示在一页上
// 两个都等于1时,如果行数太多则会挤压列,一般来说只设置一个FitWidth=1,让行数自动换页
// 要使这两个参数有效,则需要设置FitToPage=true
sheet.setFitToPage(true);
sheet.getPrintSetup().setFitWidth((short) 1);
// sheet.getPrintSetup().setFitHeight((short)1);
// 是否显示自动换页符
sheet.setAutobreaks(true);
}
try (FileOutputStream out = new FileOutputStream(targetFile)) {
workbook.write(out);
workbook.close();
}
}
}
实列代码