java 使用 aspose 将DOC,DOCX,OOXML,RTF HTML, OpenDocument,PDF,EPUB,XPS, SWF,MD,EXCEL转为pdf
1、下载 aspose-words-21.11 aspose-cells-21.11 两个jar文件,这两个jar文件是收费的,直接使用导出文档会存在水印无法去除,需要进一步处理(已经处理去除水印jar文件点击下载)
2、将下载jar文件自动引入maven,或直接引入到项目中
3、转换工具 代码
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.HtmlSaveOptions;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.aspose.words.SaveOptions;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
/**
* 通用文档转换pdf工具
*
* @author Yang douya
* @date 2023/4/13 16:00
*/
public class ExportPdfUtil {
private static License license = null;
/**
* 设置授权
private static Boolean getLicense() {
InputStream licenseStream = new ByteArrayInputStream("".getBytes());
License license = new License();
try {
license.setLicense(licenseStream);
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
licenseStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return false;
}**/
/**
* 获取 license 去除水印
* 若不验证则转化出的pdf文档会有水印产生
*/
private static void initLicense() {
// 直接放入代码 String licenseFilePath = "excel-license.xml";
if (null != license) {
// 已经授权
return;
}
try {
StringBuffer buffer = new StringBuffer();
buffer.append("<License>");
buffer.append(" <Data>");
buffer.append(" <Products>");
buffer.append(" <Product>Aspose.Total for Java</Product>");
buffer.append(" <Product>Aspose.Words for Java</Product>");
buffer.append(" </Products>");
buffer.append(" <EditionType>Enterprise</EditionType>");
buffer.append(" <SubscriptionExpiry>20991231</SubscriptionExpiry>");
buffer.append(" <LicenseExpiry>20991231</LicenseExpiry>");
buffer.append(" <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>");
buffer.append(" </Data>");
buffer.append(" <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>");
buffer.append("</License>");
ByteArrayInputStream byteArrayInputStream = null;
byteArrayInputStream = new ByteArrayInputStream(buffer.toString().getBytes());
license = new License();
license.setLicense(byteArrayInputStream);
byteArrayInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* word转pdf静态方法
* <p>
* DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,EPUB, XPS, SWF, MD
*
* @param wordPath word文件全路径含文件名
* @param pdfPath pdf输出全路径含文件名
* @return boolean 成功/失败
*/
public static boolean word2Pdf(String wordPath, String pdfPath) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
initLicense();
FileOutputStream os = null;
try {
// long old = System.currentTimeMillis();
// 新建一个空白pdf文档
File file = new File(pdfPath);
os = new FileOutputStream(file);
// inPath是将要被转化的word文档
Document doc = new Document(wordPath);
// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,EPUB, XPS, SWF, MD 相互转换
doc.save(os, SaveFormat.PDF);
long now = System.currentTimeMillis();
// 转化用时
// System.out.println("file转换pdf成功,共耗时:" + ((now - old) / 1000.0) + "秒");
} catch (Exception e) {
return false;
} finally {
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
/***********excel******************/
/**
* excel 转 pdf
*
* @param excelFilePath excel文件路径
*/
public static boolean excel2pdf(String excelFilePath) {
return excel2pdf(excelFilePath, null, null);
}
/**
* excel 转 pdf
*
* @param excelFilePath excel文件路径
* @param convertSheets 需要转换的sheet
*/
public static void excel2pdf(String excelFilePath, int[] convertSheets) {
excel2pdf(excelFilePath, null, convertSheets);
}
/**
* excel 转 pdf
*
* @param excelFilePath excel文件路径
* @param pdfFilePath pdf文件路径
*/
public static void excel2pdf(String excelFilePath, String pdfFilePath) {
excel2pdf(excelFilePath, pdfFilePath, null);
}
/**
* excel 转 pdf
*
* @param excelFilePath excel文件路径
* @param pdfFilePath pdf文件路径
* @param convertSheets 需要转换的sheet
*/
public static boolean excel2pdf(String excelFilePath, String pdfFilePath, int[] convertSheets) {
boolean success = true;
try {
pdfFilePath = pdfFilePath == null ? getPdfFilePath(excelFilePath) : pdfFilePath;
// 验证 License
initLicense();
Workbook wb = new Workbook(excelFilePath);
FileOutputStream fileOS = new FileOutputStream(pdfFilePath);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
if (null != convertSheets) {
printSheetPage(wb, convertSheets);
}
wb.save(fileOS, pdfSaveOptions);
fileOS.flush();
fileOS.close();
// System.out.println("convert success");
} catch (Exception e) {
// System.out.println("convert failed");
e.printStackTrace();
success = false;
}
return success;
}
/**
* 获取 生成的 pdf 文件路径,默认与源文件同一目录
*
* @param excelFilePath excel文件
* @return 生成的 pdf 文件
*/
private static String getPdfFilePath(String excelFilePath) {
return excelFilePath + ".pdf";
}
/**
* 隐藏workbook中不需要的sheet页。
*
* @param sheets 显示页的sheet数组
*/
private static void printSheetPage(Workbook wb, int[] sheets) {
for (int i = 1; i < wb.getWorksheets().getCount(); i++) {
wb.getWorksheets().get(i).setVisible(false);
}
if (null == sheets || sheets.length == 0) {
wb.getWorksheets().get(0).setVisible(true);
} else {
for (int i = 0; i < sheets.length; i++) {
wb.getWorksheets().get(i).setVisible(true);
}
}
}
/******************html*************************/
/**
* 将word的内容转为html返回字符串,图片全部转为base64编码。
*
* @param in
* @return
*/
public static String wordToHtml(InputStream in) {
initLicense();
ByteArrayOutputStream htmlStream = new ByteArrayOutputStream();
String htmlText = "";
try {
Document doc = new Document(in);
HtmlSaveOptions opts = new HtmlSaveOptions(SaveFormat.HTML);
opts.setExportXhtmlTransitional(true);
opts.setExportImagesAsBase64(true);
opts.setExportPageSetup(true);
doc.save(htmlStream, opts);
htmlText = new String(htmlStream.toByteArray(), StandardCharsets.UTF_8);
htmlStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return htmlText;
}
/**
* html内容转word
*
* @param html html内容
* @param wordPath word保存路径
* @return
*/
public static boolean htmlToWord(String html, String wordPath) {
initLicense();
try {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertHtml(html);
//生成doc文件
doc.save(wordPath, SaveOptions.createSaveOptions(SaveFormat.DOC));
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}