weasyprint-thermal 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ # /home/jtafur/Desktop-jtafur/weasyprint-thermal/LICENSE
2
+ MIT License
3
+
4
+ Copyright (c) 2026 jhon.tafur
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
@@ -0,0 +1,89 @@
1
+ Metadata-Version: 2.4
2
+ Name: weasyprint-thermal
3
+ Version: 0.1.0
4
+ Summary: A Python library to generate dynamic-height PDF tickets for thermal printers using WeasyPrint
5
+ Author-email: "jhon.tafur" <jhon.tafur0590@gmail.com>
6
+ Classifier: Programming Language :: Python :: 3
7
+ Classifier: License :: OSI Approved :: MIT License
8
+ Classifier: Operating System :: OS Independent
9
+ Requires-Python: >=3.8
10
+ Description-Content-Type: text/markdown
11
+ License-File: LICENSE
12
+ Requires-Dist: weasyprint>=53.0
13
+ Dynamic: license-file
14
+
15
+ # WeasyPrint Thermal Tickets
16
+
17
+ `weasyprint-thermal` is a lightweight, zero-configuration Python library that solves the challenge of generating dynamic-height PDF tickets (e.g., 58mm or 80mm wide receipts for POS and thermal printers) using WeasyPrint.
18
+
19
+ ## The Challenge
20
+
21
+ WeasyPrint relies on CSS Paged Media standards which are designed for fixed page dimensions (like A4 or Letter). If you declare `@page { size: 58mm auto; }`, WeasyPrint ignores it because `auto` is not a valid height parameter, rendering an A4 size page instead.
22
+
23
+ `weasyprint-thermal` solves this by executing a smart **two-pass rendering** technique:
24
+ 1. **Pass 1:** Lays out the document at a fixed width (e.g., 58mm) and an extremely tall height to let the text flow and wrap naturally.
25
+ 2. **Analysis:** Recursively inspects WeasyPrint's internal Box Tree to calculate the exact vertical coordinate (`y + height`) of the lowest content block.
26
+ 3. **Pass 2:** Re-renders the document to a PDF file with the exact computed height + padding margins, ensuring a perfectly proportioned, single-page PDF.
27
+
28
+ ## Installation
29
+
30
+ You can install this package locally or in editable mode:
31
+
32
+ ```bash
33
+ pip install /home/jtafur/Desktop-jtafur/weasyprint-thermal
34
+ # or in editable mode
35
+ pip install -e /home/jtafur/Desktop-jtafur/weasyprint-thermal
36
+ ```
37
+
38
+ ## Quick Start
39
+
40
+ ```python
41
+ from weasyprint_thermal import render_thermal_ticket
42
+
43
+ html_content = """
44
+ <!DOCTYPE html>
45
+ <html>
46
+ <head>
47
+ <style>
48
+ body { font-family: monospace; font-size: 10pt; }
49
+ .center { text-align: center; }
50
+ .bold { font-weight: bold; }
51
+ .divider { border-top: 1px dashed black; margin: 5px 0; }
52
+ </style>
53
+ </head>
54
+ <body>
55
+ <div class="center">
56
+ <span class="bold">MY PARKING BUSINESS</span><br>
57
+ NIT: 123-456-789<br>
58
+ Calle 123 #45-67
59
+ </div>
60
+ <div class="divider"></div>
61
+ <div>Ticket No: 00054</div>
62
+ <div>Date: 25/05/2026 10:00 PM</div>
63
+ <div class="divider"></div>
64
+ <div class="center bold" style="font-size: 18pt;">ABC-123</div>
65
+ <div class="divider"></div>
66
+ <div class="center">Thank you for your visit!</div>
67
+ </body>
68
+ </html>
69
+ """
70
+
71
+ # Render the HTML content directly to a dynamically sized PDF in bytes
72
+ pdf_bytes = render_thermal_ticket(
73
+ html_content=html_content,
74
+ width_mm=58, # Target width in millimeters
75
+ margin_y_mm=2, # Top & bottom page margins
76
+ margin_x_mm=1, # Left & right page margins
77
+ extra_padding_pt=6.0 # Safe vertical spacing at the bottom
78
+ )
79
+
80
+ # Save the resulting PDF
81
+ with open("my_thermal_ticket.pdf", "wb") as f:
82
+ f.write(pdf_bytes)
83
+ ```
84
+
85
+ ## Features
86
+
87
+ - **Dynamic Height Auto-detection:** No more long empty blanks or cut-off text on thermal receipts.
88
+ - **Customizable Dimensions:** Supports any millimeter width (58mm, 80mm, etc.) and margins.
89
+ - **Framework Independent:** Works with raw HTML strings. Easily integrates with Django, Flask, FastAPI, or standalone scripts.
@@ -0,0 +1,75 @@
1
+ # WeasyPrint Thermal Tickets
2
+
3
+ `weasyprint-thermal` is a lightweight, zero-configuration Python library that solves the challenge of generating dynamic-height PDF tickets (e.g., 58mm or 80mm wide receipts for POS and thermal printers) using WeasyPrint.
4
+
5
+ ## The Challenge
6
+
7
+ WeasyPrint relies on CSS Paged Media standards which are designed for fixed page dimensions (like A4 or Letter). If you declare `@page { size: 58mm auto; }`, WeasyPrint ignores it because `auto` is not a valid height parameter, rendering an A4 size page instead.
8
+
9
+ `weasyprint-thermal` solves this by executing a smart **two-pass rendering** technique:
10
+ 1. **Pass 1:** Lays out the document at a fixed width (e.g., 58mm) and an extremely tall height to let the text flow and wrap naturally.
11
+ 2. **Analysis:** Recursively inspects WeasyPrint's internal Box Tree to calculate the exact vertical coordinate (`y + height`) of the lowest content block.
12
+ 3. **Pass 2:** Re-renders the document to a PDF file with the exact computed height + padding margins, ensuring a perfectly proportioned, single-page PDF.
13
+
14
+ ## Installation
15
+
16
+ You can install this package locally or in editable mode:
17
+
18
+ ```bash
19
+ pip install /home/jtafur/Desktop-jtafur/weasyprint-thermal
20
+ # or in editable mode
21
+ pip install -e /home/jtafur/Desktop-jtafur/weasyprint-thermal
22
+ ```
23
+
24
+ ## Quick Start
25
+
26
+ ```python
27
+ from weasyprint_thermal import render_thermal_ticket
28
+
29
+ html_content = """
30
+ <!DOCTYPE html>
31
+ <html>
32
+ <head>
33
+ <style>
34
+ body { font-family: monospace; font-size: 10pt; }
35
+ .center { text-align: center; }
36
+ .bold { font-weight: bold; }
37
+ .divider { border-top: 1px dashed black; margin: 5px 0; }
38
+ </style>
39
+ </head>
40
+ <body>
41
+ <div class="center">
42
+ <span class="bold">MY PARKING BUSINESS</span><br>
43
+ NIT: 123-456-789<br>
44
+ Calle 123 #45-67
45
+ </div>
46
+ <div class="divider"></div>
47
+ <div>Ticket No: 00054</div>
48
+ <div>Date: 25/05/2026 10:00 PM</div>
49
+ <div class="divider"></div>
50
+ <div class="center bold" style="font-size: 18pt;">ABC-123</div>
51
+ <div class="divider"></div>
52
+ <div class="center">Thank you for your visit!</div>
53
+ </body>
54
+ </html>
55
+ """
56
+
57
+ # Render the HTML content directly to a dynamically sized PDF in bytes
58
+ pdf_bytes = render_thermal_ticket(
59
+ html_content=html_content,
60
+ width_mm=58, # Target width in millimeters
61
+ margin_y_mm=2, # Top & bottom page margins
62
+ margin_x_mm=1, # Left & right page margins
63
+ extra_padding_pt=6.0 # Safe vertical spacing at the bottom
64
+ )
65
+
66
+ # Save the resulting PDF
67
+ with open("my_thermal_ticket.pdf", "wb") as f:
68
+ f.write(pdf_bytes)
69
+ ```
70
+
71
+ ## Features
72
+
73
+ - **Dynamic Height Auto-detection:** No more long empty blanks or cut-off text on thermal receipts.
74
+ - **Customizable Dimensions:** Supports any millimeter width (58mm, 80mm, etc.) and margins.
75
+ - **Framework Independent:** Works with raw HTML strings. Easily integrates with Django, Flask, FastAPI, or standalone scripts.
@@ -0,0 +1,26 @@
1
+ # /home/jtafur/Desktop-jtafur/weasyprint-thermal/pyproject.toml
2
+ [build-system]
3
+ requires = ["setuptools>=61.0.0", "wheel"]
4
+ build-backend = "setuptools.build_meta"
5
+
6
+ [project]
7
+ name = "weasyprint-thermal"
8
+ version = "0.1.0"
9
+ description = "A Python library to generate dynamic-height PDF tickets for thermal printers using WeasyPrint"
10
+ readme = "README.md"
11
+ authors = [
12
+ { name = "jhon.tafur", email = "jhon.tafur0590@gmail.com" }
13
+ ]
14
+ classifiers = [
15
+ "Programming Language :: Python :: 3",
16
+ "License :: OSI Approved :: MIT License",
17
+ "Operating System :: OS Independent",
18
+ ]
19
+ requires-python = ">=3.8"
20
+ dependencies = [
21
+ "weasyprint>=53.0"
22
+ ]
23
+
24
+ [tool.setuptools.packages.find]
25
+ where = ["."]
26
+ include = ["weasyprint_thermal*"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,3 @@
1
+ from .renderer import render_thermal_ticket
2
+
3
+ __all__ = ['render_thermal_ticket']
@@ -0,0 +1,62 @@
1
+ from weasyprint import HTML, CSS
2
+
3
+ def _get_max_y(box):
4
+ """Recursively calculates the maximum Y coordinate of any content box."""
5
+ max_y = 0
6
+ # PageBox itself represents the page container, we exclude it to avoid getting the giant initial height
7
+ if box.__class__.__name__ != 'PageBox':
8
+ y = getattr(box, 'position_y', None)
9
+ h = getattr(box, 'height', None)
10
+ if y is not None and h is not None:
11
+ max_y = y + h
12
+
13
+ if hasattr(box, 'children'):
14
+ for child in box.children:
15
+ max_y = max(max_y, _get_max_y(child))
16
+
17
+ return max_y
18
+
19
+ def render_thermal_ticket(
20
+ html_content: str,
21
+ width_mm: float = 58.0,
22
+ margin_y_mm: float = 2.0,
23
+ margin_x_mm: float = 1.0,
24
+ extra_padding_pt: float = 6.0
25
+ ) -> bytes:
26
+ """
27
+ Renders an HTML content string into a dynamic-height thermal printer friendly PDF.
28
+
29
+ Args:
30
+ html_content (str): The HTML to render.
31
+ width_mm (float): Width in millimeters (typically 58 or 80).
32
+ margin_y_mm (float): Top & Bottom margins in millimeters.
33
+ margin_x_mm (float): Left & Right margins in millimeters.
34
+ extra_padding_pt (float): Safe padding in points (pt) to append at the end of the ticket content.
35
+
36
+ Returns:
37
+ bytes: The generated PDF file as a byte string.
38
+ """
39
+ # 1. First Pass: render with the specified width and a giant height to wrap lines naturally
40
+ giant_height_px = 5000.0
41
+ first_pass_css = CSS(string=f"@page {{ size: {width_mm}mm {giant_height_px}px; margin: {margin_y_mm}mm {margin_x_mm}mm; }}")
42
+ doc = HTML(string=html_content).render(stylesheets=[first_pass_css])
43
+
44
+ # 2. Analyze: compute content height from the rendered Box Tree (in CSS px)
45
+ page = doc.pages[0]
46
+ content_height_px = _get_max_y(page._page_box)
47
+
48
+ # 3. Convert WeasyPrint px to physical PDF points (1px = 0.75pt)
49
+ # WeasyPrint internally lays out at 96 dpi (px) but PDF points are 72 dpi (pt).
50
+ content_height_pt = content_height_px * 0.75
51
+
52
+ # 4. Calculate absolute height including margin & safe padding (in pt)
53
+ # A default extra_padding_pt of 15.0pt (approx 5mm) is perfect to let the ticket breathe
54
+ total_height_pt = content_height_pt + extra_padding_pt
55
+
56
+ # 5. Second Pass: render with the exact computed dimensions in physical points (pt)
57
+ final_css = CSS(string=f"@page {{ size: {width_mm}mm {total_height_pt}pt; margin: {margin_y_mm}mm {margin_x_mm}mm; }}")
58
+ final_doc = HTML(string=html_content).render(stylesheets=[final_css])
59
+
60
+ print(f"Generated PDF Page Size: {final_doc.pages[0].width} x {final_doc.pages[0].height} px (Height: {total_height_pt} pt)")
61
+
62
+ return final_doc.write_pdf()
@@ -0,0 +1,89 @@
1
+ Metadata-Version: 2.4
2
+ Name: weasyprint-thermal
3
+ Version: 0.1.0
4
+ Summary: A Python library to generate dynamic-height PDF tickets for thermal printers using WeasyPrint
5
+ Author-email: "jhon.tafur" <jhon.tafur0590@gmail.com>
6
+ Classifier: Programming Language :: Python :: 3
7
+ Classifier: License :: OSI Approved :: MIT License
8
+ Classifier: Operating System :: OS Independent
9
+ Requires-Python: >=3.8
10
+ Description-Content-Type: text/markdown
11
+ License-File: LICENSE
12
+ Requires-Dist: weasyprint>=53.0
13
+ Dynamic: license-file
14
+
15
+ # WeasyPrint Thermal Tickets
16
+
17
+ `weasyprint-thermal` is a lightweight, zero-configuration Python library that solves the challenge of generating dynamic-height PDF tickets (e.g., 58mm or 80mm wide receipts for POS and thermal printers) using WeasyPrint.
18
+
19
+ ## The Challenge
20
+
21
+ WeasyPrint relies on CSS Paged Media standards which are designed for fixed page dimensions (like A4 or Letter). If you declare `@page { size: 58mm auto; }`, WeasyPrint ignores it because `auto` is not a valid height parameter, rendering an A4 size page instead.
22
+
23
+ `weasyprint-thermal` solves this by executing a smart **two-pass rendering** technique:
24
+ 1. **Pass 1:** Lays out the document at a fixed width (e.g., 58mm) and an extremely tall height to let the text flow and wrap naturally.
25
+ 2. **Analysis:** Recursively inspects WeasyPrint's internal Box Tree to calculate the exact vertical coordinate (`y + height`) of the lowest content block.
26
+ 3. **Pass 2:** Re-renders the document to a PDF file with the exact computed height + padding margins, ensuring a perfectly proportioned, single-page PDF.
27
+
28
+ ## Installation
29
+
30
+ You can install this package locally or in editable mode:
31
+
32
+ ```bash
33
+ pip install /home/jtafur/Desktop-jtafur/weasyprint-thermal
34
+ # or in editable mode
35
+ pip install -e /home/jtafur/Desktop-jtafur/weasyprint-thermal
36
+ ```
37
+
38
+ ## Quick Start
39
+
40
+ ```python
41
+ from weasyprint_thermal import render_thermal_ticket
42
+
43
+ html_content = """
44
+ <!DOCTYPE html>
45
+ <html>
46
+ <head>
47
+ <style>
48
+ body { font-family: monospace; font-size: 10pt; }
49
+ .center { text-align: center; }
50
+ .bold { font-weight: bold; }
51
+ .divider { border-top: 1px dashed black; margin: 5px 0; }
52
+ </style>
53
+ </head>
54
+ <body>
55
+ <div class="center">
56
+ <span class="bold">MY PARKING BUSINESS</span><br>
57
+ NIT: 123-456-789<br>
58
+ Calle 123 #45-67
59
+ </div>
60
+ <div class="divider"></div>
61
+ <div>Ticket No: 00054</div>
62
+ <div>Date: 25/05/2026 10:00 PM</div>
63
+ <div class="divider"></div>
64
+ <div class="center bold" style="font-size: 18pt;">ABC-123</div>
65
+ <div class="divider"></div>
66
+ <div class="center">Thank you for your visit!</div>
67
+ </body>
68
+ </html>
69
+ """
70
+
71
+ # Render the HTML content directly to a dynamically sized PDF in bytes
72
+ pdf_bytes = render_thermal_ticket(
73
+ html_content=html_content,
74
+ width_mm=58, # Target width in millimeters
75
+ margin_y_mm=2, # Top & bottom page margins
76
+ margin_x_mm=1, # Left & right page margins
77
+ extra_padding_pt=6.0 # Safe vertical spacing at the bottom
78
+ )
79
+
80
+ # Save the resulting PDF
81
+ with open("my_thermal_ticket.pdf", "wb") as f:
82
+ f.write(pdf_bytes)
83
+ ```
84
+
85
+ ## Features
86
+
87
+ - **Dynamic Height Auto-detection:** No more long empty blanks or cut-off text on thermal receipts.
88
+ - **Customizable Dimensions:** Supports any millimeter width (58mm, 80mm, etc.) and margins.
89
+ - **Framework Independent:** Works with raw HTML strings. Easily integrates with Django, Flask, FastAPI, or standalone scripts.
@@ -0,0 +1,10 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ weasyprint_thermal/__init__.py
5
+ weasyprint_thermal/renderer.py
6
+ weasyprint_thermal.egg-info/PKG-INFO
7
+ weasyprint_thermal.egg-info/SOURCES.txt
8
+ weasyprint_thermal.egg-info/dependency_links.txt
9
+ weasyprint_thermal.egg-info/requires.txt
10
+ weasyprint_thermal.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ weasyprint>=53.0
@@ -0,0 +1 @@
1
+ weasyprint_thermal