updated printing solutions
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
import mariadb
|
||||
from flask import current_app, request, render_template, session, redirect, url_for
|
||||
from flask import current_app, request, render_template, session, redirect, url_for, jsonify, make_response
|
||||
import csv, os, tempfile
|
||||
from reportlab.lib.pagesizes import letter
|
||||
from reportlab.pdfgen import canvas
|
||||
from reportlab.lib.units import cm
|
||||
from reportlab.graphics.barcode import code128
|
||||
import io
|
||||
|
||||
def get_db_connection():
|
||||
settings_file = current_app.instance_path + '/external_server.conf'
|
||||
@@ -148,3 +153,68 @@ def import_locations_csv_handler():
|
||||
elif 'csv_locations' in session:
|
||||
locations = session['csv_locations']
|
||||
return render_template('import_locations_csv.html', report=report, locations=locations)
|
||||
|
||||
def generate_location_label_pdf():
|
||||
"""Generate PDF for location barcode label (8x4cm)"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
location_code = data.get('location_code', '')
|
||||
|
||||
if not location_code:
|
||||
return jsonify({'error': 'Location code is required'}), 400
|
||||
|
||||
# Create PDF in memory
|
||||
buffer = io.BytesIO()
|
||||
|
||||
# Create PDF with 8x4cm page size (width x height)
|
||||
page_width = 8 * cm
|
||||
page_height = 4 * cm
|
||||
|
||||
c = canvas.Canvas(buffer, pagesize=(page_width, page_height))
|
||||
|
||||
# Generate Code128 barcode
|
||||
barcode = code128.Code128(location_code, barWidth=1.0, humanReadable=False)
|
||||
|
||||
# Calculate the desired barcode dimensions (fill most of the label)
|
||||
desired_barcode_width = 7 * cm # Almost full width
|
||||
desired_barcode_height = 2.5 * cm # Most of the height
|
||||
|
||||
# Calculate scaling factor to fit the desired width
|
||||
scale = desired_barcode_width / barcode.width
|
||||
|
||||
# Calculate actual dimensions after scaling
|
||||
actual_width = barcode.width * scale
|
||||
actual_height = barcode.height * scale
|
||||
|
||||
# Center the barcode on the label
|
||||
barcode_x = (page_width - actual_width) / 2
|
||||
barcode_y = (page_height - actual_height) / 2 + 0.3 * cm # Slightly above center for text space
|
||||
|
||||
# Draw barcode with scaling
|
||||
c.saveState()
|
||||
c.translate(barcode_x, barcode_y)
|
||||
c.scale(scale, scale)
|
||||
barcode.drawOn(c, 0, 0)
|
||||
c.restoreState()
|
||||
|
||||
# Add location code text below barcode
|
||||
c.setFont("Helvetica-Bold", 10)
|
||||
text_width = c.stringWidth(location_code, "Helvetica-Bold", 10)
|
||||
text_x = (page_width - text_width) / 2
|
||||
text_y = barcode_y - 0.5 * cm # Below the barcode
|
||||
c.drawString(text_x, text_y, location_code)
|
||||
|
||||
# Finalize PDF
|
||||
c.save()
|
||||
|
||||
# Prepare response
|
||||
buffer.seek(0)
|
||||
response = make_response(buffer.getvalue())
|
||||
response.headers['Content-Type'] = 'application/pdf'
|
||||
response.headers['Content-Disposition'] = f'inline; filename=location_{location_code}_label.pdf'
|
||||
|
||||
return response
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error generating location label PDF: {e}")
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
Reference in New Issue
Block a user