Added pdf2image kwargs (#6488)

* Added pdf2image kwargs

* Added direct use_cairo arg and make it default enabled

* Fix docs

* Fix docs

* Fix docs

* Fix docs
This commit is contained in:
Lukas 2024-02-15 10:53:54 +01:00 committed by GitHub
parent 5af6b92f90
commit 35577fad41
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 3 deletions

View File

@ -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)

View File

@ -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'))