Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Tables #22

Merged
merged 9 commits into from
Jun 11, 2024
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
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ __pycache__
*.sqlite3
.coverage
htmlcov/*
venv/*
venv.*
venv*
dist/
*.egg-info/
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ When creating a PDF file you can:
- Create a TableOfContents (bookmarks) from markdown headings
- Tune the necessary elements using your CSS code
- Use different page sizes within single pdf
- Create tables in `markdown`

The module utilizes the functions of two great libraries.

Expand Down Expand Up @@ -63,6 +64,21 @@ The section has landscape orientation of A4 pages.
pdf.add_section(Section("## Head2\n\n### Head3\n\n", paper_size="A4-L"))
```

Add a fourth section with a table.

```python

text = """# Section with Table

|TableHeader1|TableHeader2|
|--|--|
|Text1|Text2|
|ListCell|<ul><li>FirstBullet</li><li>SecondBullet</li></ul>|
"""

pdf.add_section(Section(text))
```

Set the properties of the pdf document.

```python
Expand Down
16 changes: 16 additions & 0 deletions README_ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Создавать оглавление (bookmarks) из заголовков markdown
- Оформлять нужные элементы при помощи вашего CSS кода
- Использовать разные размеры страниц внутри одного pdf
- Создавать таблицы в `markdown`

Модуль использует функции двух замечательных библиотек.

Expand Down Expand Up @@ -57,6 +58,21 @@ pdf.add_section(
pdf.add_section(Section("## Head2\n\n### Head3\n\n", paper_size="A4-L"))
```

Добавляем четвертую секцию с таблицей.

```python

text = """# Section with Table

|TableHeader1|TableHeader2|
|--|--|
|Text1|Text2|
|ListCell|<ul><li>FirstBullet</li><li>SecondBullet</li></ul>|
"""

pdf.add_section(Section(text))
```

Устанавливаем свойства pdf документа.

```python
Expand Down
Binary file modified examples/markdown_pdf.pdf
Binary file not shown.
Binary file modified examples/markdown_pdf_ru.pdf
Binary file not shown.
Binary file modified img/with_toc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion markdown_pdf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, toc_level=6, mode='commonmark'):

# zero, commonmark, js-default, gfm-like
# https://markdown-it-py.readthedocs.io/en/latest/using.html#quick-start
self.m_d = (MarkdownIt(mode))
self.m_d = (MarkdownIt(mode).enable('table')) # Enable support for tables

self.out_file = io.BytesIO()
self.writer = fitz.DocumentWriter(self.out_file)
Expand Down
23 changes: 23 additions & 0 deletions tests/test/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
"""
from . import TestBase

TABLE_TEXT = """# Section with Table

|TableHeader1|TableHeader2|
|--|--|
|Text1|Text2|
|ListCell|<ul><li>FirstBullet</li><li>SecondBullet</li></ul>|
"""


class TestConverter(TestBase):
"""Converter markdown_pdf."""
Expand All @@ -19,6 +27,7 @@ def test_with_toc(self):
user_css="h1 {text-align:center;}"
)
pdf.add_section(Section("## Head2\n\n### Head3\n\n"))
pdf.add_section(Section(TABLE_TEXT))
pdf.save(self.build("with_toc.pdf"))

def test_no_toc(self):
Expand All @@ -28,3 +37,17 @@ def test_no_toc(self):
pdf = MarkdownPdf(toc_level=0)
pdf.add_section(Section("# Title\n"))
pdf.save(self.build("no_toc.pdf"))

def test_table(self):
"""Convert md table."""
# https://github.com/executablebooks/markdown-it-py?tab=readme-ov-file#python-api-usage
from markdown_it import MarkdownIt
from markdown_pdf import Section, MarkdownPdf

md = MarkdownIt('commonmark').enable('table')
html_text = md.render(TABLE_TEXT)
assert '<table>' in html_text

pdf = MarkdownPdf(toc_level=0)
pdf.add_section(Section(TABLE_TEXT))
pdf.save(self.build("table.pdf"))
Loading