agritheory / beam

General Purpose 2D barcode scanning for ERPNext
https://agritheory.com/documentation/beam/
Other
23 stars 11 forks source link

Add ZPL jinja method and preview technique #129

Closed agritheory closed 2 months ago

agritheory commented 3 months ago

Use the ZPL to image API here for a preview inside a Jinja method

https://pypi.org/project/zplgrf/

Also document a workflow for zebrafy as well for the writing/design of a label.

agritheory commented 3 months ago

First pass of testing, neither zebrafy or zplgrf can convert html. Trying to convert an image to ZPL might work, but it seems to make more sense to try our own design page. https://rawgit.com/germanger/zebra-html5-designer/master/index.html

agritheory commented 3 months ago

I was able to convert an HTML format to an image, which we can then convert to ZPL via zebrafy.

Untitled

import frappe
from frappe.utils.jinja import get_jinja_hooks
from frappe.utils.safe_exec import get_safe_globals
from jinja2 import DebugUndefined, Environment

import imgkit
import base64
from io import BytesIO

def image_preview(print_format, doc):
    print_format = frappe.get_doc('Print Format', print_format)
    output = ""
    e = Environment(undefined=DebugUndefined)
    e.globals.update(get_safe_globals())
    e.globals.update(get_jinja_hooks()[0])
    template = e.from_string(print_format.html)
    output = template.render(doc=doc)
    image = BytesIO(imgkit.from_string(f"<html>{output}</html>", False))
    return base64.b64encode(image.getvalue()).decode("ascii")

Print Format:

<img src="data:image/png;base64,{{ image_preview('Item Barcode', doc) }}"/>