Skip to content
This repository has been archived by the owner on Mar 4, 2022. It is now read-only.

Commit

Permalink
office support
Browse files Browse the repository at this point in the history
  • Loading branch information
itning committed Mar 14, 2019
1 parent a84b577 commit 191323d
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 55 deletions.
34 changes: 29 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>top.yunshu</groupId>
<artifactId>shw_server</artifactId>
<version>1.5.6-RELEASE</version>
<version>1.6.0-RELEASE</version>
<name>shw_server</name>
<description>Student HomeWork Management System</description>

Expand All @@ -21,6 +21,8 @@
<cas.spring.boot.starter.version>1.0.6-RELEASE</cas.spring.boot.starter.version>
<springfox.version>2.9.2</springfox.version>
<jib.maven.plugin.version>1.0.1</jib.maven.plugin.version>
<common.io.version>2.6</common.io.version>
<itning.aspose.version>1.0.0</itning.aspose.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -73,6 +75,11 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down Expand Up @@ -101,12 +108,29 @@
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/fr.opensagres.xdocreport/org.apache.poi.xwpf.converter.pdf -->
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${common.io.version}</version>
</dependency>
<!--这个依赖为本地依赖 开始-->
<dependency>
<groupId>top.itning.aspose</groupId>
<artifactId>cells</artifactId>
<version>${itning.aspose.version}</version>
</dependency>
<dependency>
<groupId>top.itning.aspose</groupId>
<artifactId>slides</artifactId>
<version>${itning.aspose.version}</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.6</version>
<groupId>top.itning.aspose</groupId>
<artifactId>words</artifactId>
<version>${itning.aspose.version}</version>
</dependency>
<!--这个依赖为本地依赖 结束-->

