Skip to content

Commit

Permalink
Fixed a bug when fpdf.Template was used to render QRCodes, due to a…
Browse files Browse the repository at this point in the history
… forced conversion to string - close #175
  • Loading branch information
Lucas-C committed Jun 24, 2021
1 parent 88752bd commit 1c8c470
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 7 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/),
and [PEP 440](https://www.python.org/dev/peps/pep-0440/).

## [2.4.2] - not released yet
- `HTMLMixin` / `HTML2FPDF`: support setting HTML font colors by name and short hex codes
- fixed a bug when `fpdf.Template` was used to render QRCodes, due to a forced conversion to string (#175)

## [2.4.1] - 2021-06-12
### Fixed
- erroneous page breaks occured for full-width / full-height images
Expand All @@ -21,7 +25,6 @@ and [PEP 440](https://www.python.org/dev/peps/pep-0440/).
- the `h` (height) parameter of the `cell`, `multi_cell` & `write` methods gets a default value change, `None`, meaning to use the current font size
- removed the useless `w` & `h` parameters of the `FPDF.text_annotation()` method
### Added
- Support setting HTML font colors by name and short hex codes
- new `FPDF.add_action()` method, documented in the [Annotations section](https://pyfpdf.github.io/fpdf2/Annotations.html)
- `FPDF.cell`: new optional `markdown=True` parameter that enables basic Markdown-like styling: `**bold**, __italics__, --underlined--`
- `FPDF.cell`: new optional boolean `center` parameter that positions the cell horizontally
Expand Down
6 changes: 3 additions & 3 deletions fpdf/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def px2mm(px):
return int(px) * 25.4 / 72


def hex2dec(color="#000000"):
def color_as_decimal(color="#000000"):
if not color:
return None

Expand Down Expand Up @@ -294,7 +294,7 @@ def _insert_td(self, data=""):
else:
align = self.td.get("align", "L")[0].upper()
border = border and "LR"
bgcolor = hex2dec(self.td.get("bgcolor", self.tr.get("bgcolor", "")))
bgcolor = color_as_decimal(self.td.get("bgcolor", self.tr.get("bgcolor", "")))
# parsing table header/footer (drawn later):
if self.thead is not None:
self.theader.append(((width, height, data, border, 0, align), bgcolor))
Expand Down Expand Up @@ -442,7 +442,7 @@ def handle_starttag(self, tag, attrs):
# save previous font state:
self.font_stack.append((self.font_face, self.font_size, self.font_color))
if "color" in attrs:
color = hex2dec(attrs["color"])
color = color_as_decimal(attrs["color"])
self.font_color = color
if "face" in attrs:
face = attrs.get("face").lower()
Expand Down
1 change: 0 additions & 1 deletion fpdf/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ def __setitem__(self, name, value):
raise FPDFException(f"Element not loaded, cannot set item: {name}")
if not self.pg_no:
raise FPDFException("No page open, you need to call add_page() first")
value = "" if value is None else str(value)
self.texts[self.pg_no][name.lower()] = value

# setitem shortcut (may be further extended)
Expand Down
4 changes: 2 additions & 2 deletions test/image/test_full_page_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
IMAGE_PATH = HERE / "png_images/ba2b2b6e72ca0e4683bb640e2d5572f8.png"


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


def test_full_height_image(tmp_path):
def test_full_height_image(tmp_path): # issue-166
img = fpdf.image_parsing.get_img_info(IMAGE_PATH)
pdf = fpdf.FPDF(format=(img["w"], img["h"]))
pdf.set_margin(0)
Expand Down
1 change: 1 addition & 0 deletions test/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pylint
pytest
pytest-cov
pytest-timeout
qrcode
Binary file added test/template/template_qrcode.pdf
Binary file not shown.
33 changes: 33 additions & 0 deletions test/template/test_template.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from pathlib import Path

import qrcode

from fpdf.template import Template

from ..conftest import assert_pdf_equal

HERE = Path(__file__).resolve().parent
Expand Down Expand Up @@ -149,3 +152,33 @@ def test_template_code39(tmp_path): # issue-161
tmpl = Template(format="A4", title="Sample Code 39 barcode", elements=elements)
tmpl.add_page()
assert_pdf_equal(tmpl, HERE / "template_code39.pdf", tmp_path)


def test_template_qrcode(tmp_path): # issue-175
elements = [
{
"name": "barcode_0",
"type": "I",
"x1": 50,
"y1": 50,
"x2": 100,
"y2": 100,
"priority": 0,
"text": None,
},
{
"name": "barcode_1",
"type": "I",
"x1": 150,
"y1": 150,
"x2": 200,
"y2": 200,
"priority": 0,
"text": None,
},
]
tmpl = Template(format="letter", elements=elements)
tmpl.add_page()
tmpl["barcode_0"] = qrcode.make("Test 0").get_image()
tmpl["barcode_1"] = qrcode.make("Test 1").get_image()
assert_pdf_equal(tmpl, HERE / "template_qrcode.pdf", tmp_path)

0 comments on commit 1c8c470

Please sign in to comment.