Skip to content

Commit

Permalink
Add support for pathlib.Path paths in type hints for save method. (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
vb64 authored Dec 22, 2024
1 parent 45369a8 commit 8c27fc9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion markdown_pdf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Markdown to pdf converter based on markdown_it and fitz."""
import io
import typing
import pathlib
from markdown_it import MarkdownIt
import fitz

Expand Down Expand Up @@ -82,7 +83,7 @@ def add_section(self, section: Section, user_css: typing.Optional[str] = None) -
story.draw(device)
self.writer.end_page()

def save(self, file_name: str) -> None:
def save(self, file_name: typing.Union[str, pathlib.Path]) -> None:
"""Save pdf to file."""
self.writer.close()
doc = fitz.open("pdf", self.out_file)
Expand Down
11 changes: 11 additions & 0 deletions tests/test/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,14 @@ def test_table(self):
pdf = MarkdownPdf(toc_level=0)
pdf.add_section(Section(TABLE_TEXT))
pdf.save(self.build("table.pdf"))

def test_pathlib(self):
"""Check pathlib.Path for save method."""
from pathlib import Path
from markdown_pdf import MarkdownPdf, Section

pdf = MarkdownPdf()
pdf.add_section(Section('# Hello World'))

file_path = Path(self.build('output.pdf'))
pdf.save(file_path)

0 comments on commit 8c27fc9

Please sign in to comment.