原来的,只支持汉字,不支持Unicode
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;
public class Doc2PdfUtil {
private static boolean getLicense() {
boolean result = false;
try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("license.xml")) {
License license = new License();
license.setLicense(in);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static byte[] doc2Pdf(byte[] bytes) {
System.out.println("pdf转换中...");
long old = System.currentTimeMillis();
try (ByteArrayOutputStream fos = new ByteArrayOutputStream()) {
if (!getLicense()) {
throw new RuntimeException("文件转换失败!");
}
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
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();
Thumbnails.of(originalImage)
.scale(1.0)
.rotate(angle)
.outputFormat("PNG")
.toOutputStream(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
try {
BufferedImage read = ImageIO.read(bais);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(read, "PNG", os);
return os.toByteArray();
} finally {
bais.close();
}
}
}
现在的支持汉字和Unicode
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;
public class Doc2PdfUtil {
private static final String DEFAULT_FONT_FOLDER = "/usr/share/fonts/noto/";
private static boolean getLicense() {
boolean result = false;
try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("license.xml")) {
License license = new License();
license.setLicense(in);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
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 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();
Thumbnails.of(originalImage)
.scale(1.0)
.rotate(angle)
.outputFormat("PNG")
.toOutputStream(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
try {
BufferedImage read = ImageIO.read(bais);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(read, "PNG", os);
return os.toByteArray();
} finally {
bais.close();
}
}
}