zpl-a-pdf / app.py
tomaseo2022's picture
Update app.py
a3a561c
import gradio as gr
import requests
import os
import tempfile
from io import BytesIO
def convert_zpl_to_pdf(zpl_string):
labelary_api = 'http://api.labelary.com/v1/printers/8dpmm/labels/4x6/0/'
headers = {'Accept': 'application/pdf'}
response = requests.post(labelary_api, headers=headers, data=zpl_string.encode('utf-8'), stream=True)
if response.status_code == 200:
# Usamos tempfile para crear un archivo temporal seguro
# mkstemp() devuelve un descriptor de archivo y la ruta del archivo temporal
fd, path = tempfile.mkstemp(suffix='.pdf')
try:
with os.fdopen(fd, 'wb') as tmp:
# Escribimos el contenido y cerramos el archivo
tmp.write(response.content)
# Devolvemos la ruta del archivo
return path
except Exception as e:
os.close(fd)
os.unlink(path)
raise e
else:
raise Exception(f"Error durante la conversi贸n de ZPL a PDF: {response.text}")
iface = gr.Interface(
fn=convert_zpl_to_pdf,
inputs=gr.Textbox(lines=10, placeholder="Pega aqu铆 tu c贸digo ZPL"),
outputs=gr.File(label="Descargar PDF"),
title="Conversor de ZPL a PDF",
description="Esta herramienta convierte c贸digo ZPL en un archivo PDF usando la API de Labelary."
)
if __name__ == "__main__":
iface.launch()