1
0
Fork 0
forked from josch/img2pdf

Produce port that is fully API compatible.

This commit is contained in:
Stephan Richter 2017-05-22 12:43:27 -04:00 committed by Johannes 'josch' Schauer
parent 06560cd0d4
commit f626d7860f

View file

@ -991,20 +991,30 @@ def get_fixed_dpi_layout_fun(fixed_dpi):
# Input images can be given as file like objects (they must implement read()), # Input images can be given as file like objects (they must implement read()),
# as a binary string representing the image content or as filenames to the # as a binary string representing the image content or as filenames to the
# images. # images.
def convert(*images, title=None, def convert(*images, **kwargs):
author=None, creator=None, producer=None, creationdate=None,
moddate=None, subject=None, keywords=None, colorspace=None,
nodate=False, layout_fun=default_layout_fun, viewer_panes=None,
viewer_initial_page=None, viewer_magnification=None,
viewer_page_layout=None, viewer_fit_window=False,
viewer_center_window=False, viewer_fullscreen=False,
with_pdfrw=True, outputstream=None, first_frame_only=False):
pdf = pdfdoc("1.3", title, author, creator, producer, creationdate, _default_kwargs = dict(
moddate, subject, keywords, nodate, viewer_panes, title=None,
viewer_initial_page, viewer_magnification, viewer_page_layout, author=None, creator=None, producer=None, creationdate=None,
viewer_fit_window, viewer_center_window, viewer_fullscreen, moddate=None, subject=None, keywords=None, colorspace=None,
with_pdfrw) nodate=False, layout_fun=default_layout_fun, viewer_panes=None,
viewer_initial_page=None, viewer_magnification=None,
viewer_page_layout=None, viewer_fit_window=False,
viewer_center_window=False, viewer_fullscreen=False,
with_pdfrw=True, outputstream=None, first_frame_only=False)
for kwname, default in _default_kwargs.items():
if kwname not in kwargs:
kwargs[kwname] = default
pdf = pdfdoc(
"1.3",
kwargs['title'], kwargs['author'], kwargs['creator'],
kwargs['producer'], kwargs['creationdate'], kwargs['moddate'],
kwargs['subject'], kwargs['keywords'], kwargs['nodate'],
kwargs['viewer_panes'], kwargs['viewer_initial_page'],
kwargs['viewer_magnification'], kwargs['viewer_page_layout'],
kwargs['viewer_fit_window'], kwargs['viewer_center_window'],
kwargs['viewer_fullscreen'], kwargs['with_pdfrw'])
# backwards compatibility with older img2pdf versions where the first # backwards compatibility with older img2pdf versions where the first
# argument to the function had to be given as a list # argument to the function had to be given as a list
@ -1013,6 +1023,9 @@ def convert(*images, title=None,
if isinstance(images[0], (list, tuple)): if isinstance(images[0], (list, tuple)):
images = images[0] images = images[0]
if not isinstance(images, (list, tuple)):
images = [images]
for img in images: for img in images:
# img is allowed to be a path, a binary string representing image data # img is allowed to be a path, a binary string representing image data
# or a file-like object (really anything that implements read()) # or a file-like object (really anything that implements read())
@ -1034,9 +1047,10 @@ def convert(*images, title=None,
rawdata = img rawdata = img
for color, ndpi, imgformat, imgdata, imgwidthpx, imgheightpx \ for color, ndpi, imgformat, imgdata, imgwidthpx, imgheightpx \
in read_images(rawdata, colorspace, first_frame_only): in read_images(
rawdata, kwargs['colorspace'], kwargs['first_frame_only']):
pagewidth, pageheight, imgwidthpdf, imgheightpdf = \ pagewidth, pageheight, imgwidthpdf, imgheightpdf = \
layout_fun(imgwidthpx, imgheightpx, ndpi) kwargs['layout_fun'](imgwidthpx, imgheightpx, ndpi)
if pagewidth < 3.00 or pageheight < 3.00: if pagewidth < 3.00 or pageheight < 3.00:
logging.warning("pdf width or height is below 3.00 - too " logging.warning("pdf width or height is below 3.00 - too "
"small for some viewers!") "small for some viewers!")
@ -1050,8 +1064,8 @@ def convert(*images, title=None,
imgdata, imgwidthpdf, imgheightpdf, imgxpdf, imgdata, imgwidthpdf, imgheightpdf, imgxpdf,
imgypdf, pagewidth, pageheight) imgypdf, pagewidth, pageheight)
if outputstream: if kwargs['outputstream']:
pdf.tostream(outputstream) pdf.tostream(kwargs['outputstream'])
return return
return pdf.tostring() return pdf.tostring()