diff --git a/src/main/java/uk/gov/hmcts/reform/finrem/caseorchestration/service/DocumentConversionService.java b/src/main/java/uk/gov/hmcts/reform/finrem/caseorchestration/service/DocumentConversionService.java index dd72542168..c0d4be4d33 100644 --- a/src/main/java/uk/gov/hmcts/reform/finrem/caseorchestration/service/DocumentConversionService.java +++ b/src/main/java/uk/gov/hmcts/reform/finrem/caseorchestration/service/DocumentConversionService.java @@ -24,6 +24,7 @@ import java.io.File; import java.io.IOException; +import java.util.Optional; @Service @RequiredArgsConstructor @@ -65,12 +66,14 @@ public byte[] flattenPdfDocument(byte[] document) { try { PDDocument doc = PDDocument.load(document); - PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm(); - - acroForm.flatten(); - doc.save(bos); - doc.close(); - return bos.toByteArray(); + Optional acroForm = Optional.ofNullable(doc.getDocumentCatalog().getAcroForm()); + + if (acroForm.isPresent()) { + acroForm.get().flatten(); + doc.save(bos); + doc.close(); + return bos.toByteArray(); + } } catch (IOException e) { log.error("Unable to flatten document", e); } diff --git a/src/test/java/uk/gov/hmcts/reform/finrem/caseorchestration/service/documentgenerator/DocumentConversionServiceTest.java b/src/test/java/uk/gov/hmcts/reform/finrem/caseorchestration/service/documentgenerator/DocumentConversionServiceTest.java index 2ed76efd81..9e05b6f067 100644 --- a/src/test/java/uk/gov/hmcts/reform/finrem/caseorchestration/service/documentgenerator/DocumentConversionServiceTest.java +++ b/src/test/java/uk/gov/hmcts/reform/finrem/caseorchestration/service/documentgenerator/DocumentConversionServiceTest.java @@ -74,6 +74,16 @@ public void flattenPdfDocument() throws IOException { assertThat(expectedFlatPdfBytes, is(result)); } + @Test + public void doNotFlattenPdfDocumentWithNoFromLayer() throws IOException { + + String editedPdfFixture = "/fixtures/D81_consent_order.pdf"; + byte[] pdfBytes = loadResource(editedPdfFixture); + byte[] result = documentConversionService.flattenPdfDocument(pdfBytes); + + assertThat(pdfBytes, is(result)); + } + @Test public void flattenNonPdfDocumentHandleException() throws IOException { diff --git a/src/test/resources/fixtures/D81_consent_order.pdf b/src/test/resources/fixtures/D81_consent_order.pdf new file mode 100644 index 0000000000..66fceb7928 Binary files /dev/null and b/src/test/resources/fixtures/D81_consent_order.pdf differ