inventree / inventree-brother-plugin

Label printing plugin for Brother series printers
MIT License
16 stars 12 forks source link

Stop hardcoding parts of the printer identifier #39

Open MechanicalMink opened 1 month ago

MechanicalMink commented 1 month ago

I think am inches away from being able to run this out of a docker container. I can now print from brother_ql cli from the container running inventree. Unfortunately, since this plugin hardcodes either ip://or usb:// into the printer identifier, I have no way of getting it to work with the way I've routed the printer into docker. I need to be able to provide file:///dev/usb/lp0as the printer identifier. I know for a fact that brother_ql can function with printer identifiers in that format, so we just have to not mangle the identifier provided by the user and then we're good.

I would make this change on my own, and I have tried for several hours, but I don't understand the inventree plugin architecture well enough to get my version to load properly.

MechanicalMink commented 1 month ago

I've done the exact opposite of what I've asked for and completely hardcoded a brother plugin to work on /dev/usb/lp0. It is currently set up to use 62mm endless tape and is only compatible with QL-600s and QL-570s. Please use this in conjunction with my debian image of inventree-server, and remember to go into the inventree virtual environment in that image and downgrade PIL to 9.5.0 as well as install brother_ql via PIP. I really really really hope somebody else takes this the rest of the way and makes the main plugin backend agnostic for USB printing.

brother_usb.py

"""Simple attempt at creating a usb driver for the Brother QL-600.

"""

from rest_framework import serializers

from InvenTree.settings import BASE_DIR
from plugin import InvenTreePlugin
from plugin.mixins import LabelPrintingMixin

# Required brother_ql libs
from brother_ql.conversion import convert
from brother_ql.raster import BrotherQLRaster
from brother_ql.backends import backend_factory, guess_backend
from brother_ql.models import ALL_MODELS
from brother_ql.labels import ALL_LABELS, FormFactor
from brother_ql import create_label

# Image library
from PIL import Image

# translation
from django.utils.translation import gettext_lazy as _

class BrotherQLUSB(LabelPrintingMixin, InvenTreePlugin):
    """Plugin to provide endpoint for Brother QL label printers over USB,
        Specifically the QL-600."""

    NAME = 'Brother QL USB'
    SLUG = 'brotherqlusb'
    TITLE = 'Brother QL USB'
    DESCRIPTION = 'Plugin to provide endpoint for Brother QL label printers over USB'
    AUTHOR = 'Jacob Vorperian'
    VERSION = '0.0.1'
    BLOCKING_PRINT = True

#    printer_model = BrotherQLRaster(model='QL-570')
#    printer_id = 'file:///dev/usb/lp0'
#
#    params = {
#        'qlr': printer_model,
#        'label': '62',
#        'cut': True,
#        'rotate': 0,
#        'compress': False,
#        'hw': True,
#        'red': False,
#        'threshold': 50.0,
#        'dither': False,
#     }

    class PrintingOptionsSerializer(serializers.Serializer):
        """Serializer to return printing options."""

        amount = serializers.IntegerField(required=False, default=1)

    def print_label(self, **kwargs):
        printer_model = BrotherQLRaster(model='QL-570')
        printer_id = 'file:///dev/usb/lp0'

        params = {
            'qlr': printer_model,
            'label': '62',
            'cut': True,
            'rotate': 0,
            'compress': False,
            'hw': True,
            'red': False,
            'threshold': 50.0,
            'dither': False,
        }

        print(f"Printing Label: {kwargs['filename']} (User: {kwargs['user']})")
        png_file = kwargs.get('png_file', None)
        if not png_file:
              pdf_data = kwargs['pdf_data']
              png_file = self.render_to_png(label=None, pdf_data=pdf_data)
        # png_file = Image.open(r"../../../data/plugins/PNG_Test.png")
        try:
            selected_backend = guess_backend(printer_id)
        except ValueError:
            print("Couldn't guess printer backend from string descriptor")
        BACKEND_CLASS = backend_factory(selected_backend)['backend_class']

        create_label(params['qlr'], png_file, params['label'],
                    params['threshold'], params['cut'],
                    params['dither'], params['red'], rotate=params['rotate'])
        try:
            print("BrotherQLUSB-- Attempting to print label")
            printer_backend = BACKEND_CLASS(printer_id)
            printer_backend.write(params['qlr'].data)
            printer_backend.dispose()
            del printer_backend
        except Exception as e:
            print("Label failed to print for reason: %s" % e)
SchrodingersGat commented 1 month ago

@MechanicalMink thanks for the comments. It may be that this does not receive much attention if no one else is in your situation.. I'd make a friendly suggestion that you make a PR for this with your changes:

https://opensource.com/article/19/7/create-pull-request-github

MechanicalMink commented 1 month ago

@SchrodingersGat Happy to, that will take me another couple of days to get around to figuring out. Yay work! Again, more than happy to step out of the way for anybody else to take this into a pull request in the mean time.

SchrodingersGat commented 1 month ago

@MechanicalMink happy to help you get up and running with this - the process can be daunting but is really not that bad. :)