1
0
Fork 0
forked from josch/img2pdf

pdfrw is now disabled by default if it's not installed (closes: #74)

This commit is contained in:
Johannes 'josch' Schauer 2020-04-23 06:59:07 +02:00
parent a270c987f0
commit 0bbbc7a31a
Signed by untrusted user: josch
GPG key ID: F2CBA5C78FBD83E1
2 changed files with 30 additions and 25 deletions

View file

@ -34,6 +34,14 @@ import logging
import struct import struct
import platform import platform
with_pdfrw = False
try:
import pdfrw
with_pdfrw = True
except ImportError:
pass
PY3 = sys.version_info[0] >= 3 PY3 = sys.version_info[0] >= 3
__version__ = "0.3.4" __version__ = "0.3.4"
@ -631,25 +639,14 @@ class pdfdoc(object):
fit_window=False, fit_window=False,
center_window=False, center_window=False,
fullscreen=False, fullscreen=False,
with_pdfrw=True,
): ):
if with_pdfrw: if with_pdfrw:
try: from pdfrw import PdfWriter, PdfDict, PdfName, PdfString
from pdfrw import PdfWriter, PdfDict, PdfName, PdfString
self.with_pdfrw = True
except ImportError:
PdfWriter = MyPdfWriter
PdfDict = MyPdfDict
PdfName = MyPdfName
PdfString = MyPdfString
self.with_pdfrw = False
else: else:
PdfWriter = MyPdfWriter PdfWriter = MyPdfWriter
PdfDict = MyPdfDict PdfDict = MyPdfDict
PdfName = MyPdfName PdfName = MyPdfName
PdfString = MyPdfString PdfString = MyPdfString
self.with_pdfrw = False
now = datetime.now() now = datetime.now()
self.info = PdfDict(indirect=True) self.info = PdfDict(indirect=True)
@ -690,7 +687,7 @@ class pdfdoc(object):
self.writer.version = version self.writer.version = version
# this is done because pdfrw adds info, catalog and pages as the first # this is done because pdfrw adds info, catalog and pages as the first
# three objects in this order # three objects in this order
if not self.with_pdfrw: if not with_pdfrw:
self.writer.addobj(self.info) self.writer.addobj(self.info)
self.writer.addobj(self.writer.catalog) self.writer.addobj(self.writer.catalog)
self.writer.addobj(self.writer.pages) self.writer.addobj(self.writer.pages)
@ -726,7 +723,7 @@ class pdfdoc(object):
trimborder=None, trimborder=None,
artborder=None, artborder=None,
): ):
if self.with_pdfrw: if with_pdfrw:
from pdfrw import PdfDict, PdfName, PdfObject, PdfString from pdfrw import PdfDict, PdfName, PdfObject, PdfString
from pdfrw.py23_diffs import convert_load from pdfrw.py23_diffs import convert_load
else: else:
@ -743,7 +740,7 @@ class pdfdoc(object):
elif color == Colorspace.CMYK or color == Colorspace["CMYK;I"]: elif color == Colorspace.CMYK or color == Colorspace["CMYK;I"]:
colorspace = PdfName.DeviceCMYK colorspace = PdfName.DeviceCMYK
elif color == Colorspace.P: elif color == Colorspace.P:
if self.with_pdfrw: if with_pdfrw:
raise Exception( raise Exception(
"pdfrw does not support hex strings for " "pdfrw does not support hex strings for "
"palette image input, re-run with " "palette image input, re-run with "
@ -871,7 +868,7 @@ class pdfdoc(object):
self.writer.addpage(page) self.writer.addpage(page)
if not self.with_pdfrw: if not with_pdfrw:
self.writer.addobj(content) self.writer.addobj(content)
self.writer.addobj(image) self.writer.addobj(image)
@ -881,7 +878,7 @@ class pdfdoc(object):
return stream.getvalue() return stream.getvalue()
def tostream(self, outputstream): def tostream(self, outputstream):
if self.with_pdfrw: if with_pdfrw:
from pdfrw import PdfDict, PdfName, PdfArray, PdfObject from pdfrw import PdfDict, PdfName, PdfArray, PdfObject
else: else:
PdfDict = MyPdfDict PdfDict = MyPdfDict
@ -899,7 +896,7 @@ class pdfdoc(object):
# is added, so we can only start using it after all pages have been # is added, so we can only start using it after all pages have been
# written. # written.
if self.with_pdfrw: if with_pdfrw:
catalog = self.writer.trailer.Root catalog = self.writer.trailer.Root
else: else:
catalog = self.writer.catalog catalog = self.writer.catalog
@ -1005,7 +1002,7 @@ class pdfdoc(object):
raise ValueError("unknown page layout: %s" % self.page_layout) raise ValueError("unknown page layout: %s" % self.page_layout)
# now write out the PDF # now write out the PDF
if self.with_pdfrw: if with_pdfrw:
self.writer.trailer.Info = self.info self.writer.trailer.Info = self.info
self.writer.write(outputstream) self.writer.write(outputstream)
else: else:
@ -1809,7 +1806,6 @@ def convert(*images, **kwargs):
viewer_fit_window=False, viewer_fit_window=False,
viewer_center_window=False, viewer_center_window=False,
viewer_fullscreen=False, viewer_fullscreen=False,
with_pdfrw=True,
outputstream=None, outputstream=None,
first_frame_only=False, first_frame_only=False,
allow_oversized=True, allow_oversized=True,
@ -1821,6 +1817,9 @@ def convert(*images, **kwargs):
for kwname, default in _default_kwargs.items(): for kwname, default in _default_kwargs.items():
if kwname not in kwargs: if kwname not in kwargs:
kwargs[kwname] = default kwargs[kwname] = default
if 'with_pdfrw' in kwargs:
global with_pdfrw
with_pdfrw = kwargs['with_pdfrw']
pdf = pdfdoc( pdf = pdfdoc(
"1.3", "1.3",
@ -1840,7 +1839,6 @@ def convert(*images, **kwargs):
kwargs["viewer_fit_window"], kwargs["viewer_fit_window"],
kwargs["viewer_center_window"], kwargs["viewer_center_window"],
kwargs["viewer_fullscreen"], kwargs["viewer_fullscreen"],
kwargs["with_pdfrw"],
) )
# backwards compatibility with older img2pdf versions where the first # backwards compatibility with older img2pdf versions where the first
@ -2492,6 +2490,8 @@ def gui():
v for v in Colorspace if v.name == args["colorspace"].get() v for v in Colorspace if v.name == args["colorspace"].get()
) )
global with_pdfrw
with_pdfrw = (not args["without_pdfrw"].get(),)
convert( convert(
*infiles, *infiles,
title=args["title"].get() if args["title"].get() else None, title=args["title"].get() if args["title"].get() else None,
@ -2516,7 +2516,6 @@ def gui():
viewer_fit_window=(args["viewer_fit_window"].get() or None), viewer_fit_window=(args["viewer_fit_window"].get() or None),
viewer_center_window=(args["viewer_center_window"].get() or None), viewer_center_window=(args["viewer_center_window"].get() or None),
viewer_fullscreen=(args["viewer_fullscreen"].get() or None), viewer_fullscreen=(args["viewer_fullscreen"].get() or None),
with_pdfrw=not args["without_pdfrw"].get(),
outputstream=stream, outputstream=stream,
first_frame_only=args["first_frame_only"].get(), first_frame_only=args["first_frame_only"].get(),
cropborder=None, cropborder=None,
@ -3490,6 +3489,8 @@ and left/right, respectively. It is not possible to specify asymmetric borders.
) )
exit(2) exit(2)
global with_pdfrw
with_pdfrw = (not args.without_pdfrw,)
try: try:
convert( convert(
*args.images, *args.images,
@ -3511,7 +3512,6 @@ and left/right, respectively. It is not possible to specify asymmetric borders.
viewer_fit_window=args.viewer_fit_window, viewer_fit_window=args.viewer_fit_window,
viewer_center_window=args.viewer_center_window, viewer_center_window=args.viewer_center_window,
viewer_fullscreen=args.viewer_fullscreen, viewer_fullscreen=args.viewer_fullscreen,
with_pdfrw=not args.without_pdfrw,
outputstream=args.output, outputstream=args.output,
first_frame_only=args.first_frame_only, first_frame_only=args.first_frame_only,
cropborder=args.crop_border, cropborder=args.crop_border,

View file

@ -554,12 +554,17 @@ def test_suite():
assert os.path.isfile(outputf) assert os.path.isfile(outputf)
def handle(self, f=inputf, out=outputf, with_pdfrw=with_pdfrw): def handle(self, f=inputf, out=outputf, with_pdfrw=with_pdfrw):
try:
from pdfrw import PdfReader, PdfName, PdfWriter
from pdfrw.py23_diffs import convert_load, convert_store
except ImportError:
# the test requires pdfrw
self.skipTest("this test requires pdfrw")
return
with open(f, "rb") as inf: with open(f, "rb") as inf:
orig_imgdata = inf.read() orig_imgdata = inf.read()
output = img2pdf.convert(orig_imgdata, nodate=True, output = img2pdf.convert(orig_imgdata, nodate=True,
with_pdfrw=with_pdfrw) with_pdfrw=with_pdfrw)
from pdfrw import PdfReader, PdfName, PdfWriter
from pdfrw.py23_diffs import convert_load, convert_store
x = PdfReader(PdfReaderIO(convert_load(output))) x = PdfReader(PdfReaderIO(convert_load(output)))
self.assertEqual(sorted(x.keys()), [PdfName.Info, PdfName.Root, self.assertEqual(sorted(x.keys()), [PdfName.Info, PdfName.Root,
PdfName.Size]) PdfName.Size])