diff --git a/py_app/app/__pycache__/routes.cpython-311.pyc b/py_app/app/__pycache__/routes.cpython-311.pyc index b556339..a2bc35c 100644 Binary files a/py_app/app/__pycache__/routes.cpython-311.pyc and b/py_app/app/__pycache__/routes.cpython-311.pyc differ diff --git a/py_app/app/routes.py b/py_app/app/routes.py index ddf48c3..b81f66b 100644 --- a/py_app/app/routes.py +++ b/py_app/app/routes.py @@ -4,6 +4,8 @@ from datetime import datetime, timedelta from flask import Blueprint, render_template, redirect, url_for, request, flash, session, current_app, jsonify from .models import User from . import db +from reportlab.lib.pagesizes import letter +from reportlab.pdfgen import canvas bp = Blueprint('main', __name__) @@ -61,7 +63,7 @@ def settings(): # Load external database settings from the instance folder external_settings = {} settings_file = os.path.join(current_app.instance_path, 'external_server.conf') - if os.path.exists(settings_file): + if (os.path.exists(settings_file)): with open(settings_file, 'r') as f: for line in f: key, value = line.strip().split('=', 1) @@ -385,4 +387,37 @@ def save_template(): data = request.get_json() # Replace with logic to save the template to the database print(f"Saving template: {data}") - return jsonify({'message': 'Template saved successfully!'}) \ No newline at end of file + return jsonify({'message': 'Template saved successfully!'}) + +@bp.route('/generate_pdf', methods=['POST']) +def generate_pdf(): + data = request.get_json() + width = data.get('width', 100) # Default width in mm + height = data.get('height', 50) # Default height in mm + columns = data.get('columns', []) + + # Convert dimensions from mm to points (1 mm = 2.83465 points) + width_points = width * 2.83465 + height_points = height * 2.83465 + + # Ensure the /static/label_templates folder exists + label_templates_folder = os.path.join(current_app.root_path, 'static', 'label_templates') + os.makedirs(label_templates_folder, exist_ok=True) + + # Define the path for the PDF file + pdf_file_path = os.path.join(label_templates_folder, 'label_template.pdf') + + # Create a PDF file + c = canvas.Canvas(pdf_file_path, pagesize=(width_points, height_points)) + + # Add content to the PDF + c.drawString(10, height_points - 20, "Label Template") + y_position = height_points - 40 + for column in columns: + c.drawString(10, y_position, f"Column: {column}") + y_position -= 20 + + # Save the PDF + c.save() + + return jsonify({'message': 'PDF generated successfully!', 'pdf_path': f'/static/label_templates/label_template.pdf'}) \ No newline at end of file diff --git a/py_app/app/static/script.js b/py_app/app/static/script.js index 449d893..df51ff9 100644 --- a/py_app/app/static/script.js +++ b/py_app/app/static/script.js @@ -303,4 +303,43 @@ document.addEventListener('DOMContentLoaded', () => { }) .catch(error => console.error('Error saving template:', error)); }); + + document.getElementById('generate-pdf-btn').addEventListener('click', () => { + const width = document.getElementById('label-width').value; + const height = document.getElementById('label-height').value; + const selectedColumns = Array.from(document.querySelectorAll('#columns-container input[type="checkbox"]:checked')) + .map(checkbox => checkbox.value); + + if (!width || !height || selectedColumns.length === 0) { + alert('Please set dimensions and select at least one column.'); + return; + } + + const data = { + width: parseFloat(width), + height: parseFloat(height), + columns: selectedColumns + }; + + fetch('/generate_pdf', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(data) + }) + .then(response => response.json()) + .then(result => { + alert(result.message); + console.log('PDF Path:', result.pdf_path); + + // Provide a link to download the PDF + const downloadLink = document.createElement('a'); + downloadLink.href = result.pdf_path; + downloadLink.textContent = 'Download Generated PDF'; + downloadLink.target = '_blank'; + document.getElementById('label-preview').appendChild(downloadLink); + }) + .catch(error => console.error('Error generating PDF:', error)); + }); }); \ No newline at end of file diff --git a/py_app/app/templates/create_template.html b/py_app/app/templates/create_template.html index 020c300..c450b94 100644 --- a/py_app/app/templates/create_template.html +++ b/py_app/app/templates/create_template.html @@ -22,7 +22,6 @@ - @@ -40,6 +39,13 @@ + +