Skip to content

Commit

Permalink
Make more usable the document merge
Browse files Browse the repository at this point in the history
  • Loading branch information
car031 committed May 3, 2024
1 parent 2416177 commit cacba16
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,37 @@ public void store(Document doc) {
* @param username the user in whose name the method is run
*/
public void store(Document doc, String username) {
User user = new SecurityTool().getUser(username);

DocumentHistory transaction = new DocumentHistory();
transaction.setDocument(doc);
transaction.setDate(new Date());
transaction.setUser(user);
transaction.setUser(new SecurityTool().getUser(username));
store(doc, transaction);
}

/**
* Created a new document into the database
*
* @param doc the document that carries the metadata
* @param file the document's content
* @param username the user in whose name the method is run
*
* @return the created document
*/
public Document create(Document doc, File file, String username) {
DocumentHistory transaction = new DocumentHistory();
transaction.setDocument(doc);
transaction.setDate(new Date());
transaction.setUser(new SecurityTool().getUser(username));

DocumentManager manager = (DocumentManager) Context.get().getBean(DocumentManager.class);
try {
return manager.create(file, doc, transaction);
} catch (PersistenceException e) {
log.error(e.getMessage(), e);
return null;
}
}

/**
* Initializes lazy loaded collections
*
Expand Down Expand Up @@ -322,6 +344,18 @@ public void move(Document doc, String targetPath, String username) {
}
}

/**
* Instantiate a new document that is a clone of the given one. The returned
* clone is not persisted.
*
* @param doc The source document to clone
*
* @return The cloned document
*/
public Document clone(Document doc) {
return new Document(doc);
}

/**
* Copies a document into a target folder
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -54,6 +52,7 @@
import com.logicaldoc.core.threading.ThreadPools;
import com.logicaldoc.core.ticket.Ticket;
import com.logicaldoc.core.ticket.TicketDAO;
import com.logicaldoc.core.util.MergeUtil;
import com.logicaldoc.util.Context;
import com.logicaldoc.util.config.ContextProperties;
import com.logicaldoc.util.html.HTMLSanitizer;
Expand Down Expand Up @@ -1682,7 +1681,7 @@ public Document merge(Collection<Document> documents, long targetFolderId, Strin
Arrays.sort(pdfs, (o1, o2) -> o1.getName().compareTo(o2.getName()));

// Merge all the PDFs
bigPdf = mergePdf(List.of(pdfs));
bigPdf = MergeUtil.mergePdf(List.of(pdfs));

// Add an history entry to track the export of the document
DocumentDAO docDao = (DocumentDAO) Context.get().getBean(DocumentDAO.class);
Expand Down Expand Up @@ -1747,37 +1746,6 @@ private File preparePdfs(User user, List<Long> docIds) throws IOException {
return tempDir;
}

/**
* Merges different PDFs into a single PDF-
*
* @param pdfs ordered list of pdf files to be merged
* @return The merged Pdf file
*
* @throws IOException
*/
private File mergePdf(List<File> pdfs) throws IOException {
File tempDir = null;
try {
Path tempPath = Files.createTempDirectory(MERGE);
tempDir = tempPath.toFile();

File dst = FileUtil.createTempFile(MERGE, ".pdf");

PDFMergerUtility merger = new PDFMergerUtility();
for (File file : pdfs)
merger.addSource(file);

merger.setDestinationFileName(dst.getAbsolutePath());
MemoryUsageSetting memoryUsage = MemoryUsageSetting.setupTempFileOnly();
memoryUsage.setTempDir(tempDir);
merger.mergeDocuments(memoryUsage);

return dst;
} finally {
FileUtil.strongDelete(tempDir);
}
}

public void setDocumentLinkDAO(DocumentLinkDAO documentLinkDAO) {
this.documentLinkDAO = documentLinkDAO;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.logicaldoc.core.util;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.multipdf.PDFMergerUtility;

import com.logicaldoc.util.io.FileUtil;

/**
* An utility to merge files
*
* @author Marco Meschieri - LogicalDOC
* @since 8.9.2
*
*/
public class MergeUtil {

private static final String MERGE = "merge";

private MergeUtil() {
// Not instantiable class
}

/**
* Merges different PDFs into a single PDF-
*
* @param pdfs ordered list of pdf files to be merged
* @return The merged Pdf file
*
* @throws IOException
*/
public static File mergePdf(List<File> pdfs) throws IOException {
File tempDir = null;
try {
Path tempPath = Files.createTempDirectory(MERGE);
tempDir = tempPath.toFile();

File dst = FileUtil.createTempFile(MERGE, ".pdf");

PDFMergerUtility merger = new PDFMergerUtility();
for (File file : pdfs) {
merger.addSource(file);
System.out.println("Added "+file.getPath());
}

merger.setDestinationFileName(dst.getAbsolutePath());
MemoryUsageSetting memoryUsage = MemoryUsageSetting.setupTempFileOnly();
memoryUsage.setTempDir(tempDir);
merger.mergeDocuments(memoryUsage);

return dst;
} finally {
FileUtil.strongDelete(tempDir);
}
}
}

0 comments on commit cacba16

Please sign in to comment.