diff --git a/InvenTree/machine/machine_types/label_printer.py b/InvenTree/machine/machine_types/label_printer.py index 73fa57f2d7..bfbff04a45 100644 --- a/InvenTree/machine/machine_types/label_printer.py +++ b/InvenTree/machine/machine_types/label_printer.py @@ -180,6 +180,10 @@ class LabelPrinterBaseDriver(BaseDriver): Keyword Arguments: pdf_data (bytes): The pdf document as bytes (optional) dpi (int): The dpi used to render the image (optional) + use_cairo (bool): Whether to use the pdftocairo backend for rendering which provides better results in tests, + see [#6488](https://github.com/inventree/InvenTree/pull/6488) for details. If False, pdftoppm is used (default: True) + pdf2image_kwargs (dict): Additional keyword arguments to pass to the + [`pdf2image.convert_from_bytes`](https://pdf2image.readthedocs.io/en/latest/reference.html#pdf2image.pdf2image.convert_from_bytes) method (optional) """ label.object_to_print = item png = self.machine_plugin.render_to_png(label, request, **kwargs) diff --git a/InvenTree/plugin/base/label/mixins.py b/InvenTree/plugin/base/label/mixins.py index 4430578272..c4bffc9801 100644 --- a/InvenTree/plugin/base/label/mixins.py +++ b/InvenTree/plugin/base/label/mixins.py @@ -76,7 +76,19 @@ class LabelPrintingMixin: raise ValidationError(_('Error rendering label to HTML')) def render_to_png(self, label: LabelTemplate, request=None, **kwargs): - """Render this label to PNG format.""" + """Render this label to PNG format. + + Arguments: + label: The LabelTemplate object to render + request: The HTTP request object which triggered this print job + Keyword Arguments: + pdf_data: The raw PDF data of the rendered label (if already rendered) + dpi: The DPI to use for the PNG rendering + use_cairo (bool): Whether to use the pdftocairo backend for rendering which provides better results in tests, + see [#6488](https://github.com/inventree/InvenTree/pull/6488) for details. If False, pdftoppm is used (default: True) + pdf2image_kwargs (dict): Additional keyword arguments to pass to the + [`pdf2image.convert_from_bytes`](https://pdf2image.readthedocs.io/en/latest/reference.html#pdf2image.pdf2image.convert_from_bytes) method (optional) + """ # Check if pdf data is provided pdf_data = kwargs.get('pdf_data', None) @@ -85,11 +97,15 @@ class LabelPrintingMixin: self.render_to_pdf(label, request, **kwargs).get_document().write_pdf() ) - dpi = kwargs.get('dpi', InvenTreeSetting.get_setting('LABEL_DPI', 300)) + pdf2image_kwargs = { + 'dpi': kwargs.get('dpi', InvenTreeSetting.get_setting('LABEL_DPI', 300)), + 'use_pdftocairo': kwargs.get('use_cairo', True), + **kwargs.get('pdf2image_kwargs', {}), + } # Convert to png data try: - return pdf2image.convert_from_bytes(pdf_data, dpi=dpi)[0] + return pdf2image.convert_from_bytes(pdf_data, **pdf2image_kwargs)[0] except Exception as e: log_error('label.render_to_png') raise ValidationError(_('Error rendering label to PNG'))