def print_sh1_line( f3x_data, md5_directory, tran_type_ident, sh_h1, sh1_page_cnt, sh1_start_page, total_no_of_pages, image_num=None, ): # presidentialOnly = presidentialAndSenate = senateOnly = nonPresidentialAndNonSenate = False try: os.makedirs(md5_directory + "SH1/" + tran_type_ident, exist_ok=True) sh1_infile = current_app.config["FORM_TEMPLATES_LOCATION"].format( "SH1") sh1_page_no = 1 if sh1_page_cnt > 0: sh1_schedule_page_dict = {} # sh1_schedule_page_dict['pageNo'] = sh1_start_page + 1 sh1_schedule_page_dict["PAGESTR"] = ("PAGE " + str(sh1_start_page + 1) + " / " + str(total_no_of_pages)) if image_num: sh1_schedule_page_dict["IMGNO"] = image_num image_num += 1 # sh1_schedule_page_dict['totalPages'] = total_no_of_pages for sh1_line in sh_h1: presidentialOnly = sh1_line["presidentialOnly"] presidentialAndSenate = sh1_line["presidentialAndSenate"] senateOnly = sh1_line["senateOnly"] nonPresidentialAndNonSenate = sh1_line[ "nonPresidentialAndNonSenate"] if (presidentialOnly or presidentialAndSenate or senateOnly or nonPresidentialAndNonSenate): sh1_schedule_page_dict["presidentialOnly"] = str( sh1_line["presidentialOnly"]) sh1_schedule_page_dict["presidentialAndSenate"] = str( sh1_line["presidentialAndSenate"]) sh1_schedule_page_dict["senateOnly"] = str( sh1_line["senateOnly"]) sh1_schedule_page_dict[ "nonPresidentialAndNonSenate"] = str( sh1_line["nonPresidentialAndNonSenate"]) else: sh1_schedule_page_dict[ "federalPercent"] = "{0:.2f}".format( float(sh1_line["federalPercent"])) sh1_schedule_page_dict[ "nonFederalPercent"] = "{0:.2f}".format( float(sh1_line["nonFederalPercent"])) sh1_schedule_page_dict["administrative"] = str( sh1_line["administrative"]) sh1_schedule_page_dict["genericVoterDrive"] = str( sh1_line["genericVoterDrive"]) sh1_schedule_page_dict["publicCommunications"] = str( sh1_line["publicCommunications"]) sh1_schedule_page_dict["committeeName"] = f3x_data[ "committeeName"] sh1_outfile = (md5_directory + "SH1/" + tran_type_ident + "/page_" + str(sh1_page_no) + ".pdf") pypdftk.fill_form(sh1_infile, sh1_schedule_page_dict, sh1_outfile) sh1_page_no += 1 pypdftk.concat( directory_files(md5_directory + "SH1/" + tran_type_ident + "/"), md5_directory + "SH1/" + tran_type_ident + "/all_pages.pdf", ) if path.isfile(md5_directory + "SH1/all_pages.pdf"): pypdftk.concat( [ md5_directory + "SH1/all_pages.pdf", md5_directory + "SH1/" + tran_type_ident + "/all_pages.pdf", ], md5_directory + "SH1/temp_all_pages.pdf", ) os.rename( md5_directory + "SH1/temp_all_pages.pdf", md5_directory + "SH1/all_pages.pdf", ) else: os.rename( md5_directory + "SH1/" + tran_type_ident + "/all_pages.pdf", md5_directory + "SH1/all_pages.pdf", ) return image_num except Exception as e: raise e
def print_se_line( f3x_data, md5_directory, line_number, se_list, page_cnt, current_page_num, total_no_of_pages, image_num=None, ): try: if se_list: last_page_cnt = 2 if len(se_list) % 2 == 0 else len(se_list) % 2 schedule_total = 0 os.makedirs(md5_directory + "SE/" + line_number, exist_ok=True) se_infile = current_app.config["FORM_TEMPLATES_LOCATION"].format( "SE") for page_num in range(page_cnt): current_page_num += 1 memo_array = [] last_page = False schedule_page_dict = {} schedule_page_dict["lineNumber"] = line_number schedule_page_dict["pageNo"] = current_page_num schedule_page_dict["totalPages"] = total_no_of_pages if image_num: schedule_page_dict["IMGNO"] = image_num image_num += 1 page_start_index = page_num * 2 if page_num + 1 == page_cnt: last_page = True # This call prepares data to render on PDF build_se_per_page_schedule_dict( last_page, last_page_cnt, page_start_index, schedule_page_dict, se_list, memo_array, ) schedule_total += float(schedule_page_dict["pageSubtotal"]) if page_cnt == page_num + 1: schedule_page_dict["pageTotal"] = "{0:.2f}".format( schedule_total) schedule_page_dict["committeeName"] = f3x_data["committeeName"] schedule_page_dict["committeeId"] = f3x_data["committeeId"] # checking for signed date, it is only available for submitted reports # and adding in date signed and treasurer name for signed reports if len(f3x_data["dateSigned"]) > 0: date_signed_array = f3x_data["dateSigned"].split("/") schedule_page_dict["dateSignedMonth"] = date_signed_array[ 0] schedule_page_dict["dateSignedDay"] = date_signed_array[1] schedule_page_dict["dateSignedYear"] = date_signed_array[2] schedule_page_dict["completingName"] = f3x_data[ "treasurerName"] se_outfile = (md5_directory + "SE/" + line_number + "/page_" + str(page_num) + ".pdf") pypdftk.fill_form(se_infile, schedule_page_dict, se_outfile) # Memo text changes memo_dict = {} if len(memo_array) >= 1: current_page_num += 1 temp_memo_outfile = (md5_directory + "SE/" + line_number + "/page_memo_temp.pdf") memo_infile = current_app.config[ "FORM_TEMPLATES_LOCATION"].format("TEXT") memo_outfile = (md5_directory + "SE/" + line_number + "/page_memo_" + str(page_num) + ".pdf") memo_dict["scheduleName_1"] = memo_array[0]["scheduleName"] memo_dict["memoDescription_1"] = memo_array[0][ "memoDescription"] memo_dict["transactionId_1"] = memo_array[0][ "transactionId"] memo_dict["PAGESTR"] = ("PAGE " + str(current_page_num) + " / " + str(total_no_of_pages)) if image_num: memo_dict["IMGNO"] = image_num image_num += 1 if len(memo_array) >= 2: memo_dict["scheduleName_2"] = memo_array[1][ "scheduleName"] memo_dict["memoDescription_2"] = memo_array[1][ "memoDescription"] memo_dict["transactionId_2"] = memo_array[1][ "transactionId"] # build page pypdftk.fill_form(memo_infile, memo_dict, memo_outfile) pypdftk.concat([se_outfile, memo_outfile], temp_memo_outfile) os.remove(memo_outfile) os.rename(temp_memo_outfile, se_outfile) pypdftk.concat( directory_files(md5_directory + "SE/" + line_number + "/"), md5_directory + "SE/" + line_number + "/all_pages.pdf", ) if path.isfile(md5_directory + "SE/all_pages.pdf"): pypdftk.concat( [ md5_directory + "SE/all_pages.pdf", md5_directory + "SE/" + line_number + "/all_pages.pdf", ], md5_directory + "SE/temp_all_pages.pdf", ) os.rename( md5_directory + "SE/temp_all_pages.pdf", md5_directory + "SE/all_pages.pdf", ) else: os.rename( md5_directory + "SE/" + line_number + "/all_pages.pdf", md5_directory + "SE/all_pages.pdf", ) return current_page_num, image_num except: traceback.print_exception(*sys.exc_info())
def print_f99_pdftk_html( stamp_print="", paginate=False, begin_image_num=None, page_count=False, file_content=None, silent_print=False, filing_timestamp=None, rep_id=None, attachment_file_content=None, ): # check if json_file is in the request # HTML("templates/forms/test.html").write_pdf("output/pdf/test/test.pdf") # HTML(string='''<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><div><b>This is bold text</b></div><div><u>This is underline text</u></div><div><i>This is italics text</i><u><br></u></div><div align='center'><u>Title</u></div><div align='left'><u><br></u></div><ol><li>one</li><li>two</li><li>three</li></ol>''').write_pdf("output/pdf/test/test.pdf") # pdfkit.from_file("templates/forms/test.html", "output/pdf/test/test.pdf") # pypdftk.stamp(current_app.config['FORM_TEMPLATES_LOCATION'].format('F99'), "output/pdf/test/test.pdf", "output/pdf/test/output.pdf") try: silent_print = silent_print txn_img_num = begin_image_num filing_timestamp = filing_timestamp if ((page_count and file_content) or ((paginate or silent_print) and file_content and begin_image_num) or (not paginate and "json_file" in request.files)): if page_count and file_content: json_file_md5 = md5_for_text(file_content) json_data = json.loads(file_content) elif (paginate or silent_print) and file_content and begin_image_num: # generate md5 for file_content json_file_md5 = md5_for_text(file_content) json_data = json.loads(file_content) elif not paginate and "json_file" in request.files: json_file = request.files.get("json_file") silent_print = (True if request.form.get("silent_print") and request.form.get("silent_print").lower() in ["true", "1"] else False) page_count = (True if request.form.get("page_count") and request.form.get("page_count").lower() in ["true", "1"] else False) if silent_print: txn_img_num = request.form.get("begin_image_num", None) if not txn_img_num: if flask.request.method == "POST": envelope = common.get_return_envelope( "false", "begin_image_num is missing from your request") status_code = status.HTTP_400_BAD_REQUEST return flask.jsonify(**envelope), status_code txn_img_num = int(txn_img_num) filing_timestamp = request.form.get( "filing_timestamp", None) json_file_md5 = md5_for_file(json_file) json_file.stream.seek(0) # save json file as md5 file name json_file.save( current_app.config["REQUEST_FILE_LOCATION"].format( json_file_md5)) # load json file json_data = json.load( open(current_app.config["REQUEST_FILE_LOCATION"].format( json_file_md5))) md5_directory = current_app.config["OUTPUT_DIR_LOCATION"].format( json_file_md5) # if paginate or page_count is True and directory exist then don't remove it is_dir_exist = False if os.path.isdir(md5_directory): is_dir_exist = True os.makedirs(md5_directory, exist_ok=True) # os.makedirs(md5_directory + "images", exist_ok=True) if not os.path.exists(md5_directory + "images"): shutil.copytree("templates/forms/F99/images", md5_directory + "images") shutil.copyfile("templates/forms/F99/form-text.css", md5_directory + "form-text.css") infile = current_app.config["HTML_FORM_TEMPLATES_LOCATION"].format( "template") outfile = md5_directory + json_file_md5 + ".html" form99_json_data = json_data["data"] with open(infile) as inf: txt = inf.read() soup = bs4.BeautifulSoup(txt, features="html5lib") soup.find("label", attrs={ "id": "committeeName" }).string = form99_json_data["committeeName"] soup.find("label", attrs={ "id": "street1" }).string = form99_json_data["street1"] soup.find("label", attrs={ "id": "street2" }).string = form99_json_data["street2"] soup.find("label", attrs={ "id": "city" }).string = form99_json_data["city"] soup.find("label", attrs={ "id": "state" }).string = form99_json_data["state"] soup.find("label", attrs={ "id": "zipCode" }).string = form99_json_data["zipCode"] soup.find("span", attrs={ "id": "committeeId" }).string = form99_json_data["committeeId"] name_list = [ "LastName", "FirstName", "MiddleName", "Prefix", "Suffix" ] treasurerFullName = "" for item in name_list: item = "treasurer" + item if form99_json_data.get(item): treasurerFullName += form99_json_data.get(item) + ", " soup.find("label", attrs={ "id": "treasurerFullName" }).string = treasurerFullName[:-2] soup.find("label", attrs={ "id": "treasurerName" }).string = ((form99_json_data.get("treasurerLastName", "") + ", " + form99_json_data.get("treasurerFirstName", "") ).strip().rstrip(",").strip()) f99_html_data = form99_json_data["text"] soup.find("label", attrs={"id": "text"}).string = f99_html_data soup.find("label", attrs={ "id": form99_json_data["reason"] }).string = "X" date_array = form99_json_data["dateSigned"].split("/") soup.find("span", attrs={ "id": "dateSignedMonth" }).string = str(date_array[0]) soup.find("span", attrs={ "id": "dateSignedDate" }).string = str(date_array[1]) soup.find("span", attrs={ "id": "dateSignedYear" }).string = str(date_array[2]) with open(outfile, "w") as output_file: output_file.write( str(soup).replace("<", "<").replace(">", ">")) # F99 PDF page padding options options = { "margin-top": "0.40in", "margin-right": "0.20in", "margin-bottom": "0.40in", "margin-left": "0.20in", } # HTML(outfile).write_pdf(md5_directory + json_file_md5 + '.pdf', stylesheets=[CSS(current_app.config['FORMS_LOCATION'].format('F99.css'))]) pdfkit.from_file(outfile, md5_directory + json_file_md5 + ".pdf", options=options) # pdfkit.from_file(outfile, md5_directory + json_file_md5 + '.pdf') total_no_of_pages = pypdftk.get_num_pages(md5_directory + json_file_md5 + ".pdf") # checking if attachment_file exist if ((paginate or page_count) and attachment_file_content) or ( not paginate and "attachment_file" in request.files): # reading Attachment title file attachment_title_file = current_app.config[ "FORM_TEMPLATES_LOCATION"].format("Attachment_Title") if (paginate or page_count) and attachment_file_content: attachment_file = json.loads(attachment_file_content) else: attachment_file = request.files.get("attachment_file") attachment_file.save( os.path.join(md5_directory + "attachment_temp.pdf")) os.makedirs(md5_directory + "attachment", exist_ok=True) os.makedirs(md5_directory + "final_attachment", exist_ok=True) pypdftk.split(md5_directory + "attachment_temp.pdf", md5_directory + "attachment") os.remove(md5_directory + "attachment/doc_data.txt") attachment_no_of_pages = pypdftk.get_num_pages( os.path.join(md5_directory + "attachment_temp.pdf")) attachment_page_no = total_no_of_pages total_no_of_pages += attachment_no_of_pages # we are doing this to assign page numbers to attachment file for filename in os.listdir(md5_directory + "attachment"): attachment_page_no += 1 page_dict = {} page_dict["PAGESTR"] = ("PAGE " + str(attachment_page_no) + " / " + str(total_no_of_pages)) if silent_print: page_dict["IMGNO"] = txn_img_num + attachment_page_no pypdftk.fill_form( attachment_title_file, md5_directory + "attachment/attachment_page_" + str(attachment_page_no) + ".pdf", ) pypdftk.stamp( md5_directory + "attachment/" + filename, md5_directory + "attachment/attachment_page_" + str(attachment_page_no) + ".pdf", md5_directory + "final_attachment/attachment_" + str(attachment_page_no) + ".pdf", ) pypdftk.concat( directory_files(md5_directory + "final_attachment/"), md5_directory + "attachment.pdf", ) os.remove(md5_directory + "attachment_temp.pdf") os.makedirs(md5_directory + "pages", exist_ok=True) os.makedirs(md5_directory + "final_pages", exist_ok=True) pypdftk.split(md5_directory + json_file_md5 + ".pdf", md5_directory + "pages") os.remove(md5_directory + "pages/doc_data.txt") f99_page_no = 1 for filename in os.listdir(md5_directory + "pages"): page_dict = {} page_dict["PAGESTR"] = ("PAGE " + str(f99_page_no) + " / " + str(total_no_of_pages)) if silent_print: page_dict["IMGNO"] = txn_img_num txn_img_num += 1 # need to print timestamp on first page only if filing_timestamp and f99_page_no == 1: page_dict["FILING_TIMESTAMP"] = filing_timestamp page_number_file = current_app.config[ "FORM_TEMPLATES_LOCATION"].format("Page_Number") pypdftk.fill_form( page_number_file, page_dict, md5_directory + "pages/page_number_" + str(f99_page_no).zfill(6) + ".pdf", ) pypdftk.stamp( md5_directory + "pages/page_number_" + str(f99_page_no).zfill(6) + ".pdf", md5_directory + "pages/" + filename, md5_directory + "final_pages/page_" + str(f99_page_no).zfill(6) + ".pdf", ) f99_page_no += 1 pypdftk.concat( directory_files(md5_directory + "final_pages/"), json_file_md5 + "_temp.pdf", ) if ((paginate or page_count) and attachment_file_content) or ( not paginate and "attachment_file" in request.files): pypdftk.concat( [ json_file_md5 + "_temp.pdf", md5_directory + "attachment.pdf" ], md5_directory + "all_pages.pdf", ) shutil.rmtree(md5_directory + "attachment") shutil.rmtree(md5_directory + "final_attachment") os.remove(md5_directory + "attachment.pdf") else: shutil.move(json_file_md5 + "_temp.pdf", md5_directory + "all_pages.pdf") # clean up task shutil.rmtree(md5_directory + "pages") shutil.rmtree(md5_directory + "final_pages") os.remove(md5_directory + json_file_md5 + ".pdf") # if flask.request.method == "POST": response = { # 'file_name': ent_app.conf'{}.pdf'.format(json_file_md5), "total_pages": total_no_of_pages, } if not page_count and not paginate: s3 = boto3.client("s3") extraArgs = { "ContentType": "application/pdf", "ACL": "public-read" } if silent_print: response["pdf_url"] = current_app.config[ 'S3_FILE_URL'] + rep_id + '.pdf' s3.upload_file( md5_directory + 'all_pages.pdf', current_app. config['AWS_FECFILE_COMPONENTS_BUCKET_NAME'], current_app.config['AWS_FECFILE_OUTPUT_DIRECTORY'] + '/' + str(rep_id) + '.pdf', ExtraArgs=extraArgs) else: response["pdf_url"] = ( current_app.config["PRINT_OUTPUT_FILE_URL"].format( json_file_md5) + "all_pages.pdf", ) s3.upload_file( md5_directory + "all_pages.pdf", current_app. config["AWS_FECFILE_COMPONENTS_BUCKET_NAME"], md5_directory + "all_pages.pdf", ExtraArgs=extraArgs, ) else: if not is_dir_exist: shutil.rmtree(md5_directory) if paginate: txn_img_json = { "summary": { "committeeId": form99_json_data.get("committeeId", None), "begin_image_num": begin_image_num, "end_image_num": txn_img_num } } response["txn_img_json"] = txn_img_json envelope = common.get_return_envelope(data=response) status_code = (status.HTTP_200_OK if page_count or paginate else status.HTTP_201_CREATED) return flask.jsonify(**envelope), status_code # elif page_count or paginate: # if not is_dir_exist: # shutil.rmtree(md5_directory) # response = { # "total_pages": total_no_of_pages, # } # elif paginate: # txn_img_json = { # 'summary' : { # 'committeeId': form99_json_data.get('committeeId', None) # } # } # response['txn_img_json'] = txn_img_json # return True, response # elif silent_print and not flask.request.method == "POST": # return True, {} else: if paginate or page_count or silent_print: envelope = common.get_return_envelope(False, "") else: # elif flask.request.method == "POST": envelope = common.get_return_envelope( False, "json_file is missing from your request") return flask.jsonify(**envelope), status.HTTP_400_BAD_REQUEST except Exception as e: traceback.print_exception(*sys.exc_info()) return error("Error generating print preview, error message: " + str(e))
def print_sh5_line( f3x_data, md5_directory, line_number, sh5_list, page_cnt, current_page_num, total_no_of_pages, image_num=None, ): if sh5_list: last_page_cnt = 2 if len(sh5_list) % 2 == 0 else len(sh5_list) % 2 total_transferred_amt_subtotal = 0 total_voter_reg_amt_subtotal = 0 total_voter_id_amt_subtotal = 0 total_gotv_amt_subtotal = 0 total_generic_camp_amt_subtotal = 0 os.makedirs(md5_directory + "SH5/" + line_number, exist_ok=True) sh5_infile = current_app.config["FORM_TEMPLATES_LOCATION"].format("SH5") for sh5_page_no in range(page_cnt): current_page_num += 1 # page_subtotal = 0 memo_array = [] last_page = False schedule_page_dict = {} schedule_page_dict["lineNumber"] = line_number schedule_page_dict["pageNo"] = current_page_num schedule_page_dict["totalPages"] = total_no_of_pages if image_num: schedule_page_dict["IMGNO"] = image_num image_num += 1 page_start_index = sh5_page_no * 2 if sh5_page_no + 1 == page_cnt: last_page = True # This call prepares data to render on PDF build_sh5_per_page_schedule_dict( last_page, last_page_cnt, page_start_index, schedule_page_dict, sh5_list, memo_array, ) transferred_amt_subtotal = float( schedule_page_dict["subtotalAmountTransferred"] ) voter_reg_amt_subtotal = float( schedule_page_dict["subvoterRegistrationAmount"] ) voter_id_amt_subtotal = float(schedule_page_dict["subvoterIdAmount"]) gotv_amt_subtotal = float(schedule_page_dict["subgotvAmount"]) generic_camp_amt_subtotal = float( schedule_page_dict["subgenericCampaignAmount"] ) total_transferred_amt_subtotal += transferred_amt_subtotal total_voter_reg_amt_subtotal += voter_reg_amt_subtotal total_voter_id_amt_subtotal += voter_id_amt_subtotal total_gotv_amt_subtotal += gotv_amt_subtotal total_generic_camp_amt_subtotal += generic_camp_amt_subtotal if page_cnt == sh5_page_no + 1: schedule_page_dict["totalvoterRegistrationAmount"] = "{0:.2f}".format( total_voter_reg_amt_subtotal ) schedule_page_dict["totalvoterIdAmount"] = "{0:.2f}".format( total_voter_id_amt_subtotal ) schedule_page_dict["totalgotvAmount"] = "{0:.2f}".format( total_gotv_amt_subtotal ) schedule_page_dict["totalgenericCampaignAmount"] = "{0:.2f}".format( total_generic_camp_amt_subtotal ) schedule_page_dict["totalAmountOfTransfersReceived"] = ( total_voter_reg_amt_subtotal + total_voter_id_amt_subtotal + total_gotv_amt_subtotal + total_generic_camp_amt_subtotal ) schedule_page_dict["committeeName"] = f3x_data["committeeName"] sh5_outfile = ( md5_directory + "SH5/" + line_number + "/page_" + str(sh5_page_no) + ".pdf" ) pypdftk.fill_form(sh5_infile, schedule_page_dict, sh5_outfile) # Memo text changes memo_dict = {} if len(memo_array) >= 1: current_page_num += 1 temp_memo_outfile = ( md5_directory + "SH5/" + line_number + "/page_memo_temp.pdf" ) memo_infile = current_app.config["FORM_TEMPLATES_LOCATION"].format( "TEXT" ) memo_outfile = ( md5_directory + "SH5/" + line_number + "/page_memo_" + str(sh5_page_no) + ".pdf" ) memo_dict["scheduleName_1"] = memo_array[0]["scheduleName"] memo_dict["memoDescription_1"] = memo_array[0]["memoDescription"] memo_dict["transactionId_1"] = memo_array[0]["transactionId"] memo_dict["PAGESTR"] = ( "PAGE " + str(current_page_num) + " / " + str(total_no_of_pages) ) if image_num: memo_dict["IMGNO"] = image_num image_num += 1 if len(memo_array) >= 2: memo_dict["scheduleName_2"] = memo_array[1]["scheduleName"] memo_dict["memoDescription_2"] = memo_array[1]["memoDescription"] memo_dict["transactionId_2"] = memo_array[1]["transactionId"] # build page pypdftk.fill_form(memo_infile, memo_dict, memo_outfile) pypdftk.concat([sh5_outfile, memo_outfile], temp_memo_outfile) os.remove(memo_outfile) os.rename(temp_memo_outfile, sh5_outfile) pypdftk.concat( directory_files(md5_directory + "SH5/" + line_number + "/"), md5_directory + "SH5/" + line_number + "/all_pages.pdf", ) if path.isfile(md5_directory + "SH5/all_pages.pdf"): pypdftk.concat( [ md5_directory + "SH5/all_pages.pdf", md5_directory + "SH5/" + line_number + "/all_pages.pdf", ], md5_directory + "SH5/temp_all_pages.pdf", ) os.rename( md5_directory + "SH5/temp_all_pages.pdf", md5_directory + "SH5/all_pages.pdf", ) else: os.rename( md5_directory + "SH5/" + line_number + "/all_pages.pdf", md5_directory + "SH5/all_pages.pdf", ) return image_num
def print_slb_line( f3x_data, md5_directory, line_number, slb_list, page_cnt, current_page_num, total_no_of_pages, image_num=None, ): try: if slb_list: last_page_cnt = 5 if len(slb_list) % 5 == 0 else len(slb_list) % 5 schedule_total = 0 os.makedirs(md5_directory + "SL-B/" + line_number, exist_ok=True) slb_infile = current_app.config["FORM_TEMPLATES_LOCATION"].format( "SL-B") for page_num in range(page_cnt): current_page_num += 1 page_subtotal = 0 memo_array = [] last_page = False schedule_page_dict = {} schedule_page_dict["lineNumber"] = line_number schedule_page_dict["pageNo"] = current_page_num schedule_page_dict["totalPages"] = total_no_of_pages if image_num: schedule_page_dict["IMGNO"] = image_num image_num += 1 page_start_index = page_num * 5 if page_num + 1 == page_cnt: last_page = True # This call prepares data to render on PDF build_slb_per_page_schedule_dict( last_page, last_page_cnt, page_start_index, schedule_page_dict, slb_list, memo_array, ) page_subtotal = float(schedule_page_dict["pageSubtotal"]) schedule_total += page_subtotal if page_cnt == (page_num + 1): schedule_page_dict["scheduleTotal"] = "{0:.2f}".format( schedule_total) schedule_page_dict["committeeName"] = f3x_data["committeeName"] slb_outfile = (md5_directory + "SL-B/" + line_number + "/page_" + str(page_num) + ".pdf") pypdftk.fill_form(slb_infile, schedule_page_dict, slb_outfile) # Memo text changes if len(memo_array) >= 1: current_page_num += 1 memo_dict = {} temp_memo_outfile = (md5_directory + "SL-B/" + line_number + "/page_memo_temp.pdf") memo_infile = current_app.config[ "FORM_TEMPLATES_LOCATION"].format("TEXT") memo_outfile = (md5_directory + "SL-B/" + line_number + "/page_memo_" + str(page_num) + ".pdf") memo_dict["scheduleName_1"] = memo_array[0]["scheduleName"] memo_dict["memoDescription_1"] = memo_array[0][ "memoDescription"] memo_dict["transactionId_1"] = memo_array[0][ "transactionId"] memo_dict["PAGESTR"] = ("PAGE " + str(current_page_num) + " / " + str(total_no_of_pages)) if image_num: memo_dict["IMGNO"] = image_num image_num += 1 if len(memo_array) >= 2: memo_dict["scheduleName_2"] = memo_array[1][ "scheduleName"] memo_dict["memoDescription_2"] = memo_array[1][ "memoDescription"] memo_dict["transactionId_2"] = memo_array[1][ "transactionId"] # build page pypdftk.fill_form(memo_infile, memo_dict, memo_outfile) pypdftk.concat([slb_outfile, memo_outfile], temp_memo_outfile) os.remove(memo_outfile) os.rename(temp_memo_outfile, slb_outfile) if len(memo_array) >= 3: current_page_num += 1 memo_dict = {} memo_outfile = (md5_directory + "SL-B/" + line_number + "/page_memo_" + str(page_num) + ".pdf") memo_dict["scheduleName_1"] = memo_array[2][ "scheduleName"] memo_dict["memoDescription_1"] = memo_array[2][ "memoDescription"] memo_dict["transactionId_1"] = memo_array[2][ "transactionId"] memo_dict["PAGESTR"] = ("PAGE " + str(current_page_num) + " / " + str(total_no_of_pages)) if image_num: memo_dict["IMGNO"] = image_num image_num += 1 if len(memo_array) >= 4: memo_dict["scheduleName_2"] = memo_array[3][ "scheduleName"] memo_dict["memoDescription_2"] = memo_array[3][ "memoDescription"] memo_dict["transactionId_2"] = memo_array[3][ "transactionId"] # build page pypdftk.fill_form(memo_infile, memo_dict, memo_outfile) pypdftk.concat([slb_outfile, memo_outfile], temp_memo_outfile) os.remove(memo_outfile) os.rename(temp_memo_outfile, slb_outfile) if len(memo_array) >= 5: current_page_num += 1 memo_dict = {} memo_outfile = (md5_directory + "SL-B/" + line_number + "/page_memo_" + str(page_num) + ".pdf") memo_dict["scheduleName_1"] = memo_array[4][ "scheduleName"] memo_dict["memoDescription_1"] = memo_array[4][ "memoDescription"] memo_dict["transactionId_1"] = memo_array[4][ "transactionId"] memo_dict["PAGESTR"] = ("PAGE " + str(current_page_num) + " / " + str(total_no_of_pages)) if image_num: memo_dict["IMGNO"] = image_num image_num += 1 # build page pypdftk.fill_form(memo_infile, memo_dict, memo_outfile) pypdftk.concat([slb_outfile, memo_outfile], temp_memo_outfile) os.remove(memo_outfile) os.rename(temp_memo_outfile, slb_outfile) pypdftk.concat( directory_files(md5_directory + "SL-B/" + line_number + "/"), md5_directory + "SL-B/" + line_number + "/all_pages.pdf", ) if path.isfile(md5_directory + "SL-B/all_pages.pdf"): pypdftk.concat( [ md5_directory + "SL-B/all_pages.pdf", md5_directory + "SL-B/" + line_number + "/all_pages.pdf", ], md5_directory + "SL-B/temp_all_pages.pdf", ) os.rename( md5_directory + "SL-B/temp_all_pages.pdf", md5_directory + "SL-B/all_pages.pdf", ) else: os.rename( md5_directory + "SL-B/" + line_number + "/all_pages.pdf", md5_directory + "SL-B/all_pages.pdf", ) return image_num except: traceback.print_exception(*sys.exc_info())
def print_sa_line( f3x_data, md5_directory, line_number, sa_list, page_cnt, current_page_num, total_no_of_pages, image_num=None, ): if sa_list: last_page_cnt = 3 if len(sa_list) % 3 == 0 else len(sa_list) % 3 schedule_total = 0 os.makedirs(md5_directory + "SA/" + line_number, exist_ok=True) sa_infile = current_app.config["FORM_TEMPLATES_LOCATION"].format("SA") for page_num in range(page_cnt): current_page_num += 1 page_subtotal = 0 memo_array = [] last_page = False schedule_page_dict = {} if image_num: schedule_page_dict["IMGNO"] = image_num image_num += 1 schedule_page_dict["lineNumber"] = line_number schedule_page_dict["pageNo"] = current_page_num schedule_page_dict["totalPages"] = total_no_of_pages start_index = page_num * 3 if page_num + 1 == page_cnt: last_page = True # This call prepares data to render on PDF build_sa_per_page_schedule_dict( last_page, last_page_cnt, start_index, schedule_page_dict, sa_list, memo_array, ) page_subtotal = float(schedule_page_dict["pageSubtotal"]) schedule_total += page_subtotal if page_cnt == page_num + 1: schedule_page_dict["scheduleTotal"] = "{0:.2f}".format( schedule_total) schedule_page_dict["committeeName"] = f3x_data["committeeName"] sa_outfile = (md5_directory + "SA/" + line_number + "/page_" + str(page_num) + ".pdf") pypdftk.fill_form(sa_infile, schedule_page_dict, sa_outfile) # Memo text changes and build memo pages and return updated current_page_num current_page_num, image_num = build_memo_page( memo_array, md5_directory, line_number, current_page_num, page_num, total_no_of_pages, sa_outfile, name="SA", image_num=image_num, ) pypdftk.concat( directory_files(md5_directory + "SA/" + line_number + "/"), md5_directory + "SA/" + line_number + "/all_pages.pdf", ) if path.isfile(md5_directory + "SA/all_pages.pdf"): pypdftk.concat( [ md5_directory + "SA/all_pages.pdf", md5_directory + "SA/" + line_number + "/all_pages.pdf", ], md5_directory + "SA/temp_all_pages.pdf", ) os.rename( md5_directory + "SA/temp_all_pages.pdf", md5_directory + "SA/all_pages.pdf", ) else: os.rename( md5_directory + "SA/" + line_number + "/all_pages.pdf", md5_directory + "SA/all_pages.pdf", ) return current_page_num, image_num
def print_sl_levin( f3x_data, md5_directory, levin_name, sl_list, page_cnt, start_page, last_page_cnt, total_no_of_pages, image_num=None, ): try: if sl_list: levin_name = str(levin_name) reg = re.compile("^[a-z0-9._A-Z]+$") reg = bool(reg.match(levin_name)) p_levin_name = levin_name if reg is False: p_levin_name = re.sub("[^A-Za-z0-9]+", "", levin_name) p_levin_name = str(p_levin_name).replace(" ", "") start_page += 1 # schedule_total = 0 os.makedirs(md5_directory + "SL/" + p_levin_name, exist_ok=True) sl_infile = current_app.config["FORM_TEMPLATES_LOCATION"].format( "SL") for sl_page_no in range(page_cnt): last_page = False sl_schedule_page_dict = {} sl_schedule_page_dict["accountName"] = levin_name sl_schedule_page_dict["pageNo"] = start_page + sl_page_no sl_schedule_page_dict["totalPages"] = total_no_of_pages if image_num: sl_schedule_page_dict["IMGNO"] = image_num image_num += 1 page_start_index = sl_page_no * 1 if sl_page_no + 1 == page_cnt: last_page = True build_sl_levin_per_page_schedule_dict( last_page, last_page_cnt, page_start_index, sl_schedule_page_dict, sl_list, ) sl_schedule_page_dict["committeeName"] = f3x_data[ "committeeName"] sl_outfile = md5_directory + "SL/" + p_levin_name + "/page.pdf" pypdftk.fill_form(sl_infile, sl_schedule_page_dict, sl_outfile) pypdftk.concat( directory_files(md5_directory + "SL/" + p_levin_name + "/"), md5_directory + "SL/" + p_levin_name + "/all_pages.pdf", ) if path.isfile(md5_directory + "SL/all_pages.pdf"): pypdftk.concat( [ md5_directory + "SL/all_pages.pdf", md5_directory + "SL/" + p_levin_name + "/all_pages.pdf", ], md5_directory + "SL/temp_all_pages.pdf", ) os.rename( md5_directory + "SL/temp_all_pages.pdf", md5_directory + "SL/all_pages.pdf", ) else: os.rename( md5_directory + "SL/" + p_levin_name + "/all_pages.pdf", md5_directory + "SL/all_pages.pdf", ) return image_num except Exception as e: raise e
def print_sh2_line( f3x_data, md5_directory, tran_type_ident, sh2_list, page_cnt, current_page_num, total_no_of_pages, image_num=None, ): try: if sh2_list: last_page_cnt = 6 if len(sh2_list) % 6 == 0 else len(sh2_list) % 6 os.makedirs(md5_directory + "SH2/" + tran_type_ident, exist_ok=True) sh2_infile = current_app.config["FORM_TEMPLATES_LOCATION"].format( "SH2") for page_num in range(page_cnt): current_page_num += 1 last_page = False schedule_page_dict = {} schedule_page_dict["pageNo"] = current_page_num schedule_page_dict["totalPages"] = total_no_of_pages if image_num: schedule_page_dict["IMGNO"] = image_num image_num += 1 page_start_index = page_num * 6 if page_num + 1 == page_cnt: last_page = True # This call prepares data to render on PDF build_sh2_per_page_schedule_dict( last_page, last_page_cnt, page_start_index, schedule_page_dict, sh2_list, ) schedule_page_dict["committeeName"] = f3x_data["committeeName"] sh2_outfile = md5_directory + "SH2/" + tran_type_ident + "/page.pdf" pypdftk.fill_form(sh2_infile, schedule_page_dict, sh2_outfile) pypdftk.concat( directory_files(md5_directory + "SH2/" + tran_type_ident + "/"), md5_directory + "SH2/" + tran_type_ident + "/all_pages.pdf", ) if path.isfile(md5_directory + "SH2/all_pages.pdf"): pypdftk.concat( [ md5_directory + "SH2/all_pages.pdf", md5_directory + "SH2/" + tran_type_ident + "/all_pages.pdf", ], md5_directory + "SH2/temp_all_pages.pdf", ) os.rename( md5_directory + "SH2/temp_all_pages.pdf", md5_directory + "SH2/all_pages.pdf", ) else: os.rename( md5_directory + "SH2/" + tran_type_ident + "/all_pages.pdf", md5_directory + "SH2/all_pages.pdf", ) return image_num except: traceback.print_exception(*sys.exc_info())
def print_sh3_line( f3x_data, md5_directory, line_number, sh3_list, sh3_line_page_cnt, sh3_line_start_page, total_no_of_pages, image_num=None, ): if sh3_list: os.makedirs(md5_directory + "SH3/" + line_number, exist_ok=True) sh3_infile = current_app.config["FORM_TEMPLATES_LOCATION"].format("SH3") sh3_dict = make_sh3_dict(sh3_list) current_page_num = 0 for hash_check, hash_check_value in sh3_dict.items(): hash_check_total_pages = get_sh3_page_count(hash_check_value) account_name = hash_check.split("@@")[0] receipt_date = None if len(hash_check.split("@@")[0]) > 1: receipt_date = hash_check.split("@@")[1] event_type_dict = { "AD": {"current_index": 0, "amount": 0}, "GV": {"current_index": 0, "amount": 0}, "EA": {"current_index": 0, "amount": 0}, "DC": {"current_index": 0, "amount": 0}, "DF": {"current_index": 0, "amount": 0}, "PC": {"current_index": 0, "amount": 0}, } total_amount = 0 while hash_check_total_pages: hash_check_total_pages -= 1 current_page_total_amount = 0 current_page_num += 1 sh3_schedule_page_dict = {} sh3_schedule_page_dict["lineNumber"] = line_number sh3_schedule_page_dict["pageNo"] = ( sh3_line_start_page + current_page_num ) sh3_schedule_page_dict["totalPages"] = total_no_of_pages if image_num: sh3_schedule_page_dict["IMGNO"] = image_num image_num += 1 sh3_schedule_page_dict["accountName"] = account_name if receipt_date: date_array = receipt_date.split("/") sh3_schedule_page_dict["receiptDateMonth"] = date_array[0] sh3_schedule_page_dict["receiptDateDay"] = date_array[1] sh3_schedule_page_dict["receiptDateYear"] = date_array[2] for event_type, value_list in hash_check_value.items(): current_index = event_type_dict[event_type]["current_index"] if current_index < len(value_list): sh3_schedule_page_dict[ event_type.lower() + "transactionId" ] = value_list[current_index]["transactionId"] sh3_schedule_page_dict[ event_type.lower() + "transferredAmount" ] = "{0:.2f}".format( float(value_list[current_index]["transferredAmount"]) ) current_page_total_amount += float( sh3_schedule_page_dict[ event_type.lower() + "transferredAmount" ] ) event_type_dict[event_type]["current_index"] += 1 if event_type in ["DC", "DF"]: sub_transferred_amount = float( sh3_schedule_page_dict[ event_type.lower() + "transferredAmount" ] ) event_type_dict[event_type]["amount"] += float( sh3_schedule_page_dict[ event_type.lower() + "transferredAmount" ] ) sh3_schedule_page_dict[ event_type.lower() + "activityEventName" ] = value_list[current_index]["activityEventName"] if current_index + 1 < len(value_list): current_index += 1 sh3_schedule_page_dict[ event_type.lower() + "transactionId_1" ] = value_list[current_index]["transactionId"] sh3_schedule_page_dict[ event_type.lower() + "transferredAmount_1" ] = "{0:.2f}".format( float( value_list[current_index]["transferredAmount"] ) ) sh3_schedule_page_dict[ event_type.lower() + "activityEventName_1" ] = value_list[current_index]["activityEventName"] sub_transferred_amount += float( sh3_schedule_page_dict[ event_type.lower() + "transferredAmount_1" ] ) current_page_total_amount += float( sh3_schedule_page_dict[ event_type.lower() + "transferredAmount_1" ] ) event_type_dict[event_type]["current_index"] += 1 event_type_dict[event_type]["amount"] += float( sh3_schedule_page_dict[ event_type.lower() + "transferredAmount_1" ] ) sh3_schedule_page_dict[ event_type.lower() + "subtransferredAmount" ] = "{0:.2f}".format(float(sub_transferred_amount)) else: event_type_dict[event_type]["amount"] += float( sh3_schedule_page_dict[ event_type.lower() + "transferredAmount" ] ) sh3_schedule_page_dict["totalAmountTransferred"] = "{0:.2f}".format( float(current_page_total_amount) ) total_amount += current_page_total_amount # condition for last page if not hash_check_total_pages: sh3_schedule_page_dict["totalAmountPeriod"] = "{0:.2f}".format( float(total_amount) ) for key, value in event_type_dict.items(): sh3_schedule_page_dict[ key.lower() + "total" ] = "{0:.2f}".format(float(value["amount"])) sh3_outfile = ( md5_directory + "SH3/" + line_number + "/page_" + str(current_page_num - 1) + ".pdf" ) pypdftk.fill_form(sh3_infile, sh3_schedule_page_dict, sh3_outfile) pypdftk.concat( directory_files(md5_directory + "SH3/" + line_number + "/"), md5_directory + "SH3/" + line_number + "/all_pages.pdf", ) if path.isfile(md5_directory + "SH3/all_pages.pdf"): pypdftk.concat( [ md5_directory + "SH3/all_pages.pdf", md5_directory + "SH3/" + line_number + "/all_pages.pdf", ], md5_directory + "SH3/temp_allpages.pdf", ) os.rename( md5_directory + "SH3/temp_all_pages.pdf", md5_directory + "SH3/all_pages.pdf", ) else: os.rename( md5_directory + "SH3/" + line_number + "/all_pages.pdf", md5_directory + "SH3/all_pages.pdf", ) return image_num
def print_sb_line( f3x_data, md5_directory, line_number, sb_list, page_cnt, current_page_num, total_no_of_pages, image_num=None, ): try: if sb_list: last_page_cnt = 3 if len(sb_list) % 3 == 0 else len(sb_list) % 3 schedule_total = 0 os.makedirs(md5_directory + "SB/" + line_number, exist_ok=True) sb_infile = current_app.config["FORM_TEMPLATES_LOCATION"].format( "SB") for page_num in range(page_cnt): current_page_num += 1 memo_array = [] last_page = False schedule_page_dict = {} schedule_page_dict["lineNumber"] = line_number schedule_page_dict["pageNo"] = current_page_num schedule_page_dict["totalPages"] = total_no_of_pages if image_num: schedule_page_dict["IMGNO"] = image_num image_num += 1 page_start_index = page_num * 3 if page_num + 1 == page_cnt: last_page = True # This call prepares data to render on PDF build_sb_per_page_schedule_dict( last_page, last_page_cnt, page_start_index, schedule_page_dict, sb_list, memo_array, ) try: schedule_total += float(schedule_page_dict["pageSubtotal"]) if page_cnt == page_num + 1: schedule_page_dict["scheduleTotal"] = "{0:.2f}".format( schedule_total) schedule_page_dict["committeeName"] = f3x_data["committeeName"] sb_outfile = (md5_directory + "SB/" + line_number + "/page_" + str(page_num) + ".pdf") pypdftk.fill_form(sb_infile, schedule_page_dict, sb_outfile) # Memo text changes and build memo pages and return updated current_page_num current_page_num, image_num = build_memo_page( memo_array, md5_directory, line_number, current_page_num, page_num, total_no_of_pages, sb_outfile, name="SB", image_num=image_num, ) pypdftk.concat( directory_files(md5_directory + "SB/" + line_number + "/"), md5_directory + "SB/" + line_number + "/all_pages.pdf", ) if path.isfile(md5_directory + "SB/all_pages.pdf"): pypdftk.concat( [ md5_directory + "SB/all_pages.pdf", md5_directory + "SB/" + line_number + "/all_pages.pdf", ], md5_directory + "SB/temp_all_pages.pdf", ) os.rename( md5_directory + "SB/temp_all_pages.pdf", md5_directory + "SB/all_pages.pdf", ) else: os.rename( md5_directory + "SB/" + line_number + "/all_pages.pdf", md5_directory + "SB/all_pages.pdf", ) except: logging.error('**** Start - Error inside if condition ****') # printing stack trace traceback.print_exception(*sys.exc_info()) logging.error('**** End - Error inside if condition ****') return current_page_num, image_num except: # printing stack trace traceback.print_exception(*sys.exc_info())