@@ -73,20 +73,30 @@ public class DocumentProcessor {
7373 * - Closes PDDocument to free OS file handles (required for file deletion)
7474 * - Clears static containers to remove lingering references
7575 * Should always be called in a finally block.
76- */
76+ */
7777 private static void closePdfResources () throws Exception {
78+ Exception closeFailure = null ;
7879 PDDocument document = StaticResources .getDocument ();
7980 if (document != null ) {
80- document .close ();
81+ try {
82+ document .close ();
83+ } catch (Exception e ) {
84+ closeFailure = e ;
85+ }
8186 }
8287
8388 // cleanup static containers
8489 clearCleanupStep ("StaticResources" , StaticResources ::clear );
8590 clearCleanupStep ("StaticContainers" , () -> StaticContainers .updateContainers (null ));
91+ clearCleanupStep ("ContrastRatioConsumer" , StaticLayoutContainers ::closeContrastRatioConsumer );
8692 clearCleanupStep ("StaticLayoutContainers" , StaticLayoutContainers ::clearContainers );
8793 clearCleanupStep ("StaticStorages" , StaticStorages ::clearAllContainers );
8894 clearCleanupStep ("StaticCoreContainers" , StaticCoreContainers ::clearAllContainers );
8995 clearCleanupStep ("StaticXmpCoreContainers" , StaticXmpCoreContainers ::clearAllContainers );
96+
97+ if (closeFailure != null ) {
98+ throw closeFailure ;
99+ }
90100 }
91101
92102 /**
@@ -112,6 +122,7 @@ private static void clearCleanupStep(String name, Runnable cleanup) {
112122 * @throws IOException if unable to process the file
113123 */
114124 public static void processFile (String inputPdfName , Config config ) throws IOException {
125+ Throwable processingFailure = null ;
115126 try {
116127 preprocessing (inputPdfName , config );
117128 calculateDocumentInfo ();
@@ -131,14 +142,17 @@ public static void processFile(String inputPdfName, Config config) throws IOExce
131142 config .getFilterConfig ().isFilterSensitiveData ());
132143 contentSanitizer .sanitizeContents (contents );
133144 generateOutputs (inputPdfName , contents , config );
145+ } catch (IOException | RuntimeException | Error e ) {
146+ processingFailure = e ;
147+ throw e ;
134148 } finally {
135149 // Ensures resources are always released, even if processing throws an exception
136150 try {
137151 closePdfResources ();
138152 } catch (Exception closeException ) {
139153 LOGGER .log (Level .WARNING , "Error during PDF resource cleanup" , closeException );
140- if (originalException != null ) {
141- originalException .addSuppressed (closeException );
154+ if (processingFailure != null ) {
155+ processingFailure .addSuppressed (closeException );
142156 } else {
143157 if (closeException instanceof IOException ) {
144158 throw (IOException ) closeException ;
0 commit comments