Skip to content

Commit edf0a19

Browse files
authored
Fix password in CommandLineApp (tabulapdf#168)
* Use password if provided (tabulapdf#167) * test encrypted files * test wrong password
1 parent 8623392 commit edf0a19

3 files changed

Lines changed: 28 additions & 5 deletions

File tree

‎src/main/java/technology/tabula/CommandLineApp.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public void extractFileInto(File pdfFile, File outputFile) throws ParseException
152152
private void extractFile(File pdfFile, Appendable outFile) throws ParseException {
153153
PDDocument pdfDocument = null;
154154
try {
155-
pdfDocument = PDDocument.load(pdfFile);
155+
pdfDocument = this.password == null ? PDDocument.load(pdfFile) : PDDocument.load(pdfFile, this.password);
156156
PageIterator pageIterator = getPageIterator(pdfDocument);
157157
List<Table> tables = new ArrayList<Table>();
158158

‎src/test/java/technology/tabula/TestCommandLineApp.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,27 @@ public void testGuessOption() throws ParseException, IOException {
122122
}));
123123
}
124124

125+
@Test
126+
public void testEncryptedPasswordSupplied() throws ParseException {
127+
String s = this.csvFromCommandLineArgs(new String[]{
128+
"src/test/resources/technology/tabula/encrypted.pdf",
129+
"-s", "userpassword",
130+
"-p", "1",
131+
"-f", "CSV"
132+
});
133+
assertEquals("FLA Audit Profile,,,,,,,,,", s.split("\\r?\\n")[0]);
134+
}
135+
136+
@Test(expected=org.apache.commons.cli.ParseException.class)
137+
public void testEncryptedWrongPassword() throws ParseException {
138+
String s = this.csvFromCommandLineArgs(new String[]{
139+
"src/test/resources/technology/tabula/encrypted.pdf",
140+
"-s", "wrongpassword",
141+
"-p", "1",
142+
"-f", "CSV"
143+
});
144+
}
145+
146+
147+
125148
}

‎src/test/java/technology/tabula/TestObjectExtractor.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ public void testCanReadPDFWithOwnerEncryption() throws IOException {
3939
assertEquals(2, i);
4040
}
4141

42-
/*
42+
4343
@Test
4444
public void testGoodPassword() throws IOException {
45-
PDDocument pdf_document = PDDocument.load(new File("src/test/resources/technology/tabula/encrypted.pdf"));
46-
ObjectExtractor oe = new ObjectExtractor(pdf_document, "userpassword");
45+
PDDocument pdf_document = PDDocument.load(new File("src/test/resources/technology/tabula/encrypted.pdf"), "userpassword");
46+
ObjectExtractor oe = new ObjectExtractor(pdf_document);
4747
List<Page> pages = new ArrayList<Page>();
4848
PageIterator pi = oe.extract();
4949
while (pi.hasNext()) {
5050
pages.add(pi.next());
5151
}
5252
assertEquals(1, pages.size());
5353
}
54-
*/
54+
5555

5656
@Test
5757
public void testTextExtractionDoesNotRaise() throws IOException {

0 commit comments

Comments
 (0)