[英]Does jPod Merge PDFs by data streaming?
我正在使用jPod合並我的PDF文檔。 我合並了每20頁400個PDF,結果文件為190 MB,而單個pdf的大小為38 KB。 我在IDE中檢查了堆狀態。 我沒有任何內存不足錯誤。 我在擁有30個客戶端的Apache Tomcat中運行了相同的程序。 我的Tomcat停止處理請求。 是因為jPod不使用Streaming還是由於其他原因?
private void run() throws Throwable {
String sOutFileFullPathAndName = "/Users/test/Downloads/" + UUID.randomUUID().toString().replace("-", "");
PDDocument dstDocument = PDDocument.createNew();
for (int i = 0;i < 400; i++) {
//System.out.println(Runtime.getRuntime().freeMemory());
PDDocument srcDocument = PDDocument.createFromLocator(new FileLocator("/Users/test/Downloads/2.pdf") );
mergeDocuments(dstDocument, srcDocument);
}
FileLocator destinationLocator = new FileLocator(sOutFileFullPathAndName);
dstDocument.save(destinationLocator, null);
dstDocument.close();
}
private void mergeDocuments(PDDocument dstDocument, PDDocument srcDocument) {
PDPageTree pageTree = srcDocument.getPageTree();
int pageCount = pageTree.getCount();
for (int index = 0; index < pageCount; index++) {
PDPage srcPage = pageTree.getPageAt( index );
appendPage(dstDocument, srcPage);
srcPage = null;
}
}
private void appendPage(PDDocument document, PDPage page) {
PDResources srcResources = page.getResources();
CSContent cSContent = page.getContentStream();
PDPage newPage = (PDPage) PDPage.META.createNew();
// copy resources from source page to the newly created page
PDResources newResources = (PDResources) PDResources.META
.createFromCos(srcResources.cosGetObject().copyDeep());
newPage.setResources(newResources);
newPage.setContentStream(cSContent);
// add that new page to the destination document
document.addPageNode(newPage);
}
PDF不僅僅是頁面數據的“流”。 它是一個復雜的數據結構,其中包含相互引用的對象。 在這種具體情況下,頁面樹/節點,內容流,資源,...
jPod僅使用弱引用將持久對象保存在內存中-始終可以從隨機訪問數據中刷新它們。 如果開始更新對象結構,對象將被“鎖定”在內存中,僅僅是因為更改不是持久的並且無法再刷新。
在不定期保存結果的情況下進行大量更改將使完整的結構保留在內存中-我認為這是您的問題。 時不時地保存將減少內存占用。
另外,此算法將創建不良頁樹,該頁樹包含成千上萬頁的線性數組。 您應該嘗試創建平衡的樹結構。 優化的另一點是資源處理。 合並字體或圖像之類的資源可能會大大減小目標大小。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.