Skip to content

Commit d5b4a95

Browse files
authored
Fixed a bug when fpdf.Template was used to render QRCodes, due to a forced conversion to string - close #175 (#177)
1 parent 7ad517b commit d5b4a95

7 files changed

+45
-9
lines changed

CHANGELOG.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ and [PEP 440](https://www.python.org/dev/peps/pep-0440/).
99

1010
## [2.4.2] - not released yet
1111
### Added
12-
- disable font caching when `fpdf.FPDF` constructor invoked with `font_cache_dir=None`
13-
- `FPDF.circle`: new method added.
12+
- disable font caching when `fpdf.FPDF` constructor invoked with `font_cache_dir=None`, thanks to @moe-25 !
13+
- `FPDF.circle`: new method added, thanks to @viraj-shah18 !
14+
- `HTMLMixin` / `HTML2FPDF`: support setting HTML font colors by name and short hex codes
1415
- [`FPDF.will_page_break`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.will_page_break)
1516
utility method to let users know in advance when adding an elemnt will trigger a page break.
1617
This can be useful to repeat table headers on each page for exemple,
1718
_cf._ [documentation on Tables](https://pyfpdf.github.io/fpdf2/Tables.html#repeat-table-header-on-each-page).
1819
- `FPDF.set_link` now support a new optional `x` parameter to set the horizontal position after following the link
1920

21+
### Fixed
22+
- fixed a bug when `fpdf.Template` was used to render QRCodes, due to a forced conversion to string (#175)
23+
2024
## [2.4.1] - 2021-06-12
2125
### Fixed
2226
- erroneous page breaks occured for full-width / full-height images
@@ -31,7 +35,6 @@ _cf._ [documentation on Tables](https://pyfpdf.github.io/fpdf2/Tables.html#repea
3135
- the `h` (height) parameter of the `cell`, `multi_cell` & `write` methods gets a default value change, `None`, meaning to use the current font size
3236
- removed the useless `w` & `h` parameters of the `FPDF.text_annotation()` method
3337
### Added
34-
- Support setting HTML font colors by name and short hex codes
3538
- new `FPDF.add_action()` method, documented in the [Annotations section](https://pyfpdf.github.io/fpdf2/Annotations.html)
3639
- `FPDF.cell`: new optional `markdown=True` parameter that enables basic Markdown-like styling: `**bold**, __italics__, --underlined--`
3740
- `FPDF.cell`: new optional boolean `center` parameter that positions the cell horizontally

fpdf/html.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def px2mm(px):
169169
return int(px) * 25.4 / 72
170170

171171

172-
def hex2dec(color="#000000"):
172+
def color_as_decimal(color="#000000"):
173173
if not color:
174174
return None
175175

@@ -294,7 +294,7 @@ def _insert_td(self, data=""):
294294
else:
295295
align = self.td.get("align", "L")[0].upper()
296296
border = border and "LR"
297-
bgcolor = hex2dec(self.td.get("bgcolor", self.tr.get("bgcolor", "")))
297+
bgcolor = color_as_decimal(self.td.get("bgcolor", self.tr.get("bgcolor", "")))
298298
# parsing table header/footer (drawn later):
299299
if self.thead is not None:
300300
self.theader.append(((width, height, data, border, 0, align), bgcolor))
@@ -442,7 +442,7 @@ def handle_starttag(self, tag, attrs):
442442
# save previous font state:
443443
self.font_stack.append((self.font_face, self.font_size, self.font_color))
444444
if "color" in attrs:
445-
color = hex2dec(attrs["color"])
445+
color = color_as_decimal(attrs["color"])
446446
self.font_color = color
447447
if "face" in attrs:
448448
face = attrs.get("face").lower()

fpdf/template.py

-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ def __setitem__(self, name, value):
106106
raise FPDFException(f"Element not loaded, cannot set item: {name}")
107107
if not self.pg_no:
108108
raise FPDFException("No page open, you need to call add_page() first")
109-
value = "" if value is None else str(value)
110109
self.texts[self.pg_no][name.lower()] = value
111110

112111
# setitem shortcut (may be further extended)

test/image/test_full_page_image.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
IMAGE_PATH = HERE / "png_images/ba2b2b6e72ca0e4683bb640e2d5572f8.png"
99

1010

11-
def test_full_width_image(tmp_path):
11+
def test_full_width_image(tmp_path): # issue-166
1212
img = fpdf.image_parsing.get_img_info(IMAGE_PATH)
1313
pdf = fpdf.FPDF(format=(img["w"], img["h"]))
1414
pdf.set_margin(0)
@@ -17,7 +17,7 @@ def test_full_width_image(tmp_path):
1717
assert_pdf_equal(pdf, HERE / "full_width_image.pdf", tmp_path)
1818

1919

20-
def test_full_height_image(tmp_path):
20+
def test_full_height_image(tmp_path): # issue-166
2121
img = fpdf.image_parsing.get_img_info(IMAGE_PATH)
2222
pdf = fpdf.FPDF(format=(img["w"], img["h"]))
2323
pdf.set_margin(0)

test/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ pylint
33
pytest
44
pytest-cov
55
pytest-timeout
6+
qrcode

test/template/template_qrcode.pdf

9.29 KB
Binary file not shown.

test/template/test_template.py

+33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from pathlib import Path
22

3+
import qrcode
4+
35
from fpdf.template import Template
6+
47
from ..conftest import assert_pdf_equal
58

69
HERE = Path(__file__).resolve().parent
@@ -149,3 +152,33 @@ def test_template_code39(tmp_path): # issue-161
149152
tmpl = Template(format="A4", title="Sample Code 39 barcode", elements=elements)
150153
tmpl.add_page()
151154
assert_pdf_equal(tmpl, HERE / "template_code39.pdf", tmp_path)
155+
156+
157+
def test_template_qrcode(tmp_path): # issue-175
158+
elements = [
159+
{
160+
"name": "barcode_0",
161+
"type": "I",
162+
"x1": 50,
163+
"y1": 50,
164+
"x2": 100,
165+
"y2": 100,
166+
"priority": 0,
167+
"text": None,
168+
},
169+
{
170+
"name": "barcode_1",
171+
"type": "I",
172+
"x1": 150,
173+
"y1": 150,
174+
"x2": 200,
175+
"y2": 200,
176+
"priority": 0,
177+
"text": None,
178+
},
179+
]
180+
tmpl = Template(format="letter", elements=elements)
181+
tmpl.add_page()
182+
tmpl["barcode_0"] = qrcode.make("Test 0").get_image()
183+
tmpl["barcode_1"] = qrcode.make("Test 1").get_image()
184+
assert_pdf_equal(tmpl, HERE / "template_qrcode.pdf", tmp_path)

0 commit comments

Comments
 (0)