Skip to content

Commit 137fedd

Browse files
authored
Watermark test. (#96)
1 parent 8eac9d2 commit 137fedd

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

tests/test/test_hooks.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""Hook tests.
2+
3+
https://pymupdf.readthedocs.io/en/latest/tutorial.html
4+
5+
make test T=test_hooks.py
6+
"""
7+
import fitz
8+
from . import TestBase
9+
10+
11+
class TestHooks(TestBase):
12+
"""Hooks tests."""
13+
14+
def test_backcolor(self):
15+
"""Check page background color feature."""
16+
from markdown_pdf import Section, MarkdownPdf
17+
18+
pdf = MarkdownPdf()
19+
text = open(self.fixture("plantuml.md"), "rt", encoding='utf-8').read()
20+
section = Section(text)
21+
22+
where = section.rect + section.borders
23+
html = pdf.m_d.render(section.text)
24+
story = fitz.Story(html=html, archive=section.root)
25+
26+
story.place(where)
27+
device = pdf.writer.begin_page(section.rect)
28+
story.draw(device)
29+
pdf.writer.end_page()
30+
31+
fname = self.build('backcolor.pdf')
32+
pdf.save(fname)
33+
34+
print("###")
35+
doc = fitz.open(fname)
36+
print("#", doc.page_count)
37+
# https://pymupdf.readthedocs.io/en/latest/recipes-drawing-and-graphics.html
38+
# https://pymupdf.readthedocs.io/en/latest/the-basics.html#adding-a-watermark-to-a-pdf
39+
# insert an image watermark from a file name to fit the page bounds
40+
page = doc[0]
41+
page.insert_image(page.bound(), filename=self.fixture("plantuml.png"), overlay=False, keep_proportion=False)
42+
# https://pypi.org/project/pypng/
43+
doc.save(self.build('backcolor1.pdf'))

0 commit comments

Comments
 (0)