Building Owner : Identity Number / Tax Number : Neighborhood : Pafta : Island : Parcel : Building Type : Project Number : Building Area : Total Excavation : I want to create a pdf that is automatically saved with the information I entered to the building owner by entering this data through a form and pressing the create pdf button, I want to get an output in the form of this screenshot I uploaded, first of all I want the date to be automatically written on the right side, I also want the license project fee to be written in the number section - I want the information I entered to the building owner to be printed in the continuation of this, and the information I entered in the name and parcel in the subject section, I want the information I entered in the name and parcel to be printed between the quotation marks in the form of "" island "" parcel, In the amount section in the table, I want the calculation of the information I entered in the building area x the number of projects x 0. 09 formula to be printed, and finally, I want to print the same as the amount in the quotation part in the sentence, I want to print the same as the amount in the sentence, please create error-free code using python in detail, suitable for python.
Assistant
To create a PDF from a form input in Python, you can use the Flask framework for the web form and the reportlab library to generate the PDF. Below, I'll provide you with code that does the following:
Creates a web form for input.
Takes user inputs.
Generates a PDF with the specified formatting and data, including your additional requirements.
Prerequisites
You'll need to make sure you have Flask and ReportLab installed. You can install them using pip:
bash
pip install Flask reportlab
Code
Here's a complete example of how you can implement this:
python
from flask import Flask, render_template, request, redirect, url_for
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
import os
from datetime import datetime
app = Flask(__name__)
# Folder to save PDF files
os.makedirs('pdfs', exist_ok=True)
@app.route('/', methods=['GET', 'POST'])
def form():
if request.method == 'POST':
# Collect input data
owner = request.form['owner']
identity_number = request.form['identity_number']
tax_number = request.form['tax_number']
neighborhood = request.form['neighborhood']
pafta = request.form['pafta']
island = request.form['island']
parcel = request.form['parcel']
building_type = request.form['building_type']
project_number = request.form['project_number']
building_area = float(request.form['building_area'])
num_projects = int(request.form['num_projects'])
# Calculate license project fee and amount
license_fee = building_area * num_projects * 0.09
date_format = datetime.now().strftime("%Y-%m-%d")
# Generate PDF
pdf_filename = f'pdfs/{owner.replace(" ", "_")}_project_details.pdf'
c = canvas.Canvas(pdf_filename, pagesize=letter)
width, height = letter
# Writing to PDF
c.drawString(400, 750, date_format)
c.drawString(50, 720, f"License Project Fee: {license_fee:.2f}")
c.drawString(50, 700, f"Building Owner: {owner}")
c.drawString(50, 680, f"Identity Number: {identity_number}")
c.drawString(50, 660, f"Tax Number: {tax_number}")
c.drawString(50, 640, f"Neighborhood: {neighborhood}")
c.drawString(50, 620, f"Pafta: {pafta}")
c.drawString(50, 600, f"Island: {island}")
c.drawString(50, 580, f"Parcel: {parcel}")
c.drawString(50, 560, f"Building Type: {building_type}")
c.drawString(50, 540, f"Project Number: {project_number}")
# Subject section
subject = f'"{island}" parcel'
c.drawString(50, 520, f"Subject: {subject}")
# Amount section
c.drawString(50, 500, f"Amount: {license_fee:.2f}")
# Append same amount in quotation
c.drawString(50, 480, f'The total amount is "{license_fee:.2f}".')
c.save()
return redirect(url_for('success', filename=os.path.basename(pdf_filename)))
return render_template('form.html')
@app.route('/success/<filename>')
def success(filename):
return f"PDF generated successfully: <a href='/pdfs/{filename}'>{filename}</a>"
if __name__ == "__main__":
app.run(debug=True)
HTML Form
Create an HTML file named form.html inside a templates folder. Here’s a simple version of the HTML form:
Save the above Python code in a file named app.py.
Create a folder named templates in the same directory as app.py, and place form.html inside it.
Run the Flask application:
bash
python app.py
Open your browser and go to http://127.0.0.1:5000.
Fill out the form and submit it. Upon successful submission, you'll receive a link to download the generated PDF.
This implementation takes care of the requirements you've specified, including formatting, calculations, and dynamic data insertion into the PDF. Adjust styling and layout as per your needs.