mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Utility function to detect if a PDF is encrypted
This commit is contained in:
parent
6d449818c2
commit
ec0515d409
@ -8,7 +8,7 @@ import os, subprocess, shutil, re
|
|||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
from calibre import prints
|
from calibre import prints
|
||||||
from calibre.constants import iswindows
|
from calibre.constants import iswindows, ispy3
|
||||||
from calibre.ptempfile import TemporaryDirectory
|
from calibre.ptempfile import TemporaryDirectory
|
||||||
from calibre.ebooks.metadata import (
|
from calibre.ebooks.metadata import (
|
||||||
MetaInformation, string_to_authors, check_isbn, check_doi)
|
MetaInformation, string_to_authors, check_isbn, check_doi)
|
||||||
@ -97,6 +97,17 @@ def page_images(pdfpath, outputdir, first=1, last=1):
|
|||||||
raise ValueError('Failed to render PDF, pdftoppm errorcode: %s'%e.returncode)
|
raise ValueError('Failed to render PDF, pdftoppm errorcode: %s'%e.returncode)
|
||||||
|
|
||||||
|
|
||||||
|
def is_pdf_encrypted(path_to_pdf):
|
||||||
|
if not ispy3 and not isinstance(path_to_pdf, bytes):
|
||||||
|
path_to_pdf = path_to_pdf.encode('mbcs' if iswindows else 'utf-8')
|
||||||
|
pdfinfo = get_tools()[0]
|
||||||
|
raw = subprocess.check_output([pdfinfo, path_to_pdf])
|
||||||
|
q = re.search(br'^Encrypted:\s*(\S+)', raw, flags=re.MULTILINE)
|
||||||
|
if q is not None:
|
||||||
|
return q.group(1) == b'yes'
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def get_metadata(stream, cover=True):
|
def get_metadata(stream, cover=True):
|
||||||
with TemporaryDirectory('_pdf_metadata_read') as pdfpath:
|
with TemporaryDirectory('_pdf_metadata_read') as pdfpath:
|
||||||
stream.seek(0)
|
stream.seek(0)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user