<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public void preview(@ApiParam(value = "学号", required = true) @PathVariable S
try (ServletOutputStream outputStream = response.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(file)) {
String extensionName = file.getName().substring(file.getName().lastIndexOf(".") + 1);
if (Arrays.asList("xls", "xlsx", "doc", "docx").contains(extensionName)) {
if (Arrays.asList("xls", "xlsx", "doc", "docx", "ppt", "pptx").contains(extensionName.toLowerCase())) {
String tempFilePath = configService.getConfig(Config.ConfigKey.TEMP_DIR).orElse(System.getProperty("java.io.tmpdir")) + File.separator + FileUtils.getFileMD5(file) + ".pdf";
File tempFile = new File(tempFilePath);
if (!tempFile.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public void getDataOfZipFile(@ApiParam(value = "学生学号", required = true)
fileService.getFile(studentNumber, workId).ifPresent(file -> {
try (ServletOutputStream outputStream = response.getOutputStream()) {
String extensionName = name.substring(name.lastIndexOf(".") + 1);
if (Arrays.asList("xls", "xlsx", "doc", "docx").contains(extensionName)) {
if (Arrays.asList("xls", "xlsx", "doc", "docx", "ppt", "pptx").contains(extensionName.toLowerCase())) {
String tempFilePath = configService.getConfig(Config.ConfigKey.TEMP_DIR).orElse(System.getProperty("java.io.tmpdir")) + File.separator + FileUtils.getFileMD5(file) + "zip.pdf";
File tempFile = new File(tempFilePath);
if (!tempFile.exists()) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/top/yunshu/shw/server/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.apache.catalina.connector.ClientAbortException;
import org.apache.commons.codec.binary.Hex;
import org.apache.poi.util.IOUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
Expand All @@ -19,7 +19,7 @@
*
* @author itning
*/
public class FileUtils {
public final class FileUtils {
private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);
private static final String RANGE_SEPARATOR = "-";
private static final String RANGE_CONTAINS = "bytes=";
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/top/yunshu/shw/server/util/JwtUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @author itning
*/
public class JwtUtils {
public final class JwtUtils {
private static final String PRIVATE_KEY = "hxcshw";
private static final String LOGIN_USER = "loginUser";
private static final String DEFAULT_STR = "null";
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/top/yunshu/shw/server/util/Office2PdfLicense.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package top.yunshu.shw.server.util;

import java.io.FileInputStream;
import java.io.InputStream;


/**
* @author itning
*/
final class Office2PdfLicense {
private static final String LICENSE_PATH = Office2PdfLicense.class.getResource("/license.xml").getPath().substring(1);

/**
* 获取Word的license签字验证
*/
static boolean getWordLicense() {
boolean result = false;
try (InputStream is = new FileInputStream(LICENSE_PATH)) {
com.aspose.words.License aposeLic = new com.aspose.words.License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

/**
* 获取Excel的license签字验证
*/
static boolean getExcelLicense() {
boolean result = false;
try (InputStream is = new FileInputStream(LICENSE_PATH)) {
com.aspose.cells.License aposeLic = new com.aspose.cells.License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

/**
* 获取PPT的license
*/
static boolean getPPTLicense() {
boolean result = false;
try (InputStream is = new FileInputStream(LICENSE_PATH)) {
com.aspose.slides.License aposeLic = new com.aspose.slides.License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
95 changes: 55 additions & 40 deletions src/main/java/top/yunshu/shw/server/util/Office2PdfUtils.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
package top.yunshu.shw.server.util;

import com.lowagie.text.Font;
import com.lowagie.text.pdf.BaseFont;
import fr.opensagres.xdocreport.itext.extension.font.ITextFontRegistry;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.io.*;

/**
* Office to Pdf Utils
*
* @author itning
*/
public class Office2PdfUtils {
public final class Office2PdfUtils {
private static final Logger logger = LoggerFactory.getLogger(Office2PdfUtils.class);
private static final String FONT_PATH = Office2PdfUtils.class.getResource("/SIMHEI.TTF").getPath().substring(1);

private Office2PdfUtils() {
}
Expand All @@ -35,19 +30,18 @@ private Office2PdfUtils() {
public static void convert2Pdf(InputStream inputStream, OutputStream outputStream, String extensionName) throws Exception {
switch (extensionName) {
case "doc":
doDOC2PDF(inputStream, outputStream);
break;
case "docx":
doDOCX2PDF(inputStream, outputStream);
doWord2PDF(inputStream, outputStream);
break;
case "xls":
doXLS2PDF(inputStream, outputStream);
break;
case "xlsx":
doXLSX2PDF(inputStream, outputStream);
doExcel2PDF(inputStream, outputStream);
break;
case "ppt":
case "pptx":
doPowerPoint2PDF(inputStream, outputStream);
break;
default:
// Now we can`t convert ppt and pptx file to pdf
// Default do nothing
}
}
Expand All @@ -66,33 +60,54 @@ public static void convert2Pdf(File sourceFile, File targetFile) throws Exceptio
convert2Pdf(fileInputStream, fileOutputStream, extensionName);
}

private static void doXLS2PDF(InputStream inputStream, OutputStream outputStream) throws IOException {

}

private static void doXLSX2PDF(InputStream inputStream, OutputStream outputStream) throws IOException {

private static void doExcel2PDF(InputStream inputStream, OutputStream outputStream) throws IOException {
if (!Office2PdfLicense.getExcelLicense()) {
logger.error("doExcel2PDF Error: License Error");
throw new RuntimeException("doExcel2PDF Error: License Error");
}
try {
Workbook wb = new Workbook(inputStream);
wb.save(outputStream, com.aspose.cells.SaveFormat.PDF);
} catch (Exception e) {
logger.error("doExcel2PDF Error: ", e);
throw new RuntimeException(e);
} finally {
outputStream.close();
inputStream.close();
}
}

private static void doDOC2PDF(InputStream inputStream, OutputStream outputStream) throws IOException {

private static void doWord2PDF(InputStream inputStream, OutputStream outputStream) throws IOException {
if (!Office2PdfLicense.getWordLicense()) {
logger.error("doWord2PDF Error: License Error");
throw new RuntimeException("doWord2PDF Error: License Error");
}
try {
Document doc = new Document(inputStream);
doc.save(outputStream, com.aspose.words.SaveFormat.PDF);
} catch (Exception e) {
logger.error("doWord2PDF Error: ", e);
throw new RuntimeException(e);
} finally {
outputStream.close();
inputStream.close();
}
}

private static void doDOCX2PDF(InputStream inputStream, OutputStream outputStream) throws IOException {
logger.debug("font path: " + FONT_PATH);
ITextFontRegistry iTextFontRegistry = new ITextFontRegistry() {
@Override
public Font getFont(String familyName, String encoding, float size, int style, Color color) {
BaseFont base = null;
try {
base = BaseFont.createFont(FONT_PATH, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
} catch (Exception e) {
logger.error(e.getMessage());
}
return new Font(base, size, style, color);
}
};
XWPFDocument document = new XWPFDocument(inputStream);
PdfConverter.getInstance().convert(document, outputStream, PdfOptions.create().fontProvider(iTextFontRegistry));
private static void doPowerPoint2PDF(InputStream inputStream, OutputStream outputStream) throws IOException {
if (!Office2PdfLicense.getPPTLicense()) {
logger.error("doPowerPoint2PDF Error: License Error");
throw new RuntimeException("doPowerPoint2PDF Error: License Error");
}
try {
Presentation ppt = new Presentation(inputStream);
ppt.save(outputStream, com.aspose.slides.SaveFormat.Pdf);
} catch (Exception e) {
logger.error("doPowerPoint2PDF Error: ", e);
throw new RuntimeException(e);
} finally {
outputStream.close();
inputStream.close();
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/top/yunshu/shw/server/util/RoleUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @author itning
*/
public class RoleUtils {
public final class RoleUtils {
private static final String USER_TYPE_STUDENT = "99";

private RoleUtils() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @author itning
*/
public class ZipCompressedFileUtils {
public final class ZipCompressedFileUtils {
private static final Logger logger = LoggerFactory.getLogger(ZipCompressedFileUtils.class);

private static final Gson GSON = new Gson();
Expand Down
Binary file removed src/main/resources/SIMHEI.TTF
Binary file not shown.
13 changes: 13 additions & 0 deletions src/main/resources/license.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<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>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
</Data>
<Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>
21 changes: 18 additions & 3 deletions src/test/java/top/yunshu/shw/server/util/Office2PdfUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@
public class Office2PdfUtilsTest {
@Test
public void testDocx2Pdf() throws Exception {
String inputFile = "C:\\Users\\wangn\\Desktop\\bb.docx";
String outputFile = "C:\\Users\\wangn\\Desktop\\TEST.pdf";
Office2PdfUtils.convert2Pdf(new File(inputFile), new File(outputFile));
String input = "C:\\Users\\wangn\\Desktop\\";
String output = "C:\\Users\\wangn\\Desktop\\";
dos(input + "doc.doc", output + "doc.pdf");

dos(input + "docx.docx", output + "docx.pdf");

dos(input + "xls.xls", output + "xls.pdf");

dos(input + "xlsx.xlsx", output + "xlsx.pdf");

dos(input + "ppt.ppt", output + "ppt.pdf");

dos(input + "pptx.pptx", output + "pptx.pdf");

}

@Test
Expand All @@ -18,4 +29,8 @@ public void testMD5() {
String fileMD5 = FileUtils.getFileMD5(new File(inputFile));
System.out.println(fileMD5);
}

private static void dos(String inputFile, String outputFile) throws Exception {
Office2PdfUtils.convert2Pdf(new File(inputFile), new File(outputFile));
}
}

0 comments on commit 191323d

Please sign in to comment.