Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DFR-2943 d11 served blank #1754

Merged
merged 16 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ public List<byte[]> downloadDocuments(BulkPrintRequest bulkPrintRequest, String
log.info("Downloading document for bulk print for Case ID: {}", caseId);

List<byte[]> documents = bulkPrintRequest.getBulkPrintDocuments().stream()
.map(bulkPrintDocument -> service.download(bulkPrintDocument.getBinaryFileUrl(), auth))
.map(bulkPrintDocument -> documentConversionService.flattenPdfDocument(service.download(bulkPrintDocument.getBinaryFileUrl(), auth)))
.toList();
log.info("Download document count for bulk print {} for Case ID: {} ", documents.size(),
caseId);

return documents;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.tika.Tika;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
Expand Down Expand Up @@ -57,6 +60,23 @@ public String getConvertedFilename(String filename) {
return FilenameUtils.getBaseName(filename) + ".pdf";
}

public byte[] flattenPdfDocument(byte[] document) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();

try {
PDDocument doc = PDDocument.load(document);
PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();

acroForm.flatten();
doc.save(bos);
doc.close();
return bos.toByteArray();
} catch (IOException e) {
log.error("Unable to flatten document");
al-hmcts marked this conversation as resolved.
Show resolved Hide resolved
}
return document;
}

private byte[] convert(Document sourceDocument, String auth) {
try {
String filename = getConvertedFilename(sourceDocument.getFileName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class BulkPrintDocumentServiceTest {
private static final String DOC_FILE_NAME = "abc.docx";
public static final String AUTH = "auth";
private final byte[] someBytes = "ainhsdcnoih".getBytes();
private final byte[] someFlattenedBytes = "ainhsdcnoih_flattened".getBytes();
@InjectMocks
private BulkPrintDocumentService service;

Expand All @@ -48,6 +49,7 @@ class BulkPrintDocumentServiceTest {
@Test
void downloadDocuments() {
when(evidenceManagementService.download(FILE_URL, AUTH)).thenReturn(someBytes);
when(documentConversionService.flattenPdfDocument(someBytes)).thenReturn(someFlattenedBytes);

BulkPrintRequest bulkPrintRequest = BulkPrintRequest.builder()
.bulkPrintDocuments(singletonList(BulkPrintDocument.builder()
Expand All @@ -57,7 +59,7 @@ void downloadDocuments() {
.build();

List<byte[]> result = service.downloadDocuments(bulkPrintRequest, AUTH);
assertThat(result.get(0), is(equalTo(someBytes)));
assertThat(result.get(0), is(equalTo(someFlattenedBytes)));
}

@Test
Expand All @@ -77,6 +79,7 @@ void validateWordDocumentOnUploadedDocument() {
@Test
void validateEncryptionOnUploadedDocumentWhenInvalidByteSupplied() {
when(evidenceManagementService.download(FILE_BINARY_URL, AUTH)).thenReturn(someBytes);
when(documentConversionService.flattenPdfDocument(someBytes)).thenReturn(someFlattenedBytes);
CaseDocument caseDocument = TestSetUpUtils.caseDocument(FILE_URL, FILE_NAME, FILE_BINARY_URL);
BulkPrintRequest bulkPrintRequest = BulkPrintRequest.builder()
.bulkPrintDocuments(singletonList(BulkPrintDocument.builder()
Expand All @@ -86,7 +89,7 @@ void validateEncryptionOnUploadedDocumentWhenInvalidByteSupplied() {
.build();

List<byte[]> result = service.downloadDocuments(bulkPrintRequest, AUTH);
assertThat(result.get(0), is(equalTo(someBytes)));
assertThat(result.get(0), is(equalTo(someFlattenedBytes)));

List<String> errors = new ArrayList<>();
service.validateEncryptionOnUploadedDocument(caseDocument, "1234", errors, AUTH);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package uk.gov.hmcts.reform.finrem.caseorchestration.service.documentgenerator;


import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -19,6 +18,9 @@
import uk.gov.hmcts.reform.finrem.caseorchestration.service.DocumentConversionService;
import uk.gov.hmcts.reform.finrem.caseorchestration.service.evidencemanagement.EvidenceManagementDownloadService;

import java.io.IOException;
import java.io.InputStream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
Expand Down Expand Up @@ -57,6 +59,20 @@ public void setUp() {
documentToConvert.setBinaryUrl("binaryurl.com");
}

@Test
public void flattenPdfDocument() throws IOException {

String editedPdfFixture = "/fixtures/D11Edited.pdf";
byte[] editedPdfBytes = loadResource(editedPdfFixture);

String flatPdfFixture = "/fixtures/D11Edited-flattened.pdf";
byte[] expectedFlatPdfBytes = loadResource(flatPdfFixture);

byte[] result = documentConversionService.flattenPdfDocument(editedPdfBytes);

assertThat(expectedFlatPdfBytes, is(result));
}

@Test
public void convertWordToPdf() {
mockServer.expect(requestTo(PDF_SERVICE_URI))
Expand Down Expand Up @@ -91,4 +107,12 @@ public void getConvertedFilename() {
assertThat(documentConversionService.getConvertedFilename("nodot"), is("nodot.pdf"));
assertThat(documentConversionService.getConvertedFilename("word.docx"), is("word.pdf"));
}

private byte[] loadResource(String testPdf) throws IOException {

try (InputStream resourceAsStream = getClass().getResourceAsStream(testPdf)) {
assert resourceAsStream != null;
return resourceAsStream.readAllBytes();
}
}
ptrelease marked this conversation as resolved.
Show resolved Hide resolved
}
Binary file not shown.
Binary file added src/test/resources/fixtures/D11Edited.pdf
Binary file not shown.