mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-29 20:16:44 +00:00
* Adds a new "generic" ReportTemplate model * expose API endpoints * Update model / migrations / serializer * Add new mixin class to existing database models * - Add detail view for report template - Revert filters field behaviour * Filter report list by provided item IDs - Greatly simplify filtering logic compared to existing implemetation - Expose to API schema * Create data migration for converting *old* report templates * Ignore internal reports for data migration * Add report mixin to StockLocation model * Provide model choices in admin interface * Offload context data generation to the model classes * Remove old report template models * Refactor JS code in CUI * Fix for API filtering * Add data migration to delete old models * Remove dead URL * Updates * Construct sample report templates on app start * Bump API version * Typo fix * Fix incorrect context calls * Add new LabelTemplate model - ReportTemplate and LabelTemplate share common base - Refactor previous migration * Expose to admin interface * Add in extra context from existing label models * Add migration to create LabelTemplate instances from existing labels * Add API endpoints for listing and updating LabelTemplate objects * Adjust 'upload_to' path * Refactor label printing * Move default label templates * Update API endpoints * Update migrations * Handle LookupError in migration * Redirect the "label" API endpoint * Add new model for handling result of template printing * Refactor LabelPrinting mixin * Unlink "labels" app entirely * Fix typo * Record 'plugin' used to generate a particular output * Fix imports * Generate label print response - Still not good yet * Refactoring label printing in CUI * add "items" count to TemplateOutput model * Fix for InvenTreeLabelSheetPlugin * Remove old "label" app * Make request object optional * Fix filename generation * Add help text for "model_type" * Simplify TemplateTable * Tweak TemplateTable * Get template editor to display template data again * Stringify template name - Important, otherwise you get a TypeError instead of TemplateDoesNotExist * Add hooks to reset plugin state * fix context for StockLocation model * Tweak log messages * Fix incorrect serializer * Cleanup TemplateTable * Fix broken import * Filter by target model type * Remove manual file operations * Update old migrations - Remove references to functions that no longer exist * Refactor asset / snippet uploading * Update comments * Retain original filename when editing templatese * Cleanup * Refactor model type filter to use new hook * Add placeholder actions for printing labels and reports * Improve hookiness * Add new ReportOutput class * Report printing works from PUI now! * More inspired filename pattern for generated reports * Fix template preview window - Use new "output" response field across the board * Remove outdated task * Update data migration to use raw SQL - If the 'labels' app is no longer available, this will fail - So, use raw SQL instead * Add more API endpoint defs * Adds placeholder API endpoint for label printing * Expose plugin field to the printing endpoint * Adds plugin model type * Hook to print labels * Refactor action dropdown items * Refactor report printing for CUI * Refactor label print for CUI - Still needs to handle custom printing options for plugin * Fix migration * Update ModelType dict * playwright test fix * Unit test fixes * Fix model ruleset associations * Fix for report.js * Add support for "dynamic" fields in metadata.py * Add in custom fields based on plugin * Refactoring * Reset plugin on form close * Set custom timeout values * Update migration - Not atomic * Cleanup * Implement more printing actions * Reduce timeout * Unit test updates * Fix part serializers * Label printing works in CUI again * js linting * Update <ActionDropdown> * Fix for label printing API endpoint * Fix filterselectdrawer * Improve button rendering * Allow printing from StockLocationTable * Add aria-labels to modal form fields * Add test for printing stock item labels from table * Add test for report printing * Add unit testing for report template editing / preview * Message refactor * Refactor InvenTreeReportMixin class * Update playwright test * Update 'verbose_name' for a number of models * Additional admin filtering * Playwright test updates * Run checks against new python lib branch (temporary, will be reverted) * remove old app reference * fix testing ref * fix app init * remove old tests * Revert custom target branch * Expose label and report output objects to API * refactor * fix a few tests * factor plugin_ref out * fix options testing * Update table field header * re-enable full options testing * fix missing plugin matching * disable call assert * Add custom related field for PluginConfig - Uses 'key' rather than 'pk' - Revert label print plugin to use slug * Add support for custom pk field in metadata * switch to labels for testing * re-align report testing code * disable version check * fix url * Implement lazy loading * Allow blank plugin for printing - Uses the builtin label printer if not specified * Add printing actions for StockItem * Fix for metadata helper * Use key instead of pk in printing actions * Support non-standard pk values in RelatedModelField * pass context data to report serializers * disable template / item discovery * fix call * Tweak unit test * Run python checks against specific branch * Add task for running docs server - Option to compile schema as part of task * Custom branch no longer needed * Starting on documentation updates * fix tests for reports * fix label testing * Update template context variables * Refactor report context documentation * Documentation cleanup * Docs cleanup * Include sample report files * Fix links * Link cleanup * Integrate plugin example code into docs * Code cleanup * Fix type annotation * Revert deleted variable * remove templatetype * remove unused imports * extend context testing * test if plg can print * re-enable version check * Update unit tests * Fix test * Adjust unit test * Add debug statement to test * Fix unit test - Labels get printed against LabelTemplate items, duh * Unit test update * Unit test updates * Test update * Patch fix for <PartColumn> component * Fix ReportSerialierBase class - Re-initialize field options if not already set * Fix unit test for sqlite * Fix kwargs for non-blocking label printing * Update playwright tests * Tweak unit test --------- Co-authored-by: Matthias Mair <code@mjmair.com>
192 lines
5.7 KiB
Markdown
192 lines
5.7 KiB
Markdown
---
|
|
title: Custom Labels
|
|
---
|
|
|
|
## Custom Labels
|
|
|
|
InvenTree supports printing of custom template-based labels, using the [WeasyPrint](https://weasyprint.org/) PDF generation engine.
|
|
|
|
Custom labels can be generated using simple HTML templates, with support for QR-codes, and conditional formatting using the Django template engine.
|
|
|
|
|
|
Simple (generic) label templates are supplied 'out of the box' with InvenTree - however support is provided for generation of extremely specific custom labels, to meet any particular requirement.
|
|
|
|
## Label Templates
|
|
|
|
Label templates are written using a mixture of [HTML](https://www.w3schools.com/html/) and [CSS](https://www.w3schools.com/css). [Weasyprint](https://weasyprint.org/) templates support a *subset* of HTML and CSS features. In addition to supporting HTML and CSS formatting, the label templates support the Django templating engine, allowing conditional formatting of the label data.
|
|
|
|
A label template is a single `.html` file which is uploaded to the InvenTree server by the user.
|
|
|
|
Below is a reasonably simple example of a label template which demonstrates much of the available functionality. The template code shown below will produce the following label:
|
|
|
|
{% with id="label_example", url="report/label_example.png", description="Example label" %}
|
|
{% include 'img.html' %}
|
|
{% endwith %}
|
|
|
|
```html
|
|
{% raw %}
|
|
<style>
|
|
@page {
|
|
width: 75mm;
|
|
height: 24mm;
|
|
padding: 1mm;
|
|
margin: 0px 0px 0px 0px;
|
|
}
|
|
|
|
.location {
|
|
padding: 5px;
|
|
font-weight: bold;
|
|
font-family: Arial, Helvetica, sans-serif;
|
|
height: 100%;
|
|
vertical-align: middle;
|
|
float: right;
|
|
display: inline;
|
|
font-size: 125%;
|
|
position: absolute;
|
|
top: 0mm;
|
|
left: 23mm;
|
|
white-space: nowrap;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
}
|
|
|
|
.qr {
|
|
margin: 2px;
|
|
width: 22mm;
|
|
height: 22mm;
|
|
}
|
|
|
|
</style>
|
|
{% load barcode %}
|
|
<img class='qr' src="{% qrcode location.format_barcode %}"/>
|
|
|
|
<div class='location'>
|
|
{{ location.name }}
|
|
<br>
|
|
<br>
|
|
<hr>
|
|
Location ID: {{ location.id }}
|
|
</div>
|
|
</div>
|
|
{% endraw %}
|
|
```
|
|
|
|
Each significant component of the example template above are described in the sections below:
|
|
|
|
### Stylesheet
|
|
|
|
The stylesheet defines the *style* of the label, and is enclosed within the `<style>...</style>` tags. The stylesheet supports any of the CSS features which are supported natively by weasyprint.
|
|
|
|
Some points of note:
|
|
|
|
- The `@page` directive specifies the size of the label, and global margins.
|
|
- Normal per-class and per-element styling is supported
|
|
- Absolute positioning of elements is supported via the `position` CSS directive
|
|
- If text is too long to fit on a given label, the `text-overflow` directive can be used
|
|
|
|
### Context Data
|
|
|
|
Each label template is supplied with *context data* (variables) which can be used to display information based on the context in which the label is printed. Variables supplied as context objects can be easily rendered to the label using Django templating syntax.
|
|
|
|
For example, if a StockLocation object is supplied to the label as the variable `location`, it is trivially simple to render the data to the label:
|
|
|
|
```html
|
|
{% raw %}
|
|
Location ID: {{ location.id }}
|
|
<br>
|
|
Location Name: {{ location.name }}
|
|
{% endraw %}
|
|
```
|
|
|
|
Refer to the [context variables documentation](./context_variables.md).
|
|
|
|
### Barcodes
|
|
|
|
Refer to the [barcode documentation](./barcodes.md).
|
|
|
|
### Using media files
|
|
|
|
Refer to the [media files documentation](./helpers.md#media-files).
|
|
|
|
### Conditional Formatting
|
|
|
|
Conditional formatting of label data is also supported. Below is an example excerpt from a label which determines the content based on the supplied context variables:
|
|
|
|
```html
|
|
{% raw %}
|
|
{% if item.in_stock %}
|
|
Quantity: {{ item.quantity }}
|
|
{% else %}
|
|
OUT OF STOCK
|
|
{% endif %}
|
|
{% endraw %}
|
|
```
|
|
|
|
### Label Filters
|
|
|
|
Each label template provides a set of programmable filters which can be used to determine the relevance of that particular label. It may be the case that a particular label template is only applicable if certain conditions are met.
|
|
|
|
As an example, consider a label template for a StockItem. A user may wish to define a label which displays the firmware version of any items related to the Part with the IPN (Internal Part Number) `IPN123`.
|
|
|
|
To restrict the label accordingly, we could set the *filters* value to `part__IPN=IPN123`.
|
|
|
|
## Built-In Templates
|
|
|
|
The InvenTree installation provides a number of simple *default* templates which can be used as a starting point for creating custom labels. These built-in templates can be disabled if they are not required.
|
|
|
|
Built-in templates can also be used to quickly scaffold custom labels, using template inheritance.
|
|
|
|
### Base Template
|
|
|
|
For example, InvenTree provides a *base* template from which all of the default label templates are derived. This *base* template provides the essentials for generating a label:
|
|
|
|
```html
|
|
{% raw %}
|
|
<head>
|
|
<style>
|
|
@page {
|
|
size: {{ width }}mm {{ height }}mm;
|
|
{% block margin %}
|
|
margin: 0mm;
|
|
{% endblock %}
|
|
}
|
|
|
|
img {
|
|
display: inline-block;
|
|
image-rendering: pixelated;
|
|
}
|
|
|
|
{% block style %}
|
|
{% endblock %}
|
|
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
{% block content %}
|
|
<!-- Label data rendered here! -->
|
|
{% endblock %}
|
|
</body>
|
|
|
|
{% endraw %}
|
|
```
|
|
|
|
### Extend Base Template
|
|
|
|
To extend this template in a custom uploaded label, simply extend as follows:
|
|
|
|
```html
|
|
{% raw %}
|
|
{% extends "label/label_base.html" %}
|
|
|
|
{% block style %}
|
|
<!-- You can write custom CSS here -->
|
|
{% endblock %}
|
|
|
|
{% block content %}
|
|
<!-- HTML content goes here! -->
|
|
{% endblock %}
|
|
|
|
{% endraw %}
|
|
```
|