Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ pdf = MarkdownPdf(toc_level=2, optimize=True)
```

Add the first section to the pdf. The title is not included in the table of contents.
After adding a section to a pdf, the `page_count` property in the section contains the number of pdf pages created for the added section.

```python
from markdown_pdf import Section

pdf.add_section(Section("# Title\n", toc=False))
section = Section("# Title\n", toc=False)
assert section.page_count == 0
pdf.add_section(section)
assert section.page_count == 1
```

Add a second section with external and internal hyperlinks.
Expand Down
6 changes: 5 additions & 1 deletion README_ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ pdf = MarkdownPdf(toc_level=2)
```

Добавляем в pdf первую секцию. Заголовок не включаем в оглавление.
После добавления секции в pdf свойство `page_count` у секции содержит количество созданных страниц pdf для добавленной секции.

```python
from markdown_pdf import Section

pdf.add_section(Section("# Title\n", toc=False))
section = Section("# Title\n", toc=False)
assert section.page_count == 0
pdf.add_section(section)
assert section.page_count == 1
```

Добавляем вторую секцию с внешними и внутренними гипер-ссылками.
Expand Down
2 changes: 2 additions & 0 deletions history.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
+ Section.page_count contains number of pdf pages that has been generated for section after add_section call.

12.02.2026 ver.1.12
-------------------

Expand Down
2 changes: 2 additions & 0 deletions markdown_pdf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(
self.text = text
self.toc = toc
self.root = root
self.page_count = 0

self.paper_size = paper_size
if isinstance(paper_size, str):
Expand Down Expand Up @@ -120,6 +121,7 @@ def add_section(self, section: Section, user_css: typing.Optional[str] = None) -
more = 1
while more: # loop outputting the story
self.page_num += 1
section.page_count += 1
device = self.writer.begin_page(section.rect)
more, _ = story.place(where) # layout into allowed rectangle
story.element_positions(self._recorder, {"toc": section.toc, "pdfile": self})
Expand Down
8 changes: 5 additions & 3 deletions tests/test/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ def test_hrefs(self):
"""Convert hrefs content to pdf."""
from markdown_pdf import Section, MarkdownPdf

sect = Section(open(self.fixture("hrefs.md"), "rt", encoding='utf-8').read())
assert sect.page_count == 0
pdf = MarkdownPdf()
pdf.add_section(
Section(open(self.fixture("hrefs.md"), "rt", encoding='utf-8').read())
)
pdf.add_section(sect)
assert sect.page_count == 1

pdf.save(self.build("hrefs.pdf"))

def test_bytes(self):
Expand Down