def convert_to_pdf(os_dir, chapter, file_names): """ Converts a collection of images to PDF format :param os_dir: Directory to save PDF in. :param chapter: Title of the PDF. :param file_names: Images to construct the PDF from. :return: """ print("Converting chapter %s to pdf..." % chapter) pdf_bytes = None try: pdf_bytes = img2pdf.convert(*[download_image(path) for path in file_names]) except img2pdf.PdfTooLargeError: # Sometimes the images are registered as having a dpi of 1. # Because PDF has a limitation of 200 inches max per side, a # special layout_fun has to be used, as to prevent an exception. # default manga size 5"x7" layout_fun = img2pdf.get_layout_fun(pagesize=(None, img2pdf.in_to_pt(7)), imgsize=None, border=None, fit=img2pdf.FitMode.into, auto_orient=False) pdf_bytes = img2pdf.convert(*[download_image(path) for path in file_names], layout_fun=layout_fun) file = open("%s/%s.pdf" % (os_dir, chapter), "wb") file.write(pdf_bytes) print("Conversion completed!")
def convert_to_pdf(os_dir, chapter, file_names): """ Converts a collection of images to PDF format :param os_dir: Directory to save PDF in. :param chapter: Title of the PDF. :param file_names: Images to construct the PDF from. :return: """ print("Converting chapter %s to pdf..." % chapter) pdf_bytes = None try: pdf_bytes = img2pdf.convert( *[download_image(path) for path in file_names]) except img2pdf.PdfTooLargeError: # Sometimes the images are registered as having a dpi of 1. # Because PDF has a limitation of 200 inches max per side, a # special layout_fun has to be used, as to prevent an exception. # default manga size 5"x7" layout_fun = img2pdf.get_layout_fun(pagesize=(None, img2pdf.in_to_pt(7)), imgsize=None, border=None, fit=img2pdf.FitMode.into, auto_orient=False) pdf_bytes = img2pdf.convert( *[download_image(path) for path in file_names], layout_fun=layout_fun) file = open("%s/%s.pdf" % (os_dir, chapter), "wb") file.write(pdf_bytes) print("Conversion completed!")
def batch_convert_img2pdf(images_path, pdf_pathzc): letter = (img2pdf.in_to_pt(8.5), img2pdf.in_to_pt(11)) layout = img2pdf.get_layout_fun(letter) with open('test.pdf', 'wb') as f: f.write(img2pdf.convert(images_path, layout_fun=layout))
def go_img2pdf(file, to_folder, new_name): a4_page_size = [img2pdf.in_to_pt(8.3), img2pdf.in_to_pt(11.7)] layout_function = img2pdf.get_layout_fun(a4_page_size) pdf = img2pdf.convert(file, layout_fun=layout_function) with open(to_folder + "/" + new_name, 'wb') as f: f.write(pdf)
# importing necessary libraries import img2pdf import os import argparse #set layout letter = (img2pdf.in_to_pt(11), img2pdf.in_to_pt(8.5)) layout = img2pdf.get_layout_fun(letter) # with open('tile/test.pdf', "wb") as out_file: # images = [] # for i in range(0, 31 + 1): # fname = str(i) + '_1.png' # images.append(fname) # print(fname) # out_file.write(img2pdf.convert(images)) def process_images(min_range, max_range, prefix, suffix, out_file, layout): images = [] for i in range(min_range, max_range + 1): fname = prefix + str(i) + suffix images.append(fname) out_file.write(img2pdf.convert(images, layout_fun=layout)) with open('tile/img2pdf-2-2.pdf', "wb") as out_file: process_images(0, 31, 'tile/', '_2.png', out_file, layout) # directory = 'tile/'