added report lab options
This commit is contained in:
Binary file not shown.
@@ -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!'})
|
||||
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'})
|
||||
@@ -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));
|
||||
});
|
||||
});
|
||||
@@ -22,7 +22,6 @@
|
||||
<label for="label-height">Height (mm):</label>
|
||||
<input type="number" id="label-height" name="label_height" required>
|
||||
</div>
|
||||
<button type="button" id="set-dimensions-btn" class="btn">Set Dimensions</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
@@ -40,6 +39,13 @@
|
||||
<!-- Columns will be dynamically populated here -->
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<hr style="margin: 20px 0;">
|
||||
|
||||
<!-- Generate PDF Template -->
|
||||
<div>
|
||||
<button type="button" id="generate-pdf-btn" class="btn">Generate PDF Template</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -51,7 +57,6 @@
|
||||
<div id="label-preview" style="border: 1px solid #ddd; padding: 10px; min-height: 200px;">
|
||||
<!-- Label preview will be dynamically updated here -->
|
||||
</div>
|
||||
<button type="button" id="save-template-btn" class="btn">Save Template</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -4,4 +4,5 @@ Werkzeug
|
||||
gunicorn
|
||||
flask-sqlalchemy
|
||||
pyodbc
|
||||
mariadb
|
||||
mariadb
|
||||
reportlab
|
||||
Reference in New Issue
Block a user