#PDF TO IMAGE CONVERSION
#IMPORT LIBRARIES
import pdf2image
from PIL import Image
import time
import sys


#DECLARE CONSTANTS
PDF_PATH = sys.argv[1]
INDEX = int(sys.argv[2])
DPI = 200
OUTPUT_FOLDER = "images"
FIRST_PAGE = None
LAST_PAGE = None
FORMAT = 'jpg'
THREAD_COUNT = 1
USERPWD = <PASSWORD>
USE_CROPBOX = False
STRICT = False

roll = str(INDEX) + "-" + str(int(time.time())); 

def pdftopil():
    #This method reads a pdf and converts it into a sequence of images
    #PDF_PATH sets the path to the PDF file
    #dpi parameter assists in adjusting the resolution of the image
    #output_folder parameter sets the path to the folder to which the PIL images can be stored (optional)
    #first_page parameter allows you to set a first page to be processed by pdftoppm 
    #last_page parameter allows you to set a last page to be processed by pdftoppm
    #fmt parameter allows to set the format of pdftoppm conversion (PpmImageFile, TIFF)
    #thread_count parameter allows you to set how many thread will be used for conversion.
    #userpw parameter allows you to set a password to unlock the converted PDF
    #use_cropbox parameter allows you to use the crop box instead of the media box when converting
    #strict parameter allows you to catch pdftoppm syntax error with a custom type PDFSyntaxError
    start_time = time.time()
    pil_images = pdf2image.convert_from_path(PDF_PATH, dpi=DPI, output_folder=OUTPUT_FOLDER, first_page=FIRST_PAGE, last_page=LAST_PAGE, fmt=FORMAT, thread_count=THREAD_COUNT, userpw=USERPWD, use_cropbox=USE_CROPBOX, strict=STRICT, output_file=roll)
    return pil_images
    
def save_images(pil_images):
    #This method helps in converting the images in PIL Image file format to the required image format
    index = 1
    #filename = roll + "-" + str(index
    for image in pil_images:
        #image.save("page_" + str(index) + ".jpg")
        filename = roll + "0001-" + str(index)
        print('http://localhost:3000/' + filename+'.jpg')
        index = index + 1
    print(roll+'0001-')
        

if __name__ == "__main__":
    pil_images = pdftopil()
    save_images(pil_images)
#PDF TO IMAGE CONVERSION
#IMPORT LIBRARIES
import pdf2image
from PIL import Image
import time
import sys


#DECLARE CONSTANTS
PDF_PATH = sys.argv[1]
INDEX = int(sys.argv[2])
DPI = 200
OUTPUT_FOLDER = "images"
FIRST_PAGE = None
LAST_PAGE = None
FORMAT = 'jpg'
THREAD_COUNT = 1
USERPWD = <PASSWORD>
USE_CROPBOX = False
STRICT = False

roll = str(INDEX) + "-" + str(int(time.time())); 

def pdftopil():
    #This method reads a pdf and converts it into a sequence of images
    #PDF_PATH sets the path to the PDF file
    #dpi parameter assists in adjusting the resolution of the image
    #output_folder parameter sets the path to the folder to which the PIL images can be stored (optional)
    #first_page parameter allows you to set a first page to be processed by pdftoppm 
    #last_page parameter allows you to set a last page to be processed by pdftoppm
    #fmt parameter allows to set the format of pdftoppm conversion (PpmImageFile, TIFF)
    #thread_count parameter allows you to set how many thread will be used for conversion.
    #userpw parameter allows you to set a password to unlock the converted PDF
    #use_cropbox parameter allows you to use the crop box instead of the media box when converting
    #strict parameter allows you to catch pdftoppm syntax error with a custom type PDFSyntaxError
    start_time = time.time()
    pil_images = pdf2image.convert_from_path(PDF_PATH, dpi=DPI, output_folder=OUTPUT_FOLDER, first_page=FIRST_PAGE, last_page=LAST_PAGE, fmt=FORMAT, thread_count=THREAD_COUNT, userpw=USERPWD, use_cropbox=USE_CROPBOX, strict=STRICT, output_file=roll)
    return pil_images
    
def save_images(pil_images):
    #This method helps in converting the images in PIL Image file format to the required image format
    index = 1
    #filename = roll + "-" + str(index
    for image in pil_images:
        #image.save("page_" + str(index) + ".jpg")
        filename = roll + "0001-" + str(index)
        print('http://localhost:3000/' + filename+'.jpg')
        index = index + 1
    print(roll+'0001-')
        

if __name__ == "__main__":
    pil_images = pdftopil()
    save_images(pil_images)
#PDF TO IMAGE CONVERSION #IMPORT LIBRARIES #DECLARE CONSTANTS #This method reads a pdf and converts it into a sequence of images #PDF_PATH sets the path to the PDF file #dpi parameter assists in adjusting the resolution of the image #output_folder parameter sets the path to the folder to which the PIL images can be stored (optional) #first_page parameter allows you to set a first page to be processed by pdftoppm #last_page parameter allows you to set a last page to be processed by pdftoppm #fmt parameter allows to set the format of pdftoppm conversion (PpmImageFile, TIFF) #thread_count parameter allows you to set how many thread will be used for conversion. #userpw parameter allows you to set a password to unlock the converted PDF #use_cropbox parameter allows you to use the crop box instead of the media box when converting #strict parameter allows you to catch pdftoppm syntax error with a custom type PDFSyntaxError #This method helps in converting the images in PIL Image file format to the required image format #filename = roll + "-" + str(index #image.save("page_" + str(index) + ".jpg")