Example usage for org.apache.pdfbox.io MemoryUsageSetting setupTempFileOnly

List of usage examples for org.apache.pdfbox.io MemoryUsageSetting setupTempFileOnly

Introduction

In this page you can find the example usage for org.apache.pdfbox.io MemoryUsageSetting setupTempFileOnly.

Prototype

public static MemoryUsageSetting setupTempFileOnly() 

Source Link

Document

Setups buffering memory usage to only use temporary file(s) (no main-memory) with not restricted size.

Usage

From source file:org.opensingular.lib.commons.pdf.PDFUtil.java

License:Apache License

/**
 * Concatena os pdf em um nico PDF./* w  ww.  j a va2  s. co  m*/
 * @param pdfs
 * @return O arquivo retornado  temporrio e deve ser apagado pelo solicitante para no deixa lixo.
 */
@Nonnull
public File merge(@Nonnull List<InputStream> pdfs) throws SingularPDFException {
    try (TempFileProvider tmp = TempFileProvider.createForUseInTryClause(this)) {
        PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
        pdfs.forEach(pdfMergerUtility::addSource);

        File tempMergedFile = tmp.createTempFileByDontPutOnDeleteList("merge.pdf");

        try (FileOutputStream output = new FileOutputStream(tempMergedFile)) {
            pdfMergerUtility.setDestinationStream(output);
            pdfMergerUtility.mergeDocuments(MemoryUsageSetting.setupTempFileOnly());
            return tempMergedFile;
        }
    } catch (Exception e) {
        throw new SingularPDFException("Erro realizando merge de arquivos PDF", e);
    } finally {
        for (InputStream in : pdfs) {
            try {
                in.close();
            } catch (IOException e) {
                getLogger().error("Erro fechando inputStrem", e);
            }
        }
    }
}