Skip to content

Commit 518843f

Browse files
Code0987Neeraj
and
Neeraj
authored
Adding ownerpw param (#208)
* add owp flag * add opw * added ownerpw param * update * added test case Co-authored-by: Neeraj <[email protected]>
1 parent dc024b8 commit 518843f

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

pdf2image/pdf2image.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def convert_from_path(
4444
jpegopt=None,
4545
thread_count=1,
4646
userpw=None,
47+
ownerpw=None,
4748
use_cropbox=False,
4849
strict=False,
4950
transparent=False,
@@ -69,6 +70,7 @@ def convert_from_path(
6970
jpegopt -> jpeg options `quality`, `progressive`, and `optimize` (only for jpeg format)
7071
thread_count -> How many threads we are allowed to spawn for processing
7172
userpw -> PDF's password
73+
ownerpw -> PDF's owner password
7274
use_cropbox -> Use cropbox instead of mediabox
7375
strict -> When a Syntax Error is thrown, it will be raised as an Exception
7476
transparent -> Output with a transparent background instead of a white one.
@@ -95,7 +97,7 @@ def convert_from_path(
9597
if isinstance(poppler_path, pathlib.PurePath):
9698
poppler_path = poppler_path.as_posix()
9799

98-
page_count = pdfinfo_from_path(pdf_path, userpw, poppler_path=poppler_path)["Pages"]
100+
page_count = pdfinfo_from_path(pdf_path, userpw, ownerpw, poppler_path=poppler_path)["Pages"]
99101

100102
# We start by getting the output format, the buffer processing function and if we need pdftocairo
101103
parsed_fmt, final_extension, parse_buffer_func, use_pdfcairo_format = _parse_format(
@@ -169,6 +171,7 @@ def convert_from_path(
169171
jpegopt,
170172
thread_output_file,
171173
userpw,
174+
ownerpw,
172175
use_cropbox,
173176
transparent,
174177
single_file,
@@ -237,6 +240,7 @@ def convert_from_bytes(
237240
jpegopt=None,
238241
thread_count=1,
239242
userpw=None,
243+
ownerpw=None,
240244
use_cropbox=False,
241245
strict=False,
242246
transparent=False,
@@ -262,6 +266,7 @@ def convert_from_bytes(
262266
jpegopt -> jpeg options `quality`, `progressive`, and `optimize` (only for jpeg format)
263267
thread_count -> How many threads we are allowed to spawn for processing
264268
userpw -> PDF's password
269+
ownerpw -> PDF's owner password
265270
use_cropbox -> Use cropbox instead of mediabox
266271
strict -> When a Syntax Error is thrown, it will be raised as an Exception
267272
transparent -> Output with a transparent background instead of a white one.
@@ -290,6 +295,7 @@ def convert_from_bytes(
290295
jpegopt=jpegopt,
291296
thread_count=thread_count,
292297
userpw=userpw,
298+
ownerpw=ownerpw,
293299
use_cropbox=use_cropbox,
294300
strict=strict,
295301
transparent=transparent,
@@ -317,6 +323,7 @@ def _build_command(
317323
jpegopt,
318324
output_file,
319325
userpw,
326+
ownerpw,
320327
use_cropbox,
321328
transparent,
322329
single_file,
@@ -354,6 +361,9 @@ def _build_command(
354361
if userpw is not None:
355362
args.extend(["-upw", userpw])
356363

364+
if ownerpw is not None:
365+
args.extend(["-opw", ownerpw])
366+
357367
if grayscale:
358368
args.append("-gray")
359369

@@ -440,14 +450,17 @@ def _get_poppler_version(command, poppler_path=None, timeout=None):
440450

441451

442452
def pdfinfo_from_path(
443-
pdf_path, userpw=None, poppler_path=None, rawdates=False, timeout=None
453+
pdf_path, userpw=None, ownerpw=None, poppler_path=None, rawdates=False, timeout=None
444454
):
445455
try:
446456
command = [_get_command_path("pdfinfo", poppler_path), pdf_path]
447457

448458
if userpw is not None:
449459
command.extend(["-upw", userpw])
450460

461+
if ownerpw is not None:
462+
command.extend(["-opw", ownerpw])
463+
451464
if rawdates:
452465
command.extend(["-rawdates"])
453466

@@ -491,15 +504,15 @@ def pdfinfo_from_path(
491504

492505

493506
def pdfinfo_from_bytes(
494-
pdf_file, userpw=None, poppler_path=None, rawdates=False, timeout=None
507+
pdf_file, userpw=None, ownerpw=None, poppler_path=None, rawdates=False, timeout=None
495508
):
496509
fh, temp_filename = tempfile.mkstemp()
497510
try:
498511
with open(temp_filename, "wb") as f:
499512
f.write(pdf_file)
500513
f.flush()
501-
return pdfinfo_from_path(temp_filename, userpw=userpw, rawdates=rawdates,
502-
poppler_path=poppler_path)
514+
return pdfinfo_from_path(temp_filename, userpw=userpw, ownerpw=ownerpw,
515+
rawdates=rawdates, poppler_path=poppler_path)
503516
finally:
504517
os.close(fh)
505518
os.remove(temp_filename)

tests.py

+17
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,23 @@ def test_locked_pdf_with_ownerpw_and_userpw(self):
621621
)
622622
)
623623

624+
@profile
625+
@unittest.skipIf(not POPPLER_INSTALLED, "Poppler is not installed!")
626+
def test_locked_pdf_with_ownerpw_and_userpw_forgotten(self):
627+
start_time = time.time()
628+
with TemporaryDirectory() as path:
629+
with open("./tests/test_locked_both_user_forgotten.pdf", "rb") as pdf_file:
630+
images_from_bytes = convert_from_bytes(
631+
pdf_file.read(), output_folder=path, fmt=".jpg", ownerpw="pdf2image"
632+
)
633+
self.assertTrue(len(images_from_bytes) == 1)
634+
[im.close() for im in images_from_bytes]
635+
print(
636+
"test_locked_pdf_with_ownerpw_and_userpw_forgotten: {} sec".format(
637+
time.time() - start_time
638+
)
639+
)
640+
624641
## Tests cropbox
625642

626643
@profile
1.39 KB
Binary file not shown.

0 commit comments

Comments
 (0)