Skip to content

Commit

Permalink
Add support for parsing password-protected PDFs
Browse files Browse the repository at this point in the history
  • Loading branch information
jsvine committed Apr 15, 2019
1 parent b186653 commit 6f70d09
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ with pdfplumber.open("path/to/file.pdf") as pdf:

Both methods return an instance of the `pdfplumber.PDF` class.

To load a password-protected PDF, pass the `password` keyword argument, e.g., `pdfplumber.open("file.pdf", password = "test")`.

### The `pdfplumber.PDF` class

The top-level `pdfplumber.PDF` class represents a single PDF and has two main properties:
Expand Down
10 changes: 8 additions & 2 deletions pdfplumber/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@
class PDF(Container):
cached_properties = Container.cached_properties + [ "_pages" ]

def __init__(self, stream, pages=None, laparams=None, precision=0.001):
def __init__(self,
stream,
pages = None,
laparams = None,
precision = 0.001,
password = b""
):
self.laparams = None if laparams == None else LAParams(**laparams)
self.stream = stream
self.pages_to_parse = pages
self.precision = precision
rsrcmgr = PDFResourceManager()
self.doc = PDFDocument(PDFParser(stream))
self.doc = PDFDocument(PDFParser(stream), password = password)
self.metadata = {}
for info in self.doc.info:
self.metadata.update(info)
Expand Down
Binary file added tests/pdfs/password-example.pdf
Binary file not shown.
6 changes: 6 additions & 0 deletions tests/test-basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ def test_rotation(self):

assert(rotated.pages[0].cropbox == self.pdf.pages[0].cropbox)
assert(rotated.pages[0].bbox != self.pdf.pages[0].bbox)

def test_password(self):
path = os.path.join(HERE, "pdfs/password-example.pdf")
pdf = pdfplumber.open(path, password = "test")
assert(len(pdf.chars) > 0)
pdf.close()

0 comments on commit 6f70d09

Please sign in to comment.