forked from reingart/pyfpdf
-
Notifications
You must be signed in to change notification settings - Fork 227
/
test_opacity.py
36 lines (27 loc) · 1.08 KB
/
test_opacity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from pathlib import Path
from fpdf import FPDF
from test.conftest import assert_pdf_equal
HERE = Path(__file__).resolve().parent
def test_transparency(tmp_path):
"Inspired by https://github.com/digidigital/Extensions-and-Scripts-for-pyFPDF-fpdf2/tree/main/alpha"
pdf = FPDF()
pdf.set_font("Helvetica", "B", 24)
pdf.set_line_width(1.5)
pdf.add_page()
# Draw an opaque red square:
pdf.set_fill_color(255, 0, 0)
pdf.rect(10, 10, 40, 40, "DF")
# Set alpha to semi-transparency for shape lines & filled areas:
with pdf.local_context(fill_opacity=0.5, stroke_opacity=0.5):
# Draw a green square:
pdf.set_fill_color(0, 255, 0)
pdf.rect(20, 20, 40, 40, "DF")
# Set transparency for images & text:
with pdf.local_context(fill_opacity=0.25):
# Insert an image:
pdf.image(HERE / "../docs/fpdf2-logo.png", 30, 30, 40)
# Print some text:
pdf.text(22, 29, "You are...")
# Print some text with full opacity:
pdf.text(30, 45, "Over the top")
assert_pdf_equal(pdf, HERE / "transparency.pdf", tmp_path)