Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ exclude: |
)$
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
rev: v6.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
exclude: mkdocs.yml
- id: mixed-line-ending
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.11.13
rev: v0.14.0
hooks:
- id: ruff-format
args: [--preview]
Expand All @@ -29,7 +29,7 @@ repos:
--preview
]
- repo: https://github.com/astral-sh/uv-pre-commit
rev: 0.7.12
rev: 0.9.3
hooks:
- id: pip-compile
name: pip-compile requirements-dev.in
Expand Down Expand Up @@ -71,16 +71,16 @@ repos:
src/frontend/vite.config.ts |
)$
- repo: https://github.com/biomejs/pre-commit
rev: v2.0.0-beta.5
rev: v2.2.6
hooks:
- id: biome-check
additional_dependencies: ["@biomejs/[email protected]"]
files: ^src/frontend/.*\.(js|ts|tsx)$
- repo: https://github.com/gitleaks/gitleaks
rev: v8.27.2
rev: v8.28.0
hooks:
- id: gitleaks
language_version: 1.23.6
language_version: 1.23.8
#- repo: https://github.com/jumanjihouse/pre-commit-hooks
# rev: 3.0.0
# hooks:
Expand Down
8 changes: 7 additions & 1 deletion src/backend/InvenTree/report/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,13 @@ def print(self, template, items_to_print, request):
item_ids = [item.pk for item in items_to_print]

# Offload the task to the background worker
offload_task(report.tasks.print_reports, template.pk, item_ids, output.pk)
offload_task(
report.tasks.print_reports,
template.pk,
item_ids,
output.pk,
request=request,
)

output.refresh_from_db()

Expand Down
27 changes: 25 additions & 2 deletions src/backend/InvenTree/report/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django.core.files.storage import default_storage
from django.core.validators import FileExtensionValidator, MinValueValidator
from django.db import models
from django.http.request import HttpRequest
from django.template import Context, Template
from django.template.exceptions import TemplateDoesNotExist, TemplateSyntaxError
from django.template.loader import render_to_string
Expand All @@ -35,7 +36,7 @@
from plugin.registry import registry

try:
from weasyprint import HTML
from weasyprint import HTML, default_url_fetcher
except OSError as err: # pragma: no cover
print(f'OSError: {err}')
print("Unable to import 'weasyprint' module.")
Expand All @@ -46,6 +47,24 @@
logger = structlog.getLogger('inventree')


def build_fetcher(request: HttpRequest):
"""Factory function that returns a function that handles Weasyprint url requests.

Takes in an HttpRequest and copies it's headers for future requests.
"""
http_headers = None

if request is not None:
http_headers = request.headers

def url_fetcher(url: str, timeout=10, ssl_context=None):
return default_url_fetcher(
url, timeout=timeout, ssl_context=ssl_context, http_headers=http_headers
)

return url_fetcher


def log_report_error(*args, **kwargs):
"""Log an error message when a report fails to render."""
try:
Expand Down Expand Up @@ -272,7 +291,11 @@ def render(self, instance, request=None, context=None, **kwargs) -> bytes:
bytes: PDF data
"""
html = self.render_as_string(instance, request, context, **kwargs)
pdf = HTML(string=html).write_pdf()
pdf = HTML(
string=html,
url_fetcher=build_fetcher(request),
base_url=get_base_url(request),
).write_pdf()

return pdf

Expand Down
2 changes: 1 addition & 1 deletion src/backend/InvenTree/report/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def print_reports(template_id: int, item_ids: list[int], output_id: int, **kwarg
# Ensure they are sorted by the order of the provided item IDs
items = sorted(items, key=lambda item: item_ids.index(item.pk))

template.print(items, output=output)
template.print(items, output=output, request=kwargs.get('request'))


@tracer.start_as_current_span('print_labels')
Expand Down