需求 :把docx文档转pdf文件
这里需要注意的一点就是word转pdf,在linux系统转换的时候,有可能因为linux下没有某些字体,导致转换后pdf中文出现小方框,解决办法就是把window下的C盘字体上传到linux下。
还有一个问题是转换后的pdf字体有些加粗,有些正常显示,这个问题也是因为linux没有某些字体导致的。
我的项目是springboot项目:
一、在项目resources里增加license.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<License>
<Data>
<Products>
<Product>Aspose.Total for Java</Product>
<Product>Aspose.Words for Java</Product>
</Products>
<EditionType>Enterprise</EditionType>
<SubscriptionExpiry>20991231</SubscriptionExpiry>
<LicenseExpiry>20991231</LicenseExpiry>
<SerialNumber>23dcc79f-44ec-4a23-be3a-03c1632404e9</SerialNumber>
</Data>
<Signature>0nRuwNEddXwLfXB7pw66G71MS93gW8mNzJ7vuh3Sf4VAEOBfpxtHLCotymv1PoeukxYe31K441Ivq0Pkvx1yZZG4O1KCv3Omdbs7uqzUB4xXHlOub4VsTODzDJ5MWHqlRCB1HHcGjlyT2sVGiovLt0Grvqw5+QXBuinoBY0suX0=</Signature>
</License>
二、doc转pdf类:
import java.io.*;
import com.aspose.words.*;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class WordToPdfUtil {
private static final Logger logger = LoggerFactory.getLogger(WordToPdfUtil.class);
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = WordToPdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
logger.error("error:", e);
}
return result;
}
public static boolean docTopdfByOutputStream(InputStream is, ByteArrayOutputStream byteArrayOutputStream) throws Exception {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return false;
}
long old = System.currentTimeMillis();
if (System.getProperties().getProperty("os.name").toUpperCase().indexOf("WINDOWS") == -1) {
FontSettings.setFontsFolder("/usr/share/fonts" + File.separator, true);//此处处理乱码和小方块
}
Document doc = new Document(is); //Address是将要被转化的word文档
SectionCollection sections = doc.getSections();
Section[] array_section = sections.toArray();
for (int i = 0; i < array_section.length; i++) {
Section sec = array_section[i];
PageSetup ps = sec.getPageSetup();
ps.setPaperSize(PaperSize.A4);
ps.setOrientation(Orientation.PORTRAIT);
ps.setTopMargin(ConvertUtil.inchToPoint(1.0));
ps.setBottomMargin(ConvertUtil.inchToPoint(1.0));
}
doc.save(byteArrayOutputStream, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
long now = System.currentTimeMillis();
logger.info("共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时
return true;
}
public static boolean docTopdfByOutputStream(ByteArrayInputStream byteArrayInputStream, ByteArrayOutputStream byteArrayOutputStream) throws Exception {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return false;
}
long old = System.currentTimeMillis();
if (System.getProperties().getProperty("os.name").toUpperCase().indexOf("WINDOWS") == -1) {
FontSettings.setFontsFolder("/usr/share/fonts" + File.separator, true);//此处处理乱码和小方块
}
Document doc = new Document(byteArrayInputStream); //Address是将要被转化的word文档
SectionCollection sections = doc.getSections();
Section[] array_section = sections.toArray();
for (int i = 0; i < array_section.length; i++) {
Section sec = array_section[i];
PageSetup ps = sec.getPageSetup();
ps.setPaperSize(PaperSize.A4);
ps.setOrientation(Orientation.PORTRAIT);
ps.setTopMargin(ConvertUtil.inchToPoint(1.0));
ps.setBottomMargin(ConvertUtil.inchToPoint(1.0));
}
doc.save(byteArrayOutputStream, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
long now = System.currentTimeMillis();
logger.info("共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时
return true;
}
public static boolean docTopdfByInputFile(String inputFile, ByteArrayOutputStream byteArrayOutputStream) throws Exception {
byte[] inbyte = FileUtils.readFileToByteArray(new File(inputFile));
ByteArrayInputStream bis = new ByteArrayInputStream(inbyte);
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return false;
}
if (System.getProperties().getProperty("os.name").toUpperCase().indexOf("WINDOWS") == -1) {
FontSettings.setFontsFolder("/usr/share/fonts" + File.separator, true);//此处处理乱码和小方块
}
Document doc = new Document(bis);
doc.save(byteArrayOutputStream, SaveFormat.PDF);
return true;
}
public static String docTopdfByOutFile(String inputFile,String outputFile) throws Exception {
byte[] inbyte = FileUtils.readFileToByteArray(new File(inputFile));
ByteArrayInputStream bis = new ByteArrayInputStream(inbyte);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return "false";
}
if (System.getProperties().getProperty("os.name").toUpperCase().indexOf("WINDOWS") == -1) {
FontSettings.setFontsFolder("/usr/share/fonts" + File.separator, true);//此处处理乱码和小方块
}
Document doc = new Document(bis);
doc.save(bos, SaveFormat.PDF);
byte[] bytes = bos.toByteArray();
FileUtils.writeByteArrayToFile(new File(outputFile), bytes);
bos.close();
bis.close();
return outputFile;
}
public static String docToPdfByOutFile(ByteArrayInputStream byteArrayInputStream,String outputFile) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return "false";
}
if (System.getProperties().getProperty("os.name").toUpperCase().indexOf("WINDOWS") == -1) {
FontSettings.setFontsFolder("/usr/share/fonts" + File.separator, true);//此处处理乱码和小方块
}
Document doc = new Document(byteArrayInputStream);
doc.save(bos, SaveFormat.PDF);
byte[] bytes = bos.toByteArray();
FileUtils.writeByteArrayToFile(new File(outputFile), bytes);
bos.close();
byteArrayInputStream.close();
return outputFile;
}
public static void main(String args[]) throws Exception {
//String Address = "/DEV/znsd/tpl/sdhz.rtf";
String Address = "f:\\test\\gtwj2020-05-14111335.docx";
byte[] inbyte = FileUtils.readFileToByteArray(new File(Address));
ByteArrayInputStream bis = new ByteArrayInputStream(inbyte);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return;
}
Document doc = new Document(bis);
doc.save(bos, SaveFormat.PDF);
byte[] bytes = bos.toByteArray();
//String pdfFile = "/DEV/znsd/new/sdhz.pdf";
String pdfFile = "f:\\test\\gtwj.pdf";
FileUtils.writeByteArrayToFile(new File(pdfFile), bytes);
bos.close();
bis.close();
}
}
三、项目引入的jar包:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8</version>
<classifier>jdk16</classifier>
</dependency>
四、我用到了docTopdfByOutputStream这个方法。