POM文件引入
<!--word转PDF-->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose.words</artifactId>
<version>14.11</version>
<classifier>jdk16</classifier>
</dependency>
<!-- excel 转PDF-->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>18.3</version>
</dependency>
word转pdf
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.UUID;
/**
* @author tqf
* @description word转PDF
* @date 2019-10-29
*/
public class WordToPdfUtil {
private static Logger log = LogManager.getLogger();
/**
* 获取license 主要除去水印
*
* @return
*/
private static boolean getLicense() {
boolean result = false;
try {
// 凭证
String licenseStr =
"<License>\n" +
" <Data>\n" +
" <Products>\n" +
" <Product>Aspose.Total for Java</Product>\n" +
" <Product>Aspose.Words for Java</Product>\n" +
" </Products>\n" +
" <EditionType>Enterprise</EditionType>\n" +
" <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
" <LicenseExpiry>20991231</LicenseExpiry>\n" +
" <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
" </Data>\n" +
" <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
"</License>";
InputStream license = new ByteArrayInputStream(licenseStr.getBytes("UTF-8"));
License asposeLic = new License();
asposeLic.setLicense(license);
result = true;
} catch (Exception e) {
}
return result;
}
public static String convertDocx2Pdf(MultipartFile file, String templatePath, String fileName) {
// 验证License
if (!getLicense()) {
log.error("验证License失败!");
}
FileOutputStream fileOS = null;
String pdfPath = null;
try {
String uuid = UUID.randomUUID().toString();
String str = templatePath+ File.separator + uuid + File.separator + fileName;
File saveFile = new File(str);
FileUtils.copyInputStreamToFile(file.getInputStream(), saveFile);
Document doc = new Document(file.getInputStream());
String pdfName = fileName.substring(0,fileName.lastIndexOf("."));
pdfPath = "/" + uuid + "/" + pdfName + ".pdf";
String pdfAbsPath = templatePath+ File.separator + uuid + File.separator + pdfName + ".pdf";
fileOS = new FileOutputStream(new File(pdfAbsPath));
// 保存转换的pdf文件
doc.save(fileOS, SaveFormat.PDF);
saveFile.delete();
} catch (Exception e){
log.error("error:", e);
} finally {
try {
if(fileOS != null){
fileOS.close();
}
} catch (IOException e) {
log.error("error:", e);
}
}
return pdfPath;
}
}
excel转PDF
import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.PdfSecurityOptions;
import com.aspose.cells.Workbook;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.UUID;
/**
* @version 1.0 说明 :
*/
public class ExcelToPdf {
/**
* 获取license
*
* @return
*/
private static boolean getLicense() {
boolean result = false;
try {
// 凭证
String licenseStr =
"<License>\n" +
" <Data>\n" +
" <Products>\n" +
" <Product>Aspose.Total for Java</Product>\n" +
" <Product>Aspose.Words for Java</Product>\n" +
" </Products>\n" +
" <EditionType>Enterprise</EditionType>\n" +
" <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
" <LicenseExpiry>20991231</LicenseExpiry>\n" +
" <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
" </Data>\n" +
" <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
"</License>";
InputStream license = new ByteArrayInputStream(licenseStr.getBytes("UTF-8"));
License asposeLic = new License();
asposeLic.setLicense(license);
result = true;
} catch (Exception e) {
}
return result;
}
public static String excel2pdf(MultipartFile file, String templatePath, String fileName) {
String pdfPath = null;
// 验证License
if (!getLicense()) {
System.out.println("验证License失败!");
}
try {
String uuid = UUID.randomUUID().toString();
String str = templatePath+ File.separator + uuid + File.separator + fileName;
File saveFile = new File(str);
FileUtils.copyInputStreamToFile(file.getInputStream(), saveFile);
String pdfName = fileName.substring(0,fileName.lastIndexOf("."));
Workbook wb = new Workbook(saveFile.getPath());// 原始excel路径
//pdf保存选项,此处可以使excel转PDF时过长不会被分隔成多块
PdfSaveOptions xlsSaveOption = new PdfSaveOptions();
PdfSecurityOptions pso = new PdfSecurityOptions();
xlsSaveOption.setSecurityOptions(pso);
xlsSaveOption.getSecurityOptions().setPrintPermission(false);
xlsSaveOption.setAllColumnsInOnePagePerSheet(true);
pdfPath = "/" + uuid + "/" + pdfName + ".pdf";
wb.save(saveFile.getParent() + File.separator + pdfName + ".pdf", xlsSaveOption);
saveFile.delete();
} catch (Exception e) {
e.printStackTrace();
}
return pdfPath;
}
}
注明:excel转pdf除去水印的证书文件没生效,继续找寻中,有生效的希望可以共享一下