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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ The `Section` class can set the following attributes.

- toc: whether to include the headers `<h1>` - `<h6>` of this section in the TOC. Default is True.
- root: the name of the root directory from which the image file paths starts in markdown. Default ".".
- paper_size: name of paper size, [as described here](https://pymupdf.readthedocs.io/en/latest/functions.html#paper_size). Default "A4".
- paper_size: either the name of a paper size, [as described here](https://pymupdf.readthedocs.io/en/latest/functions.html#paper_size), or a list containing the width and height in mm. Default "A4".
- borders: size of borders. Default (36, 36, -36, -36).

The following document properties are available for assignment (dictionary `MarkdownPdf.meta`) with the default values indicated.
Expand Down
12 changes: 9 additions & 3 deletions markdown_pdf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import fitz
from markdown_it import MarkdownIt
from pymupdf import (
_as_pdf_document, mupdf, JM_embedded_clean, JM_ensure_identity, JM_new_output_fileptr, ASSERT_PDF,
_as_pdf_document, mupdf, JM_embedded_clean, JM_ensure_identity, JM_new_output_fileptr, ASSERT_PDF, Rect,
)


Expand All @@ -18,7 +18,7 @@ def __init__(
text: str,
toc: bool = True,
root: str = ".",
paper_size: str = "A4",
paper_size: str|list = "A4",
borders: typing.Tuple[int, int, int, int] = (36, 36, -36, -36)
):
"""Create md section with given properties."""
Expand Down Expand Up @@ -81,7 +81,13 @@ def _recorder(elpos):

def add_section(self, section: Section, user_css: typing.Optional[str] = None) -> str:
"""Add markdown section to pdf."""
rect = fitz.paper_rect(section.paper_size)
if isinstance(section.paper_size, str):
rect = fitz.paper_rect(section.paper_size)
elif isinstance(section.paper_size, list):
# Other paper sizes are in pt, so need to times mm by 2.835.
rect = Rect(0.0, 0.0, (section.paper_size[0] * 2.835), (section.paper_size[1] * 2.835))
else:
raise TypeError("paper_size must be 'str' or 'list'")
where = rect + section.borders
html = self.m_d.render(section.text)
story = fitz.Story(html=html, archive=section.root, user_css=user_css)
Expand Down
Loading