Skip to content

Commit

Permalink
Update PDFBox to version 3.0.0 (#355)
Browse files Browse the repository at this point in the history
* WIP update PDFBox to version 3.0.0
* Pass bytes to loader function
* Remove unused imports
  • Loading branch information
cy-by authored Aug 24, 2023
1 parent 514c28e commit 161cbe5
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 74 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ dependencies {
implementation 'org.webjars.npm:dropzone:5.9.3'
implementation 'com.amazonaws:aws-java-sdk-s3:1.12.364'
implementation 'com.smartystreets.api:smartystreets-java-sdk:3.14.0'
implementation 'org.apache.pdfbox:pdfbox:2.0.29'
implementation 'org.apache.pdfbox:pdfbox:3.0.0'
implementation 'org.yaml:snakeyaml:2.1'
implementation 'com.mailgun:mailgun-java:1.0.8'
implementation 'com.google.crypto.tink:tink:1.10.0'
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/formflow/library/FileController.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -120,7 +121,7 @@ public ResponseEntity<?> upload(

String fileExtension = Files.getFileExtension(Objects.requireNonNull(file.getOriginalFilename()));
if (fileExtension.equals("pdf")) {
try (PDDocument ignored = PDDocument.load(file.getInputStream())) {
try (PDDocument ignored = Loader.loadPDF(file.getBytes())) {
} catch (InvalidPasswordException e) {
// TODO update when we add internationalization to use locale for message source
String message = messageSource.getMessage("upload-documents.error-password-protected", null, null);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/formflow/library/pdf/PDFBoxFieldFiller.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.apache.pdfbox.pdmodel.interactive.form.PDCheckBox;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;
import org.apache.pdfbox.Loader;
import org.jetbrains.annotations.NotNull;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
Expand Down Expand Up @@ -36,7 +37,7 @@ public PdfFile fill(String pathToPdfResource, Collection<PdfField> fields) {
@NotNull
private PDDocument fillOutPdfs(Collection<PdfField> fields, Resource pdfResource) {
try {
PDDocument loadedDoc = PDDocument.load(pdfResource.getInputStream());
PDDocument loadedDoc = Loader.loadPDF(pdfResource.getContentAsByteArray());
PDAcroForm acroForm = loadedDoc.getDocumentCatalog().getAcroForm();
acroForm.setNeedAppearances(true);
fillAcroForm(fields, acroForm);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/formflow/library/pdf/PdfFile.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package formflow.library.pdf;

import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.springframework.boot.system.ApplicationTemp;

Expand Down Expand Up @@ -67,7 +68,7 @@ public byte[] fileBytes() throws IOException {
* @throws IOException Thrown if the file cannot be read in.
*/
public void finalizeForSending() throws IOException {
PDDocument pdDocument = PDDocument.load(fileBytes());
PDDocument pdDocument = Loader.loadPDF(fileBytes());
pdDocument.getDocumentCatalog().getAcroForm().flatten();
pdDocument.save(path);
pdDocument.close();
Expand Down
133 changes: 67 additions & 66 deletions src/test/java/formflow/library/pdf/PDFBoxFieldFillerTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package formflow.library.pdf;

import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.junit.jupiter.api.Test;
Expand All @@ -13,70 +14,70 @@

class PDFBoxFieldFillerTest {

private final PDFBoxFieldFiller pdfBoxFieldFiller = new PDFBoxFieldFiller();
private final String pdf = "/pdfs/testPdf.pdf";

@Test
void shouldMapAllFieldTypes() throws IOException {
String expectedFieldValue = "Michael";
String radioValue = "option2";
String checkboxSelectedValue = "Yes";
String unselectedCheckboxValue = "Off";

Collection<PdfField> fields = List.of(
new PdfField("TEXT_FIELD", expectedFieldValue),
new PdfField("RADIO_BUTTON", radioValue),
new PdfField("CHECKBOX_OPTION_1", checkboxSelectedValue),
new PdfField("CHECKBOX_OPTION_3", checkboxSelectedValue)
);

PdfFile pdfFile = pdfBoxFieldFiller.fill(pdf, fields);
PDDocument pdDocument = PDDocument.load(pdfFile.fileBytes());
PDAcroForm acroForm = pdDocument.getDocumentCatalog().getAcroForm();

assertThat(acroForm.getField("TEXT_FIELD").getValueAsString()).isEqualTo(expectedFieldValue);
assertThat(acroForm.getField("RADIO_BUTTON").getValueAsString()).isEqualTo(radioValue);
assertThat(acroForm.getField("CHECKBOX_OPTION_1").getValueAsString()).isEqualTo(checkboxSelectedValue);
assertThat(acroForm.getField("CHECKBOX_OPTION_2").getValueAsString()).isEqualTo(unselectedCheckboxValue);
assertThat(acroForm.getField("CHECKBOX_OPTION_3").getValueAsString()).isEqualTo(checkboxSelectedValue);

pdDocument.close();
}

@Test
void shouldSetNullTextFieldsAsEmptyString() throws IOException {
Collection<PdfField> fields = List.of(
new PdfField("TEXT_FIELD", null)
);
PdfFile pdfFile = pdfBoxFieldFiller.fill(pdf, fields);

PDDocument pdDocument = PDDocument.load(pdfFile.fileBytes());
assertThat(pdDocument.getDocumentCatalog().getAcroForm().getField("TEXT_FIELD").getValueAsString()).isEqualTo("");
pdDocument.close();
}

@Test
void shouldNotThrowException_whenFieldIsNotFound() {
assertThatCode(() -> pdfBoxFieldFiller.fill(pdf,
List.of(new PdfField("definitely-not-a-field", ""))
)
).doesNotThrowAnyException();
}

@Test
void shouldSupportEmojis() throws IOException {
String submittedValue = "Michael😃";
String expectedValue = "Michael😃";

Collection<PdfField> fields = List.of(
new PdfField("TEXT_FIELD", submittedValue)
);

PdfFile pdfFile = pdfBoxFieldFiller.fill(pdf, fields);
PDDocument pdDocument = PDDocument.load(pdfFile.fileBytes());
PDAcroForm acroForm = pdDocument.getDocumentCatalog().getAcroForm();

assertThat(acroForm.getField("TEXT_FIELD").getValueAsString()).isEqualTo(expectedValue);
pdDocument.close();
}
private final PDFBoxFieldFiller pdfBoxFieldFiller = new PDFBoxFieldFiller();
private final String pdf = "/pdfs/testPdf.pdf";

@Test
void shouldMapAllFieldTypes() throws IOException {
String expectedFieldValue = "Michael";
String radioValue = "option2";
String checkboxSelectedValue = "Yes";
String unselectedCheckboxValue = "Off";

Collection<PdfField> fields = List.of(
new PdfField("TEXT_FIELD", expectedFieldValue),
new PdfField("RADIO_BUTTON", radioValue),
new PdfField("CHECKBOX_OPTION_1", checkboxSelectedValue),
new PdfField("CHECKBOX_OPTION_3", checkboxSelectedValue)
);

PdfFile pdfFile = pdfBoxFieldFiller.fill(pdf, fields);
PDDocument pdDocument = Loader.loadPDF(pdfFile.fileBytes());
PDAcroForm acroForm = pdDocument.getDocumentCatalog().getAcroForm();

assertThat(acroForm.getField("TEXT_FIELD").getValueAsString()).isEqualTo(expectedFieldValue);
assertThat(acroForm.getField("RADIO_BUTTON").getValueAsString()).isEqualTo(radioValue);
assertThat(acroForm.getField("CHECKBOX_OPTION_1").getValueAsString()).isEqualTo(checkboxSelectedValue);
assertThat(acroForm.getField("CHECKBOX_OPTION_2").getValueAsString()).isEqualTo(unselectedCheckboxValue);
assertThat(acroForm.getField("CHECKBOX_OPTION_3").getValueAsString()).isEqualTo(checkboxSelectedValue);

pdDocument.close();
}

@Test
void shouldSetNullTextFieldsAsEmptyString() throws IOException {
Collection<PdfField> fields = List.of(
new PdfField("TEXT_FIELD", null)
);
PdfFile pdfFile = pdfBoxFieldFiller.fill(pdf, fields);

PDDocument pdDocument = Loader.loadPDF(pdfFile.fileBytes());
assertThat(pdDocument.getDocumentCatalog().getAcroForm().getField("TEXT_FIELD").getValueAsString()).isEqualTo("");
pdDocument.close();
}

@Test
void shouldNotThrowException_whenFieldIsNotFound() {
assertThatCode(() -> pdfBoxFieldFiller.fill(pdf,
List.of(new PdfField("definitely-not-a-field", ""))
)
).doesNotThrowAnyException();
}

@Test
void shouldSupportEmojis() throws IOException {
String submittedValue = "Michael😃";
String expectedValue = "Michael😃";

Collection<PdfField> fields = List.of(
new PdfField("TEXT_FIELD", submittedValue)
);

PdfFile pdfFile = pdfBoxFieldFiller.fill(pdf, fields);
PDDocument pdDocument = Loader.loadPDF(pdfFile.fileBytes());
PDAcroForm acroForm = pdDocument.getDocumentCatalog().getAcroForm();

assertThat(acroForm.getField("TEXT_FIELD").getValueAsString()).isEqualTo(expectedValue);
pdDocument.close();
}
}
7 changes: 3 additions & 4 deletions src/test/java/formflow/library/pdf/PdfFileTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package formflow.library.pdf;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.Loader;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Pdf;
import org.springframework.boot.system.ApplicationTemp;

import java.io.IOException;
Expand Down Expand Up @@ -42,7 +41,7 @@ void finalizeForSendingRemovesFieldsToLockEditingAndPreserveViewCompatibility()
byte[] originalBytes = tempPdfFile.fileBytes();
tempPdfFile.finalizeForSending();

assertThat(PDDocument.load(tempPdfFile.fileBytes()).getDocumentCatalog().getAcroForm().getFields()).isEmpty();
assertThat(Loader.loadPDF(tempPdfFile.fileBytes()).getDocumentCatalog().getAcroForm().getFields()).isEmpty();
assertThat(originalBytes.length).isGreaterThan(tempPdfFile.fileBytes().length);
}

Expand Down Expand Up @@ -112,4 +111,4 @@ void createPdfFilesAndEnsureDeletion() throws IOException {
assertThat(Files.exists(Path.of(twoPdfFile.path()))).isFalse();
assertThat(Files.exists(Path.of(threePdfFile.path()))).isFalse();
}
}
}

0 comments on commit 161cbe5

Please sign in to comment.