Skip to content

Instantly share code, notes, and snippets.

@hansheng0512
Created November 29, 2021 15:42
Show Gist options
  • Save hansheng0512/026177a904e55c56653cd82bc7f08d06 to your computer and use it in GitHub Desktop.
Save hansheng0512/026177a904e55c56653cd82bc7f08d06 to your computer and use it in GitHub Desktop.
def generate_pdf(data):
response = select_one_data("SELECT * FROM pdf")
if(not response["success"]):
return jsonify({"msg": "Database Error", "error_msg": response["error_msg"], "success": False})
subprocess.call(["php", "tfpdf.php"], shell=True)
pdf = FPDF('P', 'in', 'A4');
pdf.add_page()
# Chinese font
pdf.add_font('Simsun', '', os.path.join(FONT_PATH, "SIMSUN.ttf"), uni=True)
pdf.set_margins(0, 0, 0)
pdf.ln(3.75)
pdf.rect(0.25, 3.75, 7.75, 4)
pdf.image(os.path.join(LOGO_ROOT, 'main.png'), 0.75, 4, 1)
pdf.set_font('Times', 'B', 4)
pdf.ln(0.85)
pdf.cell(0.925)
pdf.cell(0, 0, f"{data['team_ssv_id']}", 0)
# Header
pdf.ln(-0.775)
pdf.set_font('Simsun', '', 15)
pdf.cell(1.875)
pdf.cell(0, 0, f"孝心老人福利收留协会", 0)
pdf.set_font('Helvetica', '', 12)
pdf.ln(0.225)
pdf.cell(1.875)
pdf.cell(0, 0, f"XIAO XIN SERDANG OLD FOLKS HOME", 0)
pdf.set_font('Helvetica', 'B', 11)
pdf.ln(0.175)
pdf.cell(1.875)
pdf.cell(0, 0, f"PERTUBUHAN KEBAJIKAN ORANG TUA XIAO XIN", 0)
pdf.set_font('Times', '', 7)
pdf.ln(0.15)
pdf.cell(1.875)
pdf.cell(0, 0, f"NO 1687, JALAN 18/46 TAMAN SRI SEDANG, 43300 SERI KEMBANGAN, SELANGOR", 0)
pdf.set_font('Helvetica', '', 7)
pdf.ln(0.15)
pdf.cell(1.875)
pdf.cell(0, 0, f"Contact: +6016-4738115 (LK)/+6016-8851687 (YT) Volunteer: xxxxxxx", 0)
# pdf.ln(0.15)
# pdf.cell(1.875)
# pdf.cell(0, 0, f"VOLUNTEER", 0)
pdf.set_font('Simsun', '', 11)
pdf.ln(-0.7)
pdf.cell(6.275)
pdf.cell(0, 0, response["data"][0], 0)
pdf.set_font('Times', '', 10)
pdf.ln(0.175)
pdf.cell(6)
pdf.cell(0, 0, "OFFICIAL RECEIPT", 0)
pdf.line(6, 4.5, 7.25, 4.5)
pdf.set_font('Times', '', 14)
pdf.ln(0.275)
pdf.cell(6)
pdf.cell(0, 0, f"No {data['recipe_no']}", 0)
# Name and Contact
pdf.set_font('Simsun', '', 11)
pdf.ln(0.6)
pdf.cell(1)
pdf.cell(0, 0, response["data"][1], 0)
pdf.set_font('Times', '', 10)
pdf.ln(0.175)
pdf.cell(1)
pdf.cell(0, 0, f"NAME : {data['customer_name']}", 0)
pdf.line(1.575, 5.5, 7.25, 5.5)
pdf.set_font('Simsun', '', 10)
pdf.ln(0.225)
pdf.cell(1)
pdf.cell(0, 0, response["data"][1], 0)
pdf.set_font('Times', '', 10)
pdf.ln(0.175)
pdf.cell(1)
pdf.cell(0, 0, f"CONTACT : {data['cust_phone_no']}", 0)
pdf.line(1.825, 5.9, 7.25, 5.9)
# Checkbox
pdf.ln(0.225)
pdf.cell(1)
pdf.cell(0.25, 0.25, " X" if (data["cash_donation"]) else "", 1)
pdf.set_font('Simsun', '', 9)
pdf.ln(0.05)
pdf.cell(1.25)
pdf.cell(0, 0, response["data"][3], 0)
pdf.set_font('Times', '', 9)
pdf.ln(0.175)
pdf.cell(1.25)
pdf.cell(0, 0, "DONATION (CASH)", 0)
pdf.ln(-0.225)
pdf.cell(2.575)
pdf.cell(0.25, 0.25, " X" if (data["medicine"]) else "", 1)
pdf.set_font('Simsun', '', 9)
pdf.ln(0.05)
pdf.cell(2.825)
pdf.cell(0, 0, response["data"][4], 0)
pdf.set_font('Times', '', 9)
pdf.ln(0.175)
pdf.cell(2.825)
pdf.cell(0, 0, "MEDICINE", 0)
pdf.ln(-0.225)
pdf.cell(3.625)
pdf.cell(0.25, 0.25, " X" if (data["coffin"]) else "", 1)
pdf.set_font('Simsun', '', 9)
pdf.ln(0.05)
pdf.cell(3.875)
pdf.cell(0, 0, response["data"][5], 0)
pdf.set_font('Times', '', 9)
pdf.ln(0.175)
pdf.cell(3.875)
pdf.cell(0, 0, "COFFIN", 0)
# Bank Account
pdf.ln(-0.225)
pdf.cell(5)
pdf.image(os.path.join(ASSEST_PATH, "round_rect.png"), 4.5, 6.05, 2.75, 0.7)
# pdf.cell(2.25, 0.675, "", 1) #square rectangle
pdf.ln(0.175)
pdf.cell(4.575)
pdf.set_font('Times', 'BI', 10)
pdf.cell(0, 0, "Bank Account", 0)
pdf.ln(0.15)
pdf.cell(4.575)
pdf.cell(0, 0, f"{data['bank_owner_name']}", 0)
pdf.ln(0.15)
pdf.cell(4.575)
pdf.cell(0, 0, f"{data['bank_name']} - {data['bank_account_number']}", 0)
# Amount
pdf.ln(-0.02)
pdf.cell(1)
pdf.cell(3, 0.325, "", 1)
pdf.ln(0.16)
pdf.cell(1.05)
pdf.set_font('Times', '', 13)
pdf.cell(0, 0, f"RM {data['amount']}", 0)
pdf.ln(0.35)
pdf.cell(0.95)
pdf.set_font('Times', '', 10)
pdf.cell(0, 0, f"Cash/Cheque No : {data['cheque_no']}", 0)
# Received By
pdf.ln(0.095)
pdf.cell(4.125)
pdf.set_font('Simsun', '', 10)
pdf.cell(0, 0, response["data"][6], 0)
pdf.ln(0.175)
pdf.cell(4.125)
pdf.set_font('Times', '', 12)
pdf.cell(0, 0, "Received By : LK", 0)
pdf.line(5.125, 7.4, 7.25, 7.4)
# Date
pdf.ln(-2.325)
pdf.cell(5.6)
pdf.set_font('Simsun', '', 8)
pdf.cell(0, 0, response["data"][7], 0)
pdf.ln(0.125)
pdf.cell(5.6)
pdf.set_font('Times', '', 8)
pdf.cell(0, 0, f"DATE : {data['donation_date']}", 0)
pdf.line(6, 5.175, 7.25, 5.175)
# Output PDF
pdf_name = f"{data['username']}_{randint(1, 999999)}.pdf"
full_output_path = os.path.join(PDF_PATH, pdf_name)
pdf.output(full_output_path, 'F')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment