Skip to content

Commit

Permalink
Account for PDFs without form layer
Browse files Browse the repository at this point in the history
  • Loading branch information
al-hmcts committed Jul 23, 2024
1 parent 94c09d2 commit 35e2baf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.io.File;
import java.io.IOException;
import java.util.Optional;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -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<PDAcroForm> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Binary file added src/test/resources/fixtures/D81_consent_order.pdf
Binary file not shown.

0 comments on commit 35e2baf

Please sign in to comment.