新疆后端项目
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.renkang</groupId>
|
||||
<artifactId>functional-modules</artifactId>
|
||||
<version>2.0.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>renkang-starter-aspose-word</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.aspose</groupId>
|
||||
<artifactId>aspose-words</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
package com.renkang.aspose.configure;
|
||||
|
||||
import com.aspose.words.*;
|
||||
import com.renkang.aspose.util.DocConvertUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.core.io.support.ResourcePatternResolver;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* @author Shunzhi Jiang
|
||||
* @since 2023/11/29
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@EnableConfigurationProperties(AsposeWordProperties.class)
|
||||
@Slf4j
|
||||
public class AsposeAutoConfiguration {
|
||||
|
||||
private final AsposeWordProperties properties;
|
||||
|
||||
public AsposeAutoConfiguration(AsposeWordProperties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public License license() throws Exception {
|
||||
License license = new License();
|
||||
ClassPathResource resource = new ClassPathResource("license.xml");
|
||||
license.setLicense(resource.getInputStream());
|
||||
log.info("加载Aspose License成功");
|
||||
return license;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LoadOptions loadOptions() throws IOException {
|
||||
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
||||
Resource[] resources = resolver.getResources(properties.getFontLocation());
|
||||
FontSourceBase[] fonts = Stream.of(resources)
|
||||
.map(resource -> {
|
||||
try {
|
||||
if (!isFont(resource)) {
|
||||
return null;
|
||||
}
|
||||
log.info("加载字体{}", resource.getFilename());
|
||||
byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream());
|
||||
return new MemoryFontSource(bytes);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.toArray(FontSourceBase[]::new);
|
||||
LoadOptions loadOptions = new LoadOptions();
|
||||
FontSettings fontSettings = new FontSettings();
|
||||
fontSettings.setFontsSources(fonts);
|
||||
loadOptions.setFontSettings(fontSettings);
|
||||
DocConvertUtil.setLoadOptions(loadOptions);
|
||||
return loadOptions;
|
||||
}
|
||||
|
||||
private boolean isFont(Resource resource) {
|
||||
String filename = resource.getFilename();
|
||||
if (!StringUtils.hasText(filename)) {
|
||||
return false;
|
||||
}
|
||||
int i = filename.lastIndexOf(".");
|
||||
if (i < 0) {
|
||||
return false;
|
||||
}
|
||||
String ext = filename.substring(i + 1);
|
||||
return "ttf".equalsIgnoreCase(ext)
|
||||
|| "ttc".equalsIgnoreCase(ext)
|
||||
|| "woff".equalsIgnoreCase(ext)
|
||||
|| "woff2".equalsIgnoreCase(ext)
|
||||
|| "oft".equalsIgnoreCase(ext);
|
||||
}
|
||||
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.renkang.aspose.configure;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* @author Shunzhi Jiang
|
||||
* @since 2023/11/29
|
||||
*/
|
||||
@ConfigurationProperties("renkang.aspose")
|
||||
@Data
|
||||
public class AsposeWordProperties {
|
||||
|
||||
private String fontLocation = "classpath*:font/**/*";
|
||||
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.renkang.aspose.util;
|
||||
|
||||
import com.aspose.words.Document;
|
||||
import com.aspose.words.LoadOptions;
|
||||
import com.aspose.words.SaveFormat;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.InvalidPathException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
/**
|
||||
* @author Shunzhi Jiang
|
||||
* @since 2023/11/29
|
||||
*/
|
||||
public class DocConvertUtil {
|
||||
|
||||
private static LoadOptions loadOptions;
|
||||
|
||||
public static void setLoadOptions(LoadOptions loadOptions) {
|
||||
DocConvertUtil.loadOptions = loadOptions;
|
||||
}
|
||||
|
||||
public static boolean checkTraversal(String path) {
|
||||
return path.contains("..");
|
||||
}
|
||||
|
||||
public static void docxConvertPdfSaveLocalAspose(String docxPath, String pdfPath) throws Exception {
|
||||
if (checkTraversal(docxPath)) {
|
||||
throw new InvalidPathException(docxPath, "Illegal path symbol");
|
||||
}
|
||||
if (checkTraversal(pdfPath)) {
|
||||
throw new InvalidPathException(pdfPath, "Illegal path symbol");
|
||||
}
|
||||
//保存pdf
|
||||
//保存pdf
|
||||
try (
|
||||
InputStream ins = Files.newInputStream(Paths.get(docxPath));
|
||||
OutputStream outs = Files.newOutputStream(Paths.get(pdfPath));
|
||||
) {
|
||||
Document doc = new Document(ins, loadOptions);
|
||||
doc.save(outs, SaveFormat.PDF);
|
||||
//关流
|
||||
outs.flush();
|
||||
}
|
||||
}
|
||||
|
||||
public static void docxConvertPdfSaveLocalAspose(Path docxPath, Path pdfPath) throws Exception {
|
||||
//保存pdf
|
||||
try (
|
||||
InputStream ins = Files.newInputStream(docxPath);
|
||||
OutputStream outs = Files.newOutputStream(pdfPath)
|
||||
) {
|
||||
Document doc = new Document(ins, loadOptions);
|
||||
doc.save(outs, SaveFormat.PDF);
|
||||
//关流
|
||||
outs.flush();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
com.renkang.aspose.configure.AsposeAutoConfiguration
|
||||
@@ -0,0 +1,15 @@
|
||||
<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>
|
||||
Reference in New Issue
Block a user