版本:23.8
编程语言:java
操作系统:Windows 10 专业版
cpu:Intel(R) Core™ i5-10400 CPU @ 2.90GHz 2.90 GHz
内存:32G
问题现象:
1.文件中包含了比较多的图片,转换耗时在9秒左右
源文件:
ImageFile.zip (9.7 MB)
2.删除文件中的图片,并且填充文字到相同的26页,用相同测试方法,耗时大概1.8秒
源文件:
textFIle.zip (31.9 KB)
麻烦帮忙看看,能否优化转换耗时问题
测试main代码:
public static void main(String[] args) throws Exception {
byte[] bytes = Files.readAllBytes(Paths.get("C:\\Users\\admin\\Documents\\EMobile10 Files\\Downloads\\XXXXX.docx"));
for (int i = 0; i < 2; i++) {
new Thread(()->{
long starat = System.currentTimeMillis();
try (ByteArrayInputStream in = new ByteArrayInputStream(bytes);
ByteArrayOutputStream out = new ByteArrayOutputStream();){
convert2PdfMonitor(in,out);
} catch (Throwable e) {
throw new RuntimeException(e);
}
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end - starat));
}).start();
}
}
使用的Aspose转换代码:
public void convert2PdfMonitor(InputStream in, OutputStream out) throws Exception {
Document doc = new Document(in);
FontSettings fontSettings = FontSettings.getDefaultInstance();
FolderFontSource folderFontSource = new FolderFontSource(FONTS_FOLDER, false, 1);
SystemFontSource systemFontSource = new SystemFontSource(2);
fontSettings.setFontsSources(new FontSourceBase[] { systemFontSource, folderFontSource });
doc.setFontSettings(fontSettings);
doc.getLayoutOptions().setCommentDisplayMode(CommentDisplayMode.HIDE);
doc.acceptAllRevisions();
monitorSavePdf(doc, out);
}
public static void monitorSavePdf(Document doc, OutputStream out){
HashedWheelTimer hashedWheelTimer = HASHED_WHEEL_TIMER_FACTORY.selectHashedWheelTimer(UUID.randomUUID().toString());
RenderPageLayoutCallback pageLayoutCallback = new RenderPageLayoutCallback(hashedWheelTimer);
doc.getLayoutOptions().setCallback(pageLayoutCallback);
ToPdfPageLayoutRequest request = new ToPdfPageLayoutRequest(pageLayoutCallback, ASPOSE_WORD_CONVERT_CONFIG.getPageLayoutTimeout());
CompletableFuture<MonitorResponse<RenderPageLayoutCallback>> pageLayoutFuture = null;
try {
request.setMsg("aspose.word转换");
request.continueExpireTime();
try {
pageLayoutFuture = request.submitRequest();
PdfSaveOptions opts = new PdfSaveOptions();
opts.setUpdateFields(false);
opts.setSaveFormat(SaveFormat.PDF);
doc.save(out, opts);
} catch (Throwable e) {
logger.info("转换word为pdf发生异常", e);
} finally {
// 取消转换超时监控任务
request.cancelMonitor();
// 等待监控结果
if (pageLayoutFuture != null) {
MonitorResponse<RenderPageLayoutCallback> response = pageLayoutFuture.get(20000, TimeUnit.MILLISECONDS);
if (!response.isSuccess()) {
throw new RuntimeException(response.getMessage(), response.getThrowable());
}
}
}
} catch (Throwable e){
throw new RuntimeException(e);
}
}