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

Commit

Permalink
fix bug and add config
Browse files Browse the repository at this point in the history
  • Loading branch information
itning committed Mar 14, 2019
1 parent 050baf8 commit bcec319
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 10 deletions.
2 changes: 1 addition & 1 deletion 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.6.1-RELEASE</version>
<version>1.6.2-RELEASE</version>
<name>shw_server</name>
<description>Student HomeWork Management System</description>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package top.yunshu.shw.server.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
* @author itning
*/
@Component
public class SpringContextHelper implements ApplicationContextAware {

private static ApplicationContext applicationContext;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (SpringContextHelper.applicationContext == null) {
SpringContextHelper.applicationContext = applicationContext;
}
}

public static ApplicationContext getApplicationContext() {
return applicationContext;
}

public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}

public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}

public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,26 @@ public String saveTempDir(@ApiParam(value = "打包临时目录地址", required
return "redirect:/config";
}

/**
* 设置字体文件目录
*
* @param name 目录
* @param session {@link HttpSession}
* @return "redirect:/config"
*/
@ApiOperation("设置字体文件目录")
@PostMapping("/config/fontDir")
public String saveFontDir(@ApiParam(value = "字体文件目录", required = true) String name,
@ApiIgnore HttpSession session) {
if (session.getAttribute(USER) == null) {
return "redirect:/config/login";
}
if (StringUtils.isNotBlank(name)) {
configService.saveConfig(Config.ConfigKey.FONT_DIR, name);
}
return "redirect:/config";
}

/**
* 设置CAS SERVER
*
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/top/yunshu/shw/server/entity/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ public enum ConfigKey {
/**
* 本地服务地址
*/
LOCAL_SERVER_URL("local_server_url");
LOCAL_SERVER_URL("local_server_url"),
/**
* 字体文件目录
*/
FONT_DIR("font_dir");
private String key;

ConfigKey(String key) {
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/top/yunshu/shw/server/util/Office2PdfUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import com.aspose.words.WarningType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.yunshu.shw.server.config.SpringContextHelper;
import top.yunshu.shw.server.entity.Config;
import top.yunshu.shw.server.service.config.ConfigService;

import java.io.*;

Expand All @@ -18,8 +21,7 @@
*/
public final class Office2PdfUtils {
private static final Logger logger = LoggerFactory.getLogger(Office2PdfUtils.class);
private static final String FONT_NAME = "SIMSUN";
private static final String FONT_PATH = Office2PdfUtils.class.getResource("/").getPath().substring(1);
private static final String FONT_NAME = "simsun";

private Office2PdfUtils() {
}
Expand Down Expand Up @@ -71,12 +73,12 @@ private static void doExcel2PDF(InputStream inputStream, OutputStream outputStre
throw new RuntimeException("doExcel2PDF Error: License Error");
}
try {
FontSettings.setFontsFolder(FONT_PATH, true);
String fontDir = SpringContextHelper.getBean(ConfigService.class).getConfig(Config.ConfigKey.FONT_DIR).orElse("");
FontSettings.setFontsFolder(fontDir, true);
FontSettings.setDefaultFontName(FONT_NAME);
Workbook wb = new Workbook(inputStream);
wb.save(outputStream, com.aspose.cells.SaveFormat.PDF);
} catch (Exception e) {
logger.error("FONT_PATH: " + FONT_PATH);
logger.error("doExcel2PDF Error: ", e);
throw new RuntimeException(e);
} finally {
Expand All @@ -91,7 +93,8 @@ private static void doWord2PDF(InputStream inputStream, OutputStream outputStrea
throw new RuntimeException("doWord2PDF Error: License Error");
}
try {
FontSettings.setFontsFolder(FONT_PATH, true);
String fontDir = SpringContextHelper.getBean(ConfigService.class).getConfig(Config.ConfigKey.FONT_DIR).orElse("");
FontSettings.setFontsFolder(fontDir, true);
FontSettings.setDefaultFontName(FONT_NAME);
Document doc = new Document(inputStream);
IWarningCallback callback = info -> {
Expand All @@ -102,7 +105,6 @@ private static void doWord2PDF(InputStream inputStream, OutputStream outputStrea
doc.setWarningCallback(callback);
doc.save(outputStream, com.aspose.words.SaveFormat.PDF);
} catch (Exception e) {
logger.error("FONT_PATH: " + FONT_PATH);
logger.error("doWord2PDF Error: ", e);
throw new RuntimeException(e);
} finally {
Expand All @@ -117,12 +119,12 @@ private static void doPowerPoint2PDF(InputStream inputStream, OutputStream outpu
throw new RuntimeException("doPowerPoint2PDF Error: License Error");
}
try {
FontSettings.setFontsFolder(FONT_PATH, true);
String fontDir = SpringContextHelper.getBean(ConfigService.class).getConfig(Config.ConfigKey.FONT_DIR).orElse("");
FontSettings.setFontsFolder(fontDir, true);
FontSettings.setDefaultFontName(FONT_NAME);
Presentation ppt = new Presentation(inputStream);
ppt.save(outputStream, com.aspose.slides.SaveFormat.Pdf);
} catch (Exception e) {
logger.error("FONT_PATH: " + FONT_PATH);
logger.error("doPowerPoint2PDF Error: ", e);
throw new RuntimeException(e);
} finally {
Expand Down
Binary file removed src/main/resources/SIMSUN.TTC
Binary file not shown.
13 changes: 13 additions & 0 deletions src/main/resources/templates/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@
<input class="mdui-btn mdui-btn-raised mdui-ripple mdui-color-theme-accent" type="submit" value="保存"/>
</div>
</form>
<form th:action="@{/config/fontDir}" method="post" onsubmit="return onSave('font_dir_url')"
class="mdui-card">
<div class="mdui-card-content">
<div class="mdui-textfield mdui-textfield-floating-label">
<label class="mdui-textfield-label">字体文件目录(必须设置,否则解析中文乱码)</label>
<!--/*@thymesVar id="font_dir" type="java.lang.String"*/-->
<input class="mdui-textfield-input" id="font_dir_url" name="name" type="text"
th:value="${font_dir}"/>
<div class="mdui-textfield-error">必填</div>
</div>
<input class="mdui-btn mdui-btn-raised mdui-ripple mdui-color-theme-accent" type="submit" value="保存"/>
</div>
</form>
<form th:action="@{/config/user}" method="post" class="mdui-card">
<div class="mdui-card-content">
<div class="mdui-textfield mdui-textfield-floating-label">
Expand Down

0 comments on commit bcec319

Please sign in to comment.