原来的,只支持汉字,不支持Unicode
javascript
package com.gangwantech.web.utils;
import com.aspose.words.*;
import net.coobird.thumbnailator.Thumbnails;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
/**
* @Author: LiHuaZhi
* @Date: 2021/7/13 14:21
* @Description:
**/
public class Doc2PdfUtil {
/**
* 加载授权配置文件
*
* @return
*/
private static boolean getLicense() {
boolean result = false;
try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("license.xml")) {
// License的包路径必须为com.aspose.words.License
License license = new License();
license.setLicense(in);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* doc转pdf
*
* @return
*/
public static byte[] doc2Pdf(byte[] bytes) {
System.out.println("pdf转换中...");
long old = System.currentTimeMillis();
try (ByteArrayOutputStream fos = new ByteArrayOutputStream()) {
// 验证
if (!getLicense()) {
throw new RuntimeException("文件转换失败!");
}
// 加载字体
//FontSettings settings = FontSettings.getDefaultInstance();
//String[] fontFamilyNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
//settings.setFontsFolders(fontFamilyNames, true);
//LoadOptions loadOptions = new LoadOptions();
//loadOptions.setFontSettings(settings);
加载字体
//FontSettings settings = FontSettings.getDefaultInstance();
//settings.setFontsFolder("C:\\Windows\\Fonts", true);
//LoadOptions loadOptions = new LoadOptions();
//loadOptions.setFontSettings(settings);
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
//Document document = new Document(inputStream, loadOptions);
Document document = new Document(inputStream);
document.save(fos, SaveFormat.PDF);
byte[] buffer = fos.toByteArray();
long now = System.currentTimeMillis();
System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒");
return buffer;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("文件转换失败!");
}
}
public static byte[] rotateUsingThumbnailatorPng(String urlAddress, double angle) throws IOException {
URL url = new URL(urlAddress);
BufferedImage originalImage = ImageIO.read(url);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 直接使用Thumbnailator进行旋转操作,并写入到ByteArrayOutputStream
Thumbnails.of(originalImage)
.scale(1.0)
.rotate(angle)
.outputFormat("PNG") // 根据需要可更改输出格式
.toOutputStream(baos);
// 使用ByteArrayInputStream读取处理后的字节数组并转换为BufferedImage
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
try {
BufferedImage read = ImageIO.read(bais);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(read, "PNG", os); // 注意这里是写为PNG格式,根据实际情况可以调整为JPEG等其他格式
return os.toByteArray();
} finally {
bais.close(); // 关闭ByteArrayInputStream
}
}
}
现在的支持汉字和Unicode
javascript
package com.gangwantech.web.utils;
import com.aspose.words.*;
import net.coobird.thumbnailator.Thumbnails;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
/**
* @Author: LiHuaZhi
* @Date: 2021/7/13 14:21
* @Description:
**/
public class Doc2PdfUtil {
private static final String DEFAULT_FONT_FOLDER = "/usr/share/fonts/noto/"; // Linux环境下默认字体目录
/**
* 加载授权配置文件
*
* @return
*/
private static boolean getLicense() {
boolean result = false;
try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("license.xml")) {
// License的包路径必须为com.aspose.words.License
License license = new License();
license.setLicense(in);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* doc转pdf
*
* @return
*/
public static byte[] doc2Pdf(byte[] bytes) {
System.out.println("pdf转换中...");
long old = System.currentTimeMillis();
try (ByteArrayOutputStream fos = new ByteArrayOutputStream()) {
// 验证
if (!getLicense()) {
throw new RuntimeException("文件转换失败!");
}
// 设置字体目录以确保支持Unicode字符
FontSettings fontSettings = new FontSettings();
fontSettings.setFontsFolder(DEFAULT_FONT_FOLDER, true);
LoadOptions loadOptions = new LoadOptions();
loadOptions.setFontSettings(fontSettings);
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
Document document = new Document(inputStream, loadOptions);
document.save(fos, SaveFormat.PDF);
byte[] buffer = fos.toByteArray();
long now = System.currentTimeMillis();
System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒");
return buffer;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("文件转换失败!");
}
}
public static byte[] rotateUsingThumbnailatorPng(String urlAddress, double angle) throws IOException {
URL url = new URL(urlAddress);
BufferedImage originalImage = ImageIO.read(url);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 直接使用Thumbnailator进行旋转操作,并写入到ByteArrayOutputStream
Thumbnails.of(originalImage)
.scale(1.0)
.rotate(angle)
.outputFormat("PNG") // 根据需要可更改输出格式
.toOutputStream(baos);
// 使用ByteArrayInputStream读取处理后的字节数组并转换为BufferedImage
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
try {
BufferedImage read = ImageIO.read(bais);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(read, "PNG", os); // 注意这里是写为PNG格式,根据实际情况可以调整为JPEG等其他格式
return os.toByteArray();
} finally {
bais.close(); // 关闭ByteArrayInputStream
}
}
}