import os
import PIL
from reportlab.lib import utils
from reportlab.lib.utils import ImageReader
from reportlab.pdfgen import canvas
from image_split import ClipType
from reportlab.lib.pagesizes import A4, landscape, portrait
from PIL import Image
from reportlab.platypus import SimpleDocTemplate, Image, PageBreak
from reportlab.platypus import PageTemplate, Frame
# ----------------------------------------------------------------------
def create_pdf(filename, path, page, cliptype, imageformat):
"""
"""
if cliptype == ClipType.ALL:
imgDoc = canvas.Canvas(filename) # pagesize=letter
imgDoc.setPageSize(A4)
document_width, document_height = A4
for page in range(page):
print 'create_pdf', page
image_file = PIL.Image.open(os.path.join(path, '{0}.{1}.{2}'.format(page, '1', imageformat)))
image_width, image_height = image_file.size
if not (image_width > 0 and image_height > 0):
raise Exception
image_aspect = image_height / float(image_width)
print_width = document_width
print_height = document_width * image_aspect
imgDoc.drawImage(ImageReader(image_file), document_width - print_width,
document_height - print_height, width=print_width,
height=print_height, preserveAspectRatio=True)
# Story.append(Image(image_file, width, height))
imgDoc.showPage()
imgDoc.save()
print "%s created" % filename
# ----------------------------------------------------------------------
if __name__ == "__main__":
cliptype = ClipType.MARGIN_ONLY
pdfsavepath = r".\tmp\pdf\484d8037a509648a8e09fc0466b06f38\tongjixuexifangfa{0}.pdf".format(cliptype)
subimagepath = r".\tmp\pdf\484d8037a509648a8e09fc0466b06f38\subimage{0}".format(cliptype)
create_pdf(pdfsavepath, subimagepath, 20, cliptype, 'jpg')