def print_report_acrobat(self): if self: datas = {} for chp in self.chp_tmp_ids: datas[chp.name or ''] = chp.value_openfire or '' attachment_obj = self.env['ir.attachment'] attachment = attachment_obj.search([ ('res_model', '=', self.lettre_id._name), ('res_field', '=', 'file'), ('res_id', '=', self.lettre_id.id) ]) if not attachment: raise UserError( u'Ce modèle de courrier ne contient pas de document pdf') # Generation du fichier rempli. Le parametre flatten (True par defaut) retire la possibilite de modifier le document pdf genere file_path = attachment_obj._full_path(attachment.store_fname) fd, generated_pdf = tempfile.mkstemp(prefix='gesdoc_', suffix='.pdf') try: pypdftk.fill_form(file_path, datas, out_file=generated_pdf, flatten=not self.lettre_id.fillable) with open(generated_pdf, "rb") as encode: encoded_file = base64.b64encode(encode.read()) finally: os.close(fd) try: os.remove(generated_pdf) except Exception: pass # Si c'est un rapport généré depuis un SAV, on prend le no du SAV et le nom du client if self._context['model'] == 'project.issue' and datas.get( 'cnom') and datas.get('sno'): res_file_name = datas['sno'] + ' ' + datas['cnom'] + '.pdf' elif self._context['model'] == 'project.issue' and datas.get( '1_Client_Nom') and datas.get('1_num_sav'): res_file_name = datas['1_num_sav'] + ' ' + datas[ '1_Client_Nom'] + '.pdf' else: res_file_name = 'courrier.pdf' self.write({ 'res_file': encoded_file, 'res_file_name': res_file_name }) view = self.env.ref('of_gesdoc.view_courrier_wizard') return { 'name': 'Envoyer un courrier', 'view_type': 'form', 'view_mode': 'form', 'view_id': view.id, 'res_model': 'of.compose.mail', 'src_model': self._context['model'], 'type': 'ir.actions.act_window', 'target': 'new', 'res_id': self.id, 'context': self._context, }
def process_pdf(data, images, pagename, input_dir, out_dir): if getattr(sys, "frozen", False): fill = os.path.join(sys._MEIPASS, "files/fillable_ops.pdf") exe = os.path.join(sys._MEIPASS, "pdftk.exe") else: fill = "files/fillable_ops.pdf" pypdftk.fill_form(fill, datas=data, out_file=os.path.join(input_dir, "%s_temp.pdf" % pagename), flatten=True) # gen_xfdf(data,os.path.join(out_dir,pagename+".fdf")) # subprocess.call([exe,fill,"fill_form",os.path.join("out_dir",pagename+".fdf"),"output",os.path.join(input_dir,"%s_temp.pdf"%pagename),"flatten"]) if pagename == "Summary": write_single_image(images["left"], os.path.join(input_dir, "%s_temp.pdf" % pagename), os.path.join(out_dir, "%s.pdf" % pagename)) else: write_double_image(images["left"], images["right"], os.path.join(input_dir, "%s_temp.pdf" % pagename), os.path.join(out_dir, "%s.pdf" % pagename))
def create_form(self): self.fill_fields() # fdf = forge_fdf("", self.fields, [], [], []) # fdf_file = open("data.fdf", "wb") # fdf_file.write(fdf) # fdf_file.close() fill_form("leaveForm.pdf", self.datas, "output.pdf")
def print_summ(print_dict, page_index, reportId, json_file_md5): try: md5_directory = current_app.config['OUTPUT_DIR_LOCATION'].format(json_file_md5) infile = current_app.config['FORM_TEMPLATES_LOCATION'].format('TEXT') outfile = md5_directory + json_file_md5 + '_temp.pdf' pypdftk.fill_form(infile, print_dict, outfile) shutil.copy(outfile, md5_directory + reportId + '/F24_{}.pdf'.format(page_index)) os.remove(outfile) except Exception as e: return error('print_f24_summ error, error message: ' + str(e))
def generate_pdf(invoice): pdf_invoice = tempfile.NamedTemporaryFile() if not os.path.isfile(settings.TEMPLATE_FILE): return None if invoice.status == 4: invoice_date = "VOID" else: if invoice.bill_date: invoice_date = invoice.bill_date.strftime("%m/%d/%Y") else: invoice_date = "DRAFT" items = "" hours = "" rate = "" amount = "" for item in invoice.items.all(): items = items + item.description + "\n" if item.hours and item.rate: hours += str(item.hours) + "\n" rate += "$" + f"{item.rate:,.2f}" + "\n" else: hours += "\n" rate += "\n" amount += "$" + f"{item.amount:,.2f}" + "\n" invoice_data = { "INVOICE_ID": invoice.invoice_id, "BILL_DATE": invoice_date, "PARTNER": invoice.partner.company, "ADDRESS1": invoice.partner.get_address1(), "ADDRESS2": invoice.partner.get_address2(), "ITEMS": items, "HOURS": hours, "RATE": rate, "AMOUNT": amount, "TOTAL": "$" + f"{invoice.get_total():,.2f}", } pypdftk.fill_form(settings.TEMPLATE_FILE, invoice_data, pdf_invoice.name, flatten=True) stream = BytesIO(pdf_invoice.read()) pdf_invoice.close() return stream.getvalue()
def pdf_file_write(form_path, applicant_data): """ 根据传入的表格文件路径以及数据,尝试将匹配的数据写入到表格文件中并保存 :param form_path: 表格文件 :param applicant_data: 要填写的数据 :return: None """ if "decrypted" not in form_path: try: decrypted_file_path = pdf_decryption(form_path) pdf_template = PdfReader(decrypted_file_path) except Exception as err: print(err) return else: pdf_template = PdfReader(form_path) if pdf_template: data = {} if applicant_data and len(applicant_data) != 0: for key in applicant_data.keys(): if applicant_data[key] and applicant_data[key] != '': # 过滤掉数据中值为0的情况 data[key] = applicant_data[key] # 解决写入值后在 adobe中不显示,在浏览器预览中可以显示值的问题,但是在adobe中打开,大部分显示不全 # 使用浏览器打开可以显示,存在的问题:日期、checkbox、大段文本展示效果较差 # https://github.com/pmaupin/pdfrw/issues/84 pdf_template.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true'))) for i in range(len(pdf_template.pages)): this_page = pdf_template.pages[i] if ANNOT_KEY in this_page.keys(): annotations = this_page[ANNOT_KEY] for annotation in annotations: if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY: if ANNOT_FIELD_KEY in annotation.keys(): key = annotation[ANNOT_FIELD_KEY][1:-1] if key in data.keys() and data[key] != 'None': annotation.update(pdfrw.PdfDict(AP='')) # 解决在浏览器预览中不显示的问题 annotation.update( pdfrw.PdfDict(V='{}'.format(data[key])) ) # flatten the pdf https://stackoverflow.com/questions/14454387/pdfbox-how-to-flatten-a-pdf-form # 不起作用 # pdf_template.Root.AcroForm.update(pdfrw.PdfDict(Ff=1)) dst_name = get_out_pdf_file_name(form_path, applicant_data) pdfrw.PdfWriter().write(dst_name, pdf_template) # https://github.com/mikehaertl/php-pdftk/issues/62 尚未解决 pypdftk.fill_form("1022_Zhang_Lingyun.pdf", out_file='out.pdf', flatten=True)
def print_summary(print_dict, page_index, reportId, json_file_md5): try: md5_directory = current_app.config["OUTPUT_DIR_LOCATION"].format( json_file_md5) infile = current_app.config["FORM_TEMPLATES_LOCATION"].format("TEXT") outfile = md5_directory + json_file_md5 + "_temp.pdf" pypdftk.fill_form(infile, print_dict, outfile) shutil.copy( outfile, md5_directory + reportId + "/F24_{}.pdf".format(page_index)) os.remove(outfile) except Exception as e: return error("print_f24_summ error, error message: " + str(e))
def test_fill_form(self): result = pypdftk.fill_form(TEST_XPDF_PATH, datas=SAMPLE_DATA2, flatten=False) result_data = ordered(pypdftk.dump_data_fields(result)) expected_data = ordered(json.loads(read(TEST_XPDF_FILLED_DATA_DUMP))) self.assertEqual(result_data, expected_data)
def fill_form(data_dict, out_file): PDF_PATH = 'leaks/l_a_r.pdf' out_file = out_file generated_pdf = pypdftk.fill_form( pdf_path=PDF_PATH, datas=data_dict, out_file=out_file, )
def generate_sf425(): ''' Using sample data, fill out and display the specified PDF. ''' pdf_directory = os.path.dirname(os.path.abspath(__file__)) original_pdf_path = pdf_directory + '/' + PDF_FILENAME destination_pdf_file = pdf_directory + '/' + 'filled-out.pdf' sample_values = generate_sample_field_values() # Documentation: https://github.com/revolunet/pypdftk return pypdftk.fill_form(original_pdf_path, sample_values, destination_pdf_file)
def print_acrobat(self, template, model): """ Impression de modèle de courrier avec fichier joint. Calcul de champs si PDF éditable :param template: Modèle de courrier utilisé :param model: objet sur lequel le calcul de champ est basé :return: PDF sous forme de chaine de caractères """ template_file = False compose_mail_obj = request.env['of.compose.mail'] attachment_obj = request.env['ir.attachment'] if not template.chp_ids: template_file = base64.b64decode(template.file) else: attachment = attachment_obj.search([('res_model', '=', template._name), ('res_field', '=', 'file'), ('res_id', '=', template.id)]) datas = dict(compose_mail_obj.eval_champs(model, template.chp_ids)) file_path = attachment_obj._full_path(attachment.store_fname) fd, generated_pdf = tempfile.mkstemp(prefix='doc_joint_', suffix='.pdf') try: pypdftk.fill_form(file_path, datas, out_file=generated_pdf, flatten=not template.fillable) with open(generated_pdf, "rb") as encode: template_file = encode.read() finally: os.close(fd) try: os.remove(generated_pdf) except Exception: pass return template_file
def generate_filled_pdf(pdfname): """ Generates filled PDF file. POST: /pdf/<pdfname> :body - data to be insered in PDF file (JSON). """ r_json = request.get_json() if r_json is None: return abort(500, "Error in JSON body.") pdfFilePath = pypdftk.fill_form( "{folder}/{file}.pdf".format(folder=PDF_FOLDER, file=pdfname), r_json) return send_file(pdfFilePath, attachment_filename='out.pdf')
def invoice_gen(list): with open(list, 'r', encoding='GBK') as f: reader = csv.reader(f) fieldnames = next(reader) # print(fieldnames) csv_reader = csv.DictReader(f, fieldnames=fieldnames) # self._fieldnames = fieldnames # list of keys for the dict 以list的形式存放键名 for row in csv_reader: # 遍历写入 d = {} for k, v in row.items(): d[k] = v # print(d) index = d['ref_no'] # 文件取名用的索引 output = "Invoice" + "_" + str(date1) + "_" + str(index) + ".pdf" template = 'InvoiceTemplate.pdf' generate_pdf = pypdftk.fill_form(template, d, out_file=output)
def generate_form(datas): module_dir = os.path.dirname(__file__) # get current directory PATH = os.path.join(module_dir, "SOFC-Credit-Card-Payment.pdf") var = py.dump_data_fields(PATH) # for elem in var: # print(elem['FieldName']) generated_pdf = py.fill_form(PATH, datas, out_file=os.path.join(module_dir, 'new_pdf.pdf')) print('form has been generated') return os.path.join(module_dir, 'new_pdf.pdf') # datas = dict() # datas['Account Number'] = 123456 # datas['SubAccount'] = 123456 # datas['Amount'] = 123 # datas['Date'] = date # datas['Org Name'] = org # generate_form(datas) # Account Number # SubAccount # Date mmddyyyy # Amount # Vendor # Contact # Phone # Reservation Phone # Reservation Guest # Reservation by # Start Date # End Date # Statement of Benefit # Leader Phone # Leader Date # Advisor Phone # Advisor Date # Travel 1 # Travel 2 # Org Name # Invoice Confirmation
async def retrieve_character_sheet(self, ctx, name): try: async with self.bot.pool.acquire() as connection: async with connection.transaction(): char = await connection.fetchrow("""SELECT * FROM characters WHERE char_name = $1""", name) char = dict(char) out_char = {k[len("char_"):]: v for k, v in char.items() if not k.startswith("char_inventory")} out_inv = {k: v for k, v in char.items() if k.startswith("char_inven")} separated_inventory = dict() for k, v in out_inv.items(): order = k[len("char_inventory"):] temp_list = v.split("-") separated_inventory['ld' + order] = temp_list.pop(0) separated_inventory['dur' + order] = temp_list.pop() separated_inventory['sup' + order] = temp_list.pop() separated_inventory['desc' + order] = '-'.join(temp_list) separated_inventory["player"] = await self.get_name_from_db( out_char["player"]) out_char.update(separated_inventory) pprint.pprint(out_char) finalpdf = pypdftk.fill_form("Base_Sheet.pdf", out_char, f'{"_".join(name.split(" "))}.pdf', flatten=False) print(finalpdf) with open(finalpdf, 'rb') as file: file_to_send = discord.File(file, finalpdf) await ctx.send(f'{ctx.author.mention}, aqui está o personagem' f' requisitado, **{name}**:\n', file=file_to_send) await ctx.message.delete() except Exception as e: print(e) await ctx.message.delete() return return
import pypdftk pypdftk.fill_form('demo.pdf', out_file='flattened.pdf', flatten=True)
import csv import pypdftk #打开list.csv,按照header的关键词生成dict并写入模板 with open("list.csv",'r',encoding="utf-8") as f: reader = csv.reader(f) fieldnames = next(reader) # print(fieldnames) csv_reader = csv.DictReader(f,fieldnames=fieldnames) #self._fieldnames = fieldnames # list of keys for the dict 以list的形式存放键名 for row in csv_reader: #遍历写入 d={} for k,v in row.items(): d[k]=v output = "test"+str(v)+".pdf" template = 'InvoiceTemplate.pdf' generate_pdf=pypdftk.fill_form(template, d, out_file=output) # print(d) # accountINfo={} # accountInfo = dict(ib_name="Allen", address="36A", date="today", ref_no="001", ib_withdrawal="100", total="200", # bank_name="NAB", bank_branch_name="123", account_name="edward", account_number="114514") # arg1 = 'InvoiceTemplate.pdf' # arg2 = 'test.pdf' # generate_pdf=pypdftk.fill_form(arg1, accountInfo, out_file=arg2)
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 process_item(item, tmp_dir): """Process a single item from the JSON config - download remote resources - convert HTML to PDF - copy local resources - use supplied data to fill the PDF form fields """ log.debug("Processing item: '%s'", item) uri = item["uri"] fill_forms = True process_file = True if "output" in item and os.path.lexists(item["output"]): # if the file has already been generated if item.get("overwrite", True): os.remove(item["output"]) else: # we reuse the existing file pdf_filename = make_tmp_file(tmp_dir) log.info("Copying existing file '%s' to temporary '%s'...", item["output"], pdf_filename) shutil.copy2(item["output"], pdf_filename) process_file = False if process_file: if uri.startswith(("http://", "https://")): f = requests.head(uri) f.raise_for_status() ftype = f.headers["content-type"] if ftype.startswith("application/json"): # Start script recursively (ToDo) print "JSON !" elif ftype.startswith("application/pdf"): # download the PDF download = requests.get(uri) download.raise_for_status() pdf_filename = make_tmp_file(tmp_dir) log.info("Saving pdf from network to local '%s'", pdf_filename) # Write PDF with open(pdf_filename, "wb") as f: f.write(download.content) elif ftype.startswith("text/html"): # Make PDF from URL fill_forms = False pdf_filename = call_wkhtmltopdf(item["uri"], tmp_dir, item.get("options")) else: # Use the 'mimetype' system command to determine filetype # -b is for brief response, -M is for Magic Only. if not os.path.isfile(uri): raise Exception("File do not exist", uri) ftype = subprocess.check_output(["mimetype", "-b", "-M", uri]).strip() if ftype == "application/json": # Start script recursively (ToDo) print "JSON !" elif ftype == "application/pdf": pdf_filename = make_tmp_file(tmp_dir) log.info("Copying '%s' to '%s'...", uri, pdf_filename) shutil.copy2(uri, pdf_filename) else: raise Exception("Unsupported file type", ftype) if "output" in item: output = os.path.join(BASE_OUTPUT_DIR, item["output"]) check_output_folder(BASE_OUTPUT_DIR, output, create_folder=True) shutil.copy2(pdf_filename, output) # Use data (item specific as well as global) to fill the PDF forms if fill_forms and item.get("data"): log.info("Filling '%s' with data...", pdf_filename) filled_filename = make_tmp_file(tmp_dir) try: pypdftk.fill_form(pdf_filename, item["data"], filled_filename) except: if os.path.lexists(filled_filename): os.remove(filled_filename) # raise log.error("Got an exception when filling PDF '%s'. Using empty one instead.", pdf_filename) filled_filename = pdf_filename pdf_filename = filled_filename return pdf_filename
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): # 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") if 'json_file' in request.files: total_no_of_pages = 1 page_no = 1 json_file = request.files.get('json_file') # generate md5 for json file json_file_md5 = utils.md5_for_file(json_file) json_file.stream.seek(0) md5_directory = current_app.config['OUTPUT_DIR_LOCATION'].format( json_file_md5) 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') json_file.save( current_app.config['REQUEST_FILE_LOCATION'].format(json_file_md5)) outfile = md5_directory + json_file_md5 + '.html' json_data = json.load( open(current_app.config['REQUEST_FILE_LOCATION'].format( json_file_md5))) form99_json_data = json_data['data'] # load the file with open(infile) as inf: txt = inf.read() soup = bs4.BeautifulSoup(txt) 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'] soup.find('label', attrs={'id': 'treasurerFullName'}).string = form99_json_data['treasurerLastName'] + \ ', ' + form99_json_data['treasurerFirstName'] \ + ', ' + form99_json_data['treasurerMiddleName'] \ + ', ' + form99_json_data['treasurerPrefix'] \ + ', ' + form99_json_data['treasurerSuffix'] soup.find('label', attrs={'id': 'treasurerName'}).string = form99_json_data['treasurerLastName'] + \ ', ' + form99_json_data['treasurerFirstName'] 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.36in', 'margin-right': '0.25in', 'margin-bottom': '0.39in', 'margin-left': '0.25in' } # 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) total_no_of_pages = pypdftk.get_num_pages(md5_directory + json_file_md5 + '.pdf') page_number_file = current_app.config[ 'FORM_TEMPLATES_LOCATION'].format('Page_Number') # checking if attachment_file exist if 'attachment_file' in request.files: # reading Attachment title file attachment_title_file = current_app.config[ 'FORM_TEMPLATES_LOCATION'].format('Attachment_Title') 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 pypdftk.fill_form( attachment_title_file, { "PAGESTR": "PAGE " + str(attachment_page_no) + " / " + str(total_no_of_pages) }, 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') # shutil.rmtree(md5_directory + 'attachment') # shutil.rmtree(md5_directory + 'final_attachment') # pypdftk.concat([md5_directory + json_file_md5 + '.pdf', md5_directory + 'attachment.pdf'], md5_directory + 'all_pages_temp.pdf') # else: # shutil.move(md5_directory + json_file_md5 + '.pdf', md5_directory + 'all_pages_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'): pypdftk.fill_form( page_number_file, { "PAGESTR": "PAGE " + str(f99_page_no) + " / " + str(total_no_of_pages) }, md5_directory + 'pages/page_number_' + str(f99_page_no) + '.pdf') pypdftk.stamp( md5_directory + 'pages/page_number_' + str(f99_page_no) + '.pdf', md5_directory + 'pages/' + filename, md5_directory + 'final_pages/page_' + str(f99_page_no) + '.pdf') f99_page_no += 1 pypdftk.concat(directory_files(md5_directory + 'final_pages/'), json_file_md5 + '_temp.pdf') if '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 + '.html') # shutil.rmtree(md5_directory + 'images') # os.remove(md5_directory + 'form-text.css') os.remove(md5_directory + json_file_md5 + '.pdf') # for f99_page_no in range(f99_no_of_pages): # pypdftk.fill_form(page_number_file, # {"PAGESTR": "PAGE " + str(f99_page_no+1) + " / " + str(total_no_of_pages)}, # md5_directory + 'pages/page_' + str(f99_page_no+1) + '.pdf') # pypdftk.stamp(md5_directory + json_file_md5 + '.pdf', md5_directory + # 'pages/page_' + str(f99_page_no+1) + '.pdf', md5_directory + json_file_md5 + '_temp.pdf') # json_data['PAGESTR'] = "PAGE " + str(page_no) + " / " + str(total_no_of_pages) # json_data['MISCELLANEOUS_TEXT'] = '' # xfdf_path = pypdftk.gen_xfdf(json_data) # pypdftk.fill_form(infile, json_data, outfile) # 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><font face='Helvetica' size=10 ''' + f99_full_text).\ # write_pdf("output/pdf/test/test.pdf") # pypdftk.stamp(outfile, "output/pdf/test/test.pdf", "output/pdf/test/output.pdf") # additional_page_counter = 0 # if len(f99_pages_text_json['additional_pages']) > 0: # continuation_file = current_app.config['FORM_TEMPLATES_LOCATION'].format('F99_CONT') # os.makedirs(md5_directory + 'merge', exist_ok=True) # for additional_page in f99_pages_text_json['additional_pages']: # page_no += 1 # continuation_outfile = md5_directory + 'merge/' + str(additional_page_counter)+'.pdf' # pypdftk.fill_form(continuation_file, {"PAGESTR": "PAGE "+str(page_no)+" / " + str(total_no_of_pages), # "CONTINOUS_TEXT": additional_page[str(additional_page_counter)]}, continuation_outfile) # pypdftk.concat([outfile, continuation_outfile], md5_directory + json_file_md5 + '_all_pages_temp.pdf') # shutil.copy(md5_directory + json_file_md5 + '_all_pages_temp.pdf', outfile) # additional_page_counter += 1 # os.remove(md5_directory + json_file_md5 + '_all_pages_temp.pdf') # # # Add the F99 attachment # if 'attachment_file' in request.files: # pypdftk.concat([outfile, md5_directory + 'attachment.pdf'], md5_directory + 'all_pages.pdf') # os.remove(md5_directory + 'attachment.pdf') # else: # shutil.copy(outfile, md5_directory + 'all_pages.pdf') # os.remove(md5_directory + json_file_md5 +'_temp.pdf') # push output file to AWS s3 = boto3.client('s3') s3.upload_file( md5_directory + 'all_pages.pdf', current_app.config['AWS_FECFILE_COMPONENTS_BUCKET_NAME'], md5_directory + 'all_pages.pdf', ExtraArgs={ 'ContentType': "application/pdf", 'ACL': "public-read" }) response = { # 'file_name': '{}.pdf'.format(json_file_md5), 'pdf_url': current_app.config['PRINT_OUTPUT_FILE_URL'].format(json_file_md5) + 'all_pages.pdf' } if flask.request.method == "POST": envelope = common.get_return_envelope(data=response) status_code = status.HTTP_201_CREATED return flask.jsonify(**envelope), status_code else: if flask.request.method == "POST": envelope = common.get_return_envelope( 'false', 'JSON file is missing from your request') status_code = status.HTTP_400_BAD_REQUEST return flask.jsonify(**envelope), status_code
def print_f99_pdftk(stamp_print): # check if json_file is in the request if 'json_file' in request.files: total_no_of_pages = 1 page_no = 1 json_file = request.files.get('json_file') # generate md5 for json file json_file_md5 = utils.md5_for_file(json_file) json_file.stream.seek(0) md5_directory = current_app.config['OUTPUT_DIR_LOCATION'].format( json_file_md5) os.makedirs(md5_directory, exist_ok=True) infile = current_app.config['FORM_TEMPLATES_LOCATION'].format('F99') # save json file as md5 file name json_file.save( current_app.config['REQUEST_FILE_LOCATION'].format(json_file_md5)) outfile = md5_directory + json_file_md5 + '_temp.pdf' json_data = json.load( open(current_app.config['REQUEST_FILE_LOCATION'].format( json_file_md5))) # setting timestamp and imgno to empty as these needs to show up after submission if stamp_print != 'stamp': json_data['FILING_TIMESTAMP'] = '' json_data['IMGNO'] = '' f99_pages_text_json = json.loads(split_f99_text_pages(json_data)) json_data['MISCELLANEOUS_TEXT'] = f99_pages_text_json['main_page'] total_no_of_pages += len(f99_pages_text_json['additional_pages']) # checking if attachment_file exist if 'attachment_file' in request.files: # reading Attachment title file attachment_title_file = current_app.config[ 'FORM_TEMPLATES_LOCATION'].format('Attachment_Title') 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 pypdftk.fill_form( attachment_title_file, { "PAGESTR": "PAGE " + str(attachment_page_no) + " / " + str(total_no_of_pages) }, 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') shutil.rmtree(md5_directory + 'attachment') shutil.rmtree(md5_directory + 'final_attachment') json_data['PAGESTR'] = "PAGE " + str(page_no) + " / " + str( total_no_of_pages) pypdftk.fill_form(infile, json_data, outfile, flatten=False) additional_page_counter = 0 if len(f99_pages_text_json['additional_pages']) > 0: continuation_file = current_app.config[ 'FORM_TEMPLATES_LOCATION'].format('F99_CONT') os.makedirs(md5_directory + 'merge', exist_ok=True) for additional_page in f99_pages_text_json['additional_pages']: page_no += 1 continuation_outfile = md5_directory + 'merge/' + str( additional_page_counter) + '.pdf' pypdftk.fill_form( continuation_file, { "PAGESTR": "PAGE " + str(page_no) + " / " + str(total_no_of_pages), "CONTINOUS_TEXT": additional_page[str(additional_page_counter)] }, continuation_outfile) pypdftk.concat([outfile, continuation_outfile], md5_directory + json_file_md5 + '_all_pages_temp.pdf') shutil.copy( md5_directory + json_file_md5 + '_all_pages_temp.pdf', outfile) additional_page_counter += 1 os.remove(md5_directory + json_file_md5 + '_all_pages_temp.pdf') # Add the F99 attachment if 'attachment_file' in request.files: pypdftk.concat([outfile, md5_directory + 'attachment.pdf'], md5_directory + 'all_pages.pdf') os.remove(md5_directory + 'attachment.pdf') else: shutil.copy(outfile, md5_directory + 'all_pages.pdf') os.remove(md5_directory + json_file_md5 + '_temp.pdf') # push output file to AWS s3 = boto3.client('s3') s3.upload_file( md5_directory + 'all_pages.pdf', current_app.config['AWS_FECFILE_COMPONENTS_BUCKET_NAME'], md5_directory + 'all_pages.pdf', ExtraArgs={ 'ContentType': "application/pdf", 'ACL': "public-read" }) response = { # 'file_name': '{}.pdf'.format(json_file_md5), 'pdf_url': current_app.config['PRINT_OUTPUT_FILE_URL'].format(json_file_md5) + 'all_pages.pdf' } if flask.request.method == "POST": envelope = common.get_return_envelope(data=response) status_code = status.HTTP_201_CREATED return flask.jsonify(**envelope), status_code else: if flask.request.method == "POST": envelope = common.get_return_envelope( 'false', 'JSON file is missing from your request') status_code = status.HTTP_400_BAD_REQUEST return flask.jsonify(**envelope), status_code
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 fill_pdf(data): global xml_variables xml_var = xml_variables.copy() # Gender if(data.get("gender") == "Male"): xml_var["Anrede"] = xml_var["Anrede"][1] elif(data.get("gender") == "Female"): xml_var["Anrede"] = xml_var["Anrede"][0] # First Name fname_str = data.get("first_name") if (len(fname_str) > 28 ): fname_str= fname_str[0 : 29] xml_var["Vorname"] = fname_str # Last Name lname_str = data.get("last_name") if (len(lname_str) > 19 ): lname_str = lname_str[0 : 20] xml_var["TitelNachname"] = lname_str # Birthdate date_str = data.get("date_of_birth") if (len(date_str) > 8 ): year = date_str[0:4] month = date_str[5:7] day = date_str[8:10] date_str = day + month + year else: date_str = "" xml_var["Geburtsdatum"] = date_str # Date of registration reg_str = data.get("registration_date") if (len(reg_str) > 6 ): year = reg_str[0:4] month = reg_str[5:7] reg_str = month + year else: reg_str = "" xml_var["Anmeldedatum"] = reg_str # Address addition adr_addition = data.get("address_addition") if (len(fname_str) > 22 ): adr_addition = adr_addition[0 : 23] xml_var["Adresszusatz"] = adr_addition # Street Name street_name = data.get("street") if (len(street_name) > 22 ): street_name = street_name[0 : 23] xml_var["Straße"] = street_name # Street Number street_num = data.get("streetnumber") if (len(street_num) > 5 ): street_num = street_num[0 : 6] xml_var["Hausnummer"] = street_num # ZIP Code zip_code = str(data.get("zip_code")) if (len(zip_code) > 5 ): zip_code = zip_code[0 : 6] xml_var["PLZ"] = zip_code # City city_name = data.get("city") if (len(city_name) > 22 ): city_name = city_name[0 : 23] xml_var["Ort"] = city_name # Payment Cycle pay_cicle = int(data.get("timespan")) if(pay_cicle == 1): xml_var["Zahlungsrhythmus"] = xml_var["Zahlungsrhythmus"][0] elif(pay_cicle == 2): xml_var["Zahlungsrhythmus"] = xml_var["Zahlungsrhythmus"][1] elif(pay_cicle == 3): xml_var["Zahlungsrhythmus"] = xml_var["Zahlungsrhythmus"][2] elif(pay_cicle == 4): xml_var["Zahlungsrhythmus"] = xml_var["Zahlungsrhythmus"][3] # Type of Payment pay_type = data.get("type_of_transfer") if(pay_type == "sepa"): xml_var["Zahlungsweise"] = xml_var["Zahlungsweise"][0] elif(pay_type == "bank"): xml_var["Zahlungsweise"] = xml_var["Zahlungsweise"][1] # Street Sepa street_sepa = data.get("street_sepa") if (len(street_sepa) > 22 ): street_sepa = street_sepa[0 : 23] xml_var["Straße_Lastschrift"] = street_sepa # IBAN iban_sepa = data.get("IBAN") if (len(iban_sepa) > 28 ): iban_sepa = iban_sepa[0 : 29] xml_var["IBAN"] = iban_sepa # BIC bic_sepa = data.get("BIC") if (len(bic_sepa) > 11 ): bic_sepa = bic_sepa[0 : 12] xml_var["BIC"] = bic_sepa # Bank bank_sepa = data.get("credit_institution") if (len(bank_sepa) > 16 ): bank_sepa = bank_sepa[0 : 17] xml_var["Kreditinstitut"] = bank_sepa # Name of payment beneficiery name_sepa = data.get("name_sepa") if (len(name_sepa) > 28 ): name_sepa = name_sepa [0 : 29] xml_var["NameLastschrift"] = name_sepa # Street number of payment beneficiery streetnumber_sepa = data.get("streetnumber_sepa") if (len(streetnumber_sepa) > 5 ): streetnumber_sepa = streetnumber_sepa [0 : 6] xml_var["HausnummerLastschrift"] = streetnumber_sepa # City of payment beneficiery city_sepa = data.get("city_sepa") if (len(city_sepa) > 22 ): city_sepa = street_sepa [0 : 23] xml_var["OrtLastschrift"] = city_sepa # ZIP number of payment beneficiery zip_sepa = data.get("zip_code_sepa") if (len(zip_sepa) > 5 ): zip_sepa = zip_sepa [0 : 6] xml_var["PLZLastschrift"] = zip_sepa # Personal Telephone number phone_number = data.get("phone_number") if (len(phone_number) > 17 ): phone_number = phone_number [0 : 18] xml_var["Telefonnummer"] = phone_number # Generating PDF if exists("gez.pdf"): if not exists("pdf_storage/" + str(session["uid"])): os.mkdir("pdf_storage/" + str(session["uid"])) pdf_name = 'pdf_storage/' + str(session["uid"]) + "/GEZ_Form_" + str(datetime.datetime.now())[0:10] + ".pdf" pypdftk.fill_form('gez.pdf', xml_var ,pdf_name )
def print_pdftk(stamp_print): # check if json_file is in the request if 'json_file' in request.files: total_no_of_pages = 5 page_no = 1 json_file = request.files.get('json_file') # generate md5 for json file json_file_md5 = utils.md5_for_file(json_file) json_file.stream.seek(0) md5_directory = current_app.config['OUTPUT_DIR_LOCATION'].format( json_file_md5) os.makedirs(md5_directory, exist_ok=True) infile = current_app.config['FORM_TEMPLATES_LOCATION'].format('F3X') # save json file as md5 file name json_file.save( current_app.config['REQUEST_FILE_LOCATION'].format(json_file_md5)) outfile = md5_directory + json_file_md5 + '_temp.pdf' f3x_json = json.load( open(current_app.config['REQUEST_FILE_LOCATION'].format( json_file_md5))) # setting timestamp and imgno to empty as these needs to show up after submission if stamp_print != 'stamp': f3x_json['FILING_TIMESTAMP'] = '' f3x_json['IMGNO'] = '' # f3x_json = json.loads(json_data) f3x_data = f3x_json['data'] f3x_summary_temp = f3x_data['summary'] f3x_summary = {'cashOnHandYear': f3x_summary_temp['cashOnHandYear']} f3x_col_a = f3x_summary_temp['colA'] f3x_col_b = f3x_summary_temp['colB'] for key in f3x_col_a: f3x_summary['colA_' + key] = f3x_col_a[key] for key in f3x_col_b: f3x_summary['colB_' + key] = f3x_col_b[key] f3x_data_summary_array = [f3x_data, f3x_summary] f3x_data_summary = { i: j for x in f3x_data_summary_array for i, j in x.items() } if 'schedules' in f3x_data: schedules = f3x_data['schedules'] if 'SA' in schedules: schedule_total = 0.00 # os.remove(md5_directory + 'SA/all_pages.pdf') # create SA folder under MD5 directory os.makedirs(md5_directory + 'SA', exist_ok=True) sa_infile = current_app.config[ 'FORM_TEMPLATES_LOCATION'].format('SA') sa_schedules = schedules['SA'] # print(len(sa_schedules)) sa_array = [] sa_json = {} no_of_pages = 0 no_of_transactions_in_last_page = 0 # print(int(len(sa_schedules) / 3)) if int(len(sa_schedules) % 3) == 0: no_of_pages = int(len(sa_schedules) / 3) no_of_transactions_in_last_page = 3 else: no_of_pages = int(len(sa_schedules) / 3) + 1 no_of_transactions_in_last_page = int( len(sa_schedules) % 3) total_no_of_pages += no_of_pages if no_of_pages > 1: for sa_page_no in range(no_of_pages): page_subtotal = 0.00 sa_schedule_page_dict = {} sa_schedule_page_dict['pageNo'] = 5 + sa_page_no + 1 sa_schedule_page_dict['totalPages'] = total_no_of_pages page_start_index = sa_page_no * 3 if sa_page_no == (no_of_pages - 1): # page_end_index = page_start_index + no_of_transactions_in_last_page - 1 sa_schedule_dict = build_per_page_schedule_dict( no_of_transactions_in_last_page, page_start_index, sa_schedule_page_dict, sa_schedules) else: # no_of_transactions_in_last_page = 3 sa_schedule_dict = build_per_page_schedule_dict( 3, page_start_index, sa_schedule_page_dict, sa_schedules) page_subtotal = float( sa_schedule_page_dict['pageSubtotal']) schedule_total += page_subtotal if no_of_pages == (sa_page_no + 1): sa_schedule_page_dict[ 'scheduleTotal'] = '{0:.2f}'.format( schedule_total) sa_schedule_page_dict['committeeName'] = f3x_data[ 'committeeName'] sa_schedule_page_dict['lineNumber'] = sa_schedule_dict[ 'lineNumber'] sa_outfile = md5_directory + '/SA/' + 'page_' + str( sa_page_no) + '.pdf' pypdftk.fill_form(sa_infile, sa_schedule_page_dict, sa_outfile) pypdftk.concat(directory_files(md5_directory + 'SA/'), md5_directory + 'SA/all_pages.pdf') f3x_data_summary['PAGESTR'] = "PAGE " + str(page_no) + " / " + str( total_no_of_pages) pypdftk.fill_form(infile, f3x_data_summary, outfile) shutil.copy(outfile, md5_directory + 'F3X.pdf') os.remove(md5_directory + json_file_md5 + '_temp.pdf') # pypdftk.concat(directory_files(md5_directory + 'SA/'), md5_directory + 'SA/all_pages.pdf') pypdftk.concat( [md5_directory + 'F3X.pdf', md5_directory + 'SA/all_pages.pdf'], md5_directory + 'all_pages.pdf') os.remove(md5_directory + 'SA/all_pages.pdf') # push output file to AWS s3 = boto3.client('s3') s3.upload_file( md5_directory + 'all_pages.pdf', current_app.config['AWS_FECFILE_COMPONENTS_BUCKET_NAME'], md5_directory + 'all_pages.pdf', ExtraArgs={ 'ContentType': "application/pdf", 'ACL': "public-read" }) response = { # 'file_name': '{}.pdf'.format(json_file_md5), 'pdf_url': current_app.config['PRINT_OUTPUT_FILE_URL'].format(json_file_md5) + 'all_pages.pdf' } if flask.request.method == "POST": envelope = common.get_return_envelope(data=response) status_code = status.HTTP_201_CREATED return flask.jsonify(**envelope), status_code else: if flask.request.method == "POST": envelope = common.get_return_envelope( 'false', 'JSON file is missing from your request') status_code = status.HTTP_400_BAD_REQUEST return flask.jsonify(**envelope), status_code
def print_pdftk( stamp_print="", page_count=False, file_content=None, begin_image_num=None, silent_print=False, filing_timestamp=None, rep_id=None, ): # check if json_file_name is in the request try: silent_print = silent_print txn_img_num = begin_image_num filing_timestamp = filing_timestamp if "json_file" in request.files or (page_count and file_content): if "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 f1m_json = json.load( open(current_app.config["REQUEST_FILE_LOCATION"].format( json_file_md5))) # if page_count is True then return from here elif page_count and file_content: response = {"total_pages": 1} # if flask.request.method == "POST": envelope = common.get_return_envelope(data=response) return flask.jsonify(**envelope), status.HTTP_200_OK elif silent_print and begin_image_num and file_content: json_file_md5 = md5_for_text(file_content) f1m_json = json.loads(file_content) md5_directory = current_app.config["OUTPUT_DIR_LOCATION"].format( json_file_md5) # deleting directory if it exists and has any content delete_directory(md5_directory) os.makedirs(md5_directory, exist_ok=True) infile = current_app.config["FORM_TEMPLATES_LOCATION"].format( "F1M") outfile = md5_directory + json_file_md5 + "_temp.pdf" # setting timestamp and imgno to empty as these needs to show up after submission if stamp_print != "stamp": f1m_json["FILING_TIMESTAMP"] = "" f1m_json["IMGNO"] = "" # read data from json file f1m_data = f1m_json["data"] # adding txn_img_num if silent_print is True if silent_print: f1m_data["IMGNO"] = txn_img_num if filing_timestamp: f1m_data["FILING_TIMESTAMP"] = filing_timestamp name_list = [ "LastName", "FirstName", "MiddleName", "Prefix", "Suffix" ] # build treasurer name to map it to PDF template treasurerFullName = "" for item in name_list: item = "treasurer" + item if f1m_data.get(item): treasurerFullName += f1m_data.get(item) + " " f1m_data["treasurerFullName"] = treasurerFullName[:-1] f1m_data["treasurerName"] = ( f1m_data.get("treasurerLastName", "") + ", " + f1m_data.get("treasurerFirstName", "")) f1m_data["treasurerName"] = ( f1m_data["treasurerName"].strip().rstrip(",").strip()) f1m_data["efStamp"] = "[Electronically Filed]" if "candidates" in f1m_data: for candidate in f1m_data["candidates"]: candidateFullName = "" for item in name_list: item = "candidate" + item if f1m_data.get(item): candidateFullName += f1m_data.get(item) + " " f1m_data["candidateName" + str( candidate["candidateNumber"])] = candidateFullName[:-1] f1m_data["candidateOffice" + str(candidate["candidateNumber"] )] = candidate["candidateOffice"] f1m_data["candidateStateDist" + str(candidate["candidateNumber"])] = "/ ".join( map( str, [ candidate["candidateState"], candidate["candidateDistrict"], ], )) f1m_data["contributionDate" + str(candidate["candidateNumber"] )] = candidate["contributionDate"] os.makedirs(md5_directory + str(f1m_data["reportId"]) + "/", exist_ok=True) infile = current_app.config["FORM_TEMPLATES_LOCATION"].format( "F1M") pypdftk.fill_form(infile, f1m_data, outfile) shutil.copy(outfile, md5_directory + str(f1m_data["reportId"]) + "/F1M.pdf") os.remove(outfile) # 'file_name': '{}.pdf'.format(json_file_md5), response = {"total_pages": 1} if not page_count: s3 = boto3.client("s3") extraArgs = { "ContentType": "application/pdf", "ACL": "public-read" } if silent_print: response["pdf_url"] = ( current_app. config["AWS_FECFILE_COMPONENTS_BUCKET_NAME"], rep_id + ".pdf", ) s3.upload_file( md5_directory + str(f1m_data["reportId"]) + "/F1M.pdf", current_app. config["AWS_FECFILE_COMPONENTS_BUCKET_NAME"], rep_id + ".pdf", ExtraArgs=extraArgs, ) else: response["pdf_url"] = ( current_app.config["PRINT_OUTPUT_FILE_URL"].format( json_file_md5) + "F1M.pdf", ) s3.upload_file( md5_directory + str(f1m_data["reportId"]) + "/F1M.pdf", current_app. config["AWS_FECFILE_COMPONENTS_BUCKET_NAME"], md5_directory + "F1M.pdf", ExtraArgs=extraArgs, ) # return response # if flask.request.method == "POST": envelope = common.get_return_envelope(data=response) status_code = status.HTTP_201_CREATED return flask.jsonify(**envelope), status_code # elif silent_print: # return True, {} else: if 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: return error("Error generating print preview, error message: " + str(e))
def evaluate_row_and_build_updates(taskOrder, source_row, smart_session, sheet_id, row_id): output = "" # Set the PO Template po_template = PO_TEMPLATE[taskOrder] # Find the cell and value we want to evaluate status_cell = get_cell_by_column_name(source_row, "Package Created") status_value = status_cell.value if ((status_value is None) or (status_value is False)): # Retrieve Fields for PO Form po_cell = get_cell_by_column_name(source_row, "PO Number") po_value = po_cell.display_value quoteid_cell = get_cell_by_column_name(source_row, "Quote ID") quoteid_value = quoteid_cell.display_value email_cell = get_cell_by_column_name(source_row, "Contact Email") email_value = email_cell.display_value virtualacct_cell = get_cell_by_column_name(source_row, "Virtual Account") virtualacct_value = virtualacct_cell.display_value acctmanager_cell = get_cell_by_column_name(source_row, "Account Manager") acctmanager_value = acctmanager_cell.display_value # Skip if we are missing information if ((po_value is None) or (quoteid_value is None) or (email_value is None) or (virtualacct_value is None) or (acctmanager_value is None)): logging.warning( "WARNING: Incomplete Information - Skipping PO: %s", po_value) output += ("WARNING: Incomplete Information - Skipping PO: %s" % po_value) output += "<BR>" else: found_estimate = False response = smart_session.Attachments.list_row_attachments( sheet_id, row_id, include_all=True) attachments = response.data EstimateFileName = po_value + " - Estimate.xls" for attachment in attachments: if ((attachment.mime_type == "application/vnd.ms-excel") or (attachment.mime_type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" )): attachmentobj = smart_session.Attachments.get_attachment( sheet_id, attachment.id) response = requests.get(attachmentobj.url) open(EstimateFileName, 'wb').write(response.content) if (response.status_code == 200): found_estimate = True if (found_estimate == False): logging.warning( "WARNING: Incomplete Information - Skipping PO: %s", po_value) output += ( "WARNING: Incomplete Information - Skipping PO: %s" % po_value) output += "<BR>" return None, None, output logging.info("PROCESSING: PO: %s", po_value) output += ("PROCESSING: PO: %s" % po_value) output += "<BR>" data_dict = { 'DealID': RNSD[taskOrder], 'EstimateID': quoteid_value, 'PONumber': po_value, 'eDeliveryDestination': email_value, 'SmartAccount': SMARTACCOUNT[taskOrder], 'VirtualAccount': virtualacct_value, 'SpecialInstructions': '', 'AccountManager': acctmanager_value } # Filename is in the format of "CISCO-EA-001-000000001 - PO.docx" filename = (po_value + "\ -\ PO.pdf") pypdftk.fill_form(po_template, datas=data_dict, out_file=filename, flatten=True) if (send_email(data_dict) is True): # True when sent successfully new_cell = smart_session.models.Cell() new_cell.column_id = column_map["Package Created"] new_cell.value = True new_row = smart_session.models.Row() new_row.id = source_row.id new_row.cells.append(new_cell) return new_row, po_value, output return None, None, output
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_sd_line( f3x_data, md5_directory, sd_dict, sd_start_page, total_no_of_pages, total_sd_pages, totalOutstandingLoans, image_num=None, ): sd_total_balance = "0.00" sd_schedule_total = 0 page_count = 0 os.makedirs(md5_directory + "SD/", exist_ok=True) sd_infile = current_app.config["FORM_TEMPLATES_LOCATION"].format("SD") try: for line_number in sd_dict: sd_list = sd_dict.get(line_number) sd_sub_total = 0 sd_page_dict = {} sd_page_dict["committeeName"] = f3x_data.get("committeeName") sd_page_dict["totalNoPages"] = total_no_of_pages sd_page_dict["lineNumber"] = line_number for index in range(len(sd_list)): sd_schedule_total += float( sd_list[index].get("balanceAtClose")) sd_sub_total += float(sd_list[index].get("balanceAtClose")) sd_page_dict["pageNo"] = sd_start_page + page_count concat_no = index % 3 + 1 if image_num: sd_page_dict["IMGNO"] = image_num if ("creditorOrganizationName" in sd_list[index] and sd_list[index].get("creditorOrganizationName") != ""): sd_page_dict["creditorName_{}".format( concat_no)] = sd_list[index].get( "creditorOrganizationName") else: sd_page_dict["creditorName_{}".format(concat_no)] = "" for item in [ "creditorPrefix", "creditorLastName", "creditorFirstName", "creditorMiddleName", "creditorSuffix", ]: if sd_list[index].get(item) != "": sd_page_dict["creditorName_{}".format( concat_no)] += (sd_list[index].get(item) + " ") for item in [ "creditorStreet1", "creditorStreet2", "creditorCity", "creditorState", "creditorZipCode", "purposeOfDebtOrObligation", "transactionId", ]: sd_page_dict[item + "_{}".format(concat_no)] = sd_list[index].get( item) for item in [ "beginningBalance", "incurredAmount", "paymentAmount", "balanceAtClose", ]: sd_page_dict[item + "_{}".format(concat_no)] = "{0:.2f}".format( float(sd_list[index].get(item))) if index % 3 == 2 or index == len(sd_list) - 1: sd_page_dict["subTotal"] = "{0:.2f}".format(sd_sub_total) if page_count == total_sd_pages - 1: sd_page_dict["total"] = "{0:.2f}".format( sd_schedule_total) sd_page_dict[ "totalOutstandingLoans"] = totalOutstandingLoans sd_total_balance = sd_page_dict[ "totalBalance"] = "{0:.2f}".format( sd_schedule_total + float(totalOutstandingLoans)) sd_outfile = (md5_directory + "SD" + "/page_" + str(sd_page_dict["pageNo"]) + ".pdf") pypdftk.fill_form(sd_infile, sd_page_dict, sd_outfile) del_j = 1 while del_j <= index % 3 + 1: for item in [ "creditorName", "creditorStreet1", "creditorStreet2", "creditorCity", "creditorState", "creditorZipCode", "purposeOfDebtOrObligation", "transactionId", "beginningBalance", "incurredAmount", "paymentAmount", "balanceAtClose", ]: del sd_page_dict[item + "_{}".format(del_j)] del_j += 1 if path.isfile(md5_directory + "SD/all_pages.pdf"): pypdftk.concat( [ md5_directory + "SD/all_pages.pdf", md5_directory + "SD" + "/page_" + str(sd_page_dict["pageNo"]) + ".pdf", ], md5_directory + "SD/temp_all_pages.pdf", ) os.rename( md5_directory + "SD/temp_all_pages.pdf", md5_directory + "SD/all_pages.pdf", ) else: os.rename( md5_directory + "SD" + "/page_" + str(sd_page_dict["pageNo"]) + ".pdf", md5_directory + "SD/all_pages.pdf", ) page_count += 1 if image_num: image_num += 1 sd_sub_total = 0 return sd_total_balance, image_num except: traceback.print_exception(*sys.exc_info())
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())