markdown-pdf 1.3__tar.gz → 1.3.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,142 @@
1
+ Metadata-Version: 2.1
2
+ Name: markdown_pdf
3
+ Version: 1.3.1
4
+ Summary: Markdown to pdf renderer
5
+ Home-page: https://github.com/vb64/markdown-pdf
6
+ Author: Vitaly Bogomolov
7
+ Author-email: mail@vitaly-bogomolov.ru
8
+ License: UNKNOWN
9
+ Project-URL: Bug Tracker, https://github.com/vb64/markdown-pdf/issues
10
+ Description: # Module markdown-pdf
11
+
12
+ [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/vb64/markdown-pdf/pep257.yml?label=Pep257&style=plastic&branch=main)](https://github.com/vb64/markdown-pdf/actions?query=workflow%3Apep257)
13
+ [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/vb64/markdown-pdf/py3.yml?label=Python%203.8-3.12&style=plastic&branch=main)](https://github.com/vb64/markdown-pdf/actions?query=workflow%3Apy3)
14
+ [![Codacy Badge](https://app.codacy.com/project/badge/Grade/27b53043bff34f07bfb79ee1672b7ba0)](https://app.codacy.com/gh/vb64/markdown-pdf/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
15
+ [![Codacy Badge](https://app.codacy.com/project/badge/Coverage/27b53043bff34f07bfb79ee1672b7ba0)](https://app.codacy.com/gh/vb64/markdown-pdf/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)
16
+ [![PyPI - Downloads](https://img.shields.io/pypi/dm/markdown-pdf?label=pypi%20installs)](https://pypistats.org/packages/markdown-pdf)
17
+
18
+ The free, open source Python module `markdown-pdf` will create a PDF file from your content in `markdown` format.
19
+
20
+ When creating a PDF file you can:
21
+
22
+ - Use `UTF-8` encoded text in `markdown` in any language
23
+ - Embed images used in `markdown`
24
+ - Break text into pages in the desired order
25
+ - Create a TableOfContents (bookmarks) from markdown headings
26
+ - Tune the necessary elements using your CSS code
27
+ - Use different page sizes within single pdf
28
+ - Create tables in `markdown`
29
+
30
+ The module utilizes the functions of two great libraries.
31
+
32
+ - [markdown-it-py](https://github.com/executablebooks/markdown-it-py) to convert `markdown` to `html`.
33
+ - [PyMuPDF](https://github.com/pymupdf/PyMuPDF) to convert `html` to `pdf`.
34
+
35
+ ## Installation
36
+
37
+ ```bash
38
+ pip install markdown-pdf
39
+ ```
40
+
41
+ ## Usage
42
+
43
+ Create a pdf with TOC (bookmarks) from headings up to level 2.
44
+
45
+ ```python
46
+ from markdown_pdf import MarkdownPdf
47
+
48
+ pdf = MarkdownPdf(toc_level=2)
49
+ ```
50
+
51
+ Add the first section to the pdf. The title is not included in the table of contents.
52
+
53
+ ```python
54
+ from markdown_pdf import Section
55
+
56
+ pdf.add_section(Section("# Title\n", toc=False))
57
+ ```
58
+
59
+ Add a second section. In the pdf file it starts on a new page.
60
+ The title is centered using CSS, included in the table of contents of the pdf file, and an image from the file `img/python.png` is embedded on the page.
61
+
62
+ ```python
63
+ pdf.add_section(
64
+ Section("# Head1\n\n![python](img/python.png)\n\nbody\n"),
65
+ user_css="h1 {text-align:center;}"
66
+ )
67
+ ```
68
+
69
+ Add a third section. Two headings of different levels from this section are included in the TOC of the pdf file.
70
+ The section has landscape orientation of A4 pages.
71
+
72
+ ```python
73
+ pdf.add_section(Section("## Head2\n\n### Head3\n\n", paper_size="A4-L"))
74
+ ```
75
+
76
+ Add a fourth section with a table.
77
+
78
+ ```python
79
+
80
+ text = """# Section with Table
81
+
82
+ |TableHeader1|TableHeader2|
83
+ |--|--|
84
+ |Text1|Text2|
85
+ |ListCell|<ul><li>FirstBullet</li><li>SecondBullet</li></ul>|
86
+ """
87
+
88
+ css = "table, th, td {border: 1px solid black;}"
89
+
90
+ pdf.add_section(Section(text), user_css=css)
91
+ ```
92
+
93
+ Set the properties of the pdf document.
94
+
95
+ ```python
96
+ pdf.meta["title"] = "User Guide"
97
+ pdf.meta["author"] = "Vitaly Bogomolov"
98
+ ```
99
+
100
+ Save to file.
101
+
102
+ ```python
103
+ pdf.save("guide.pdf")
104
+ ```
105
+
106
+ ![Pdf](img/with_toc.png)
107
+
108
+ ## Settings and options
109
+
110
+ The `Section` class defines a portion of `markdown` data,
111
+ which is processed according to the same rules.
112
+ The next `Section` data starts on a new page.
113
+
114
+ The `Section` class can set the following attributes.
115
+
116
+ - toc: whether to include the headers `<h1>` - `<h6>` of this section in the TOC. Default is True.
117
+ - root: the name of the root directory from which the image file paths starts in markdown. Default ".".
118
+ - paper_size: name of paper size, [as described here](https://pymupdf.readthedocs.io/en/latest/functions.html#paper_size). Default "A4".
119
+ - borders: size of borders. Default (36, 36, -36, -36).
120
+
121
+ The following document properties are available for assignment (dictionary `MarkdownPdf.meta`) with the default values indicated.
122
+
123
+ - `creationDate`: current date
124
+ - `modDate`: current date
125
+ - `creator`: "PyMuPDF library: https://pypi.org/project/PyMuPDF"
126
+ - `producer`: ""
127
+ - `title`: ""
128
+ - `author`: ""
129
+ - `subject`: ""
130
+ - `keywords`: ""
131
+
132
+ ## Example
133
+
134
+ As an example, you can download the [pdf file](examples/markdown_pdf.pdf) created from this md file.
135
+ This [Python script](makepdf.py) was used to create the PDF file.
136
+
137
+ Platform: UNKNOWN
138
+ Classifier: Programming Language :: Python :: 3
139
+ Classifier: License :: OSI Approved :: MIT License
140
+ Classifier: Operating System :: OS Independent
141
+ Requires-Python: >=3.8
142
+ Description-Content-Type: text/markdown
@@ -76,7 +76,9 @@ text = """# Section with Table
76
76
  |ListCell|<ul><li>FirstBullet</li><li>SecondBullet</li></ul>|
77
77
  """
78
78
 
79
- pdf.add_section(Section(text))
79
+ css = "table, th, td {border: 1px solid black;}"
80
+
81
+ pdf.add_section(Section(text), user_css=css)
80
82
  ```
81
83
 
82
84
  Set the properties of the pdf document.
@@ -1,5 +1,6 @@
1
1
  """Markdown to pdf converter based on markdown_it and fitz."""
2
2
  import io
3
+ import typing
3
4
  from markdown_it import MarkdownIt
4
5
  import fitz
5
6
 
@@ -7,7 +8,14 @@ import fitz
7
8
  class Section:
8
9
  """Markdown section."""
9
10
 
10
- def __init__(self, text, toc=True, root=".", paper_size="A4", borders=(36, 36, -36, -36)):
11
+ def __init__(
12
+ self,
13
+ text: str,
14
+ toc: bool = True,
15
+ root: str = ".",
16
+ paper_size: str = "A4",
17
+ borders: tuple = (36, 36, -36, -36)
18
+ ):
11
19
  """Create md section with given properties."""
12
20
  self.text = text
13
21
  self.toc = toc
@@ -31,7 +39,7 @@ class MarkdownPdf:
31
39
  "keywords": None,
32
40
  }
33
41
 
34
- def __init__(self, toc_level=6, mode='commonmark'):
42
+ def __init__(self, toc_level: int = 6, mode: str = 'commonmark'):
35
43
  """Create md -> pdf converter with given TOC level and mode of md parsing."""
36
44
  self.toc_level = toc_level
37
45
  self.toc = []
@@ -45,7 +53,7 @@ class MarkdownPdf:
45
53
  self.page = 0
46
54
 
47
55
  @staticmethod
48
- def recorder(elpos):
56
+ def _recorder(elpos):
49
57
  """Call function invoked during story.place() for making a TOC."""
50
58
  if not elpos.open_close & 1: # only consider "open" items
51
59
  return
@@ -60,7 +68,7 @@ class MarkdownPdf:
60
68
  elpos.rect[1], # top of written rectangle (use for TOC)
61
69
  ))
62
70
 
63
- def add_section(self, section, user_css=None):
71
+ def add_section(self, section: Section, user_css: typing.Optional[str] = None) -> None:
64
72
  """Add markdown section to pdf."""
65
73
  rect = fitz.paper_rect(section.paper_size)
66
74
  where = rect + section.borders
@@ -70,11 +78,11 @@ class MarkdownPdf:
70
78
  self.page += 1
71
79
  device = self.writer.begin_page(rect)
72
80
  more, _ = story.place(where) # layout into allowed rectangle
73
- story.element_positions(self.recorder, {"toc": section.toc, "pdfile": self})
81
+ story.element_positions(self._recorder, {"toc": section.toc, "pdfile": self})
74
82
  story.draw(device)
75
83
  self.writer.end_page()
76
84
 
77
- def save(self, file_name):
85
+ def save(self, file_name: str) -> None:
78
86
  """Save pdf to file."""
79
87
  self.writer.close()
80
88
  doc = fitz.open("pdf", self.out_file)
@@ -0,0 +1,142 @@
1
+ Metadata-Version: 2.1
2
+ Name: markdown-pdf
3
+ Version: 1.3.1
4
+ Summary: Markdown to pdf renderer
5
+ Home-page: https://github.com/vb64/markdown-pdf
6
+ Author: Vitaly Bogomolov
7
+ Author-email: mail@vitaly-bogomolov.ru
8
+ License: UNKNOWN
9
+ Project-URL: Bug Tracker, https://github.com/vb64/markdown-pdf/issues
10
+ Description: # Module markdown-pdf
11
+
12
+ [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/vb64/markdown-pdf/pep257.yml?label=Pep257&style=plastic&branch=main)](https://github.com/vb64/markdown-pdf/actions?query=workflow%3Apep257)
13
+ [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/vb64/markdown-pdf/py3.yml?label=Python%203.8-3.12&style=plastic&branch=main)](https://github.com/vb64/markdown-pdf/actions?query=workflow%3Apy3)
14
+ [![Codacy Badge](https://app.codacy.com/project/badge/Grade/27b53043bff34f07bfb79ee1672b7ba0)](https://app.codacy.com/gh/vb64/markdown-pdf/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
15
+ [![Codacy Badge](https://app.codacy.com/project/badge/Coverage/27b53043bff34f07bfb79ee1672b7ba0)](https://app.codacy.com/gh/vb64/markdown-pdf/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)
16
+ [![PyPI - Downloads](https://img.shields.io/pypi/dm/markdown-pdf?label=pypi%20installs)](https://pypistats.org/packages/markdown-pdf)
17
+
18
+ The free, open source Python module `markdown-pdf` will create a PDF file from your content in `markdown` format.
19
+
20
+ When creating a PDF file you can:
21
+
22
+ - Use `UTF-8` encoded text in `markdown` in any language
23
+ - Embed images used in `markdown`
24
+ - Break text into pages in the desired order
25
+ - Create a TableOfContents (bookmarks) from markdown headings
26
+ - Tune the necessary elements using your CSS code
27
+ - Use different page sizes within single pdf
28
+ - Create tables in `markdown`
29
+
30
+ The module utilizes the functions of two great libraries.
31
+
32
+ - [markdown-it-py](https://github.com/executablebooks/markdown-it-py) to convert `markdown` to `html`.
33
+ - [PyMuPDF](https://github.com/pymupdf/PyMuPDF) to convert `html` to `pdf`.
34
+
35
+ ## Installation
36
+
37
+ ```bash
38
+ pip install markdown-pdf
39
+ ```
40
+
41
+ ## Usage
42
+
43
+ Create a pdf with TOC (bookmarks) from headings up to level 2.
44
+
45
+ ```python
46
+ from markdown_pdf import MarkdownPdf
47
+
48
+ pdf = MarkdownPdf(toc_level=2)
49
+ ```
50
+
51
+ Add the first section to the pdf. The title is not included in the table of contents.
52
+
53
+ ```python
54
+ from markdown_pdf import Section
55
+
56
+ pdf.add_section(Section("# Title\n", toc=False))
57
+ ```
58
+
59
+ Add a second section. In the pdf file it starts on a new page.
60
+ The title is centered using CSS, included in the table of contents of the pdf file, and an image from the file `img/python.png` is embedded on the page.
61
+
62
+ ```python
63
+ pdf.add_section(
64
+ Section("# Head1\n\n![python](img/python.png)\n\nbody\n"),
65
+ user_css="h1 {text-align:center;}"
66
+ )
67
+ ```
68
+
69
+ Add a third section. Two headings of different levels from this section are included in the TOC of the pdf file.
70
+ The section has landscape orientation of A4 pages.
71
+
72
+ ```python
73
+ pdf.add_section(Section("## Head2\n\n### Head3\n\n", paper_size="A4-L"))
74
+ ```
75
+
76
+ Add a fourth section with a table.
77
+
78
+ ```python
79
+
80
+ text = """# Section with Table
81
+
82
+ |TableHeader1|TableHeader2|
83
+ |--|--|
84
+ |Text1|Text2|
85
+ |ListCell|<ul><li>FirstBullet</li><li>SecondBullet</li></ul>|
86
+ """
87
+
88
+ css = "table, th, td {border: 1px solid black;}"
89
+
90
+ pdf.add_section(Section(text), user_css=css)
91
+ ```
92
+
93
+ Set the properties of the pdf document.
94
+
95
+ ```python
96
+ pdf.meta["title"] = "User Guide"
97
+ pdf.meta["author"] = "Vitaly Bogomolov"
98
+ ```
99
+
100
+ Save to file.
101
+
102
+ ```python
103
+ pdf.save("guide.pdf")
104
+ ```
105
+
106
+ ![Pdf](img/with_toc.png)
107
+
108
+ ## Settings and options
109
+
110
+ The `Section` class defines a portion of `markdown` data,
111
+ which is processed according to the same rules.
112
+ The next `Section` data starts on a new page.
113
+
114
+ The `Section` class can set the following attributes.
115
+
116
+ - toc: whether to include the headers `<h1>` - `<h6>` of this section in the TOC. Default is True.
117
+ - root: the name of the root directory from which the image file paths starts in markdown. Default ".".
118
+ - paper_size: name of paper size, [as described here](https://pymupdf.readthedocs.io/en/latest/functions.html#paper_size). Default "A4".
119
+ - borders: size of borders. Default (36, 36, -36, -36).
120
+
121
+ The following document properties are available for assignment (dictionary `MarkdownPdf.meta`) with the default values indicated.
122
+
123
+ - `creationDate`: current date
124
+ - `modDate`: current date
125
+ - `creator`: "PyMuPDF library: https://pypi.org/project/PyMuPDF"
126
+ - `producer`: ""
127
+ - `title`: ""
128
+ - `author`: ""
129
+ - `subject`: ""
130
+ - `keywords`: ""
131
+
132
+ ## Example
133
+
134
+ As an example, you can download the [pdf file](examples/markdown_pdf.pdf) created from this md file.
135
+ This [Python script](makepdf.py) was used to create the PDF file.
136
+
137
+ Platform: UNKNOWN
138
+ Classifier: Programming Language :: Python :: 3
139
+ Classifier: License :: OSI Approved :: MIT License
140
+ Classifier: Operating System :: OS Independent
141
+ Requires-Python: >=3.8
142
+ Description-Content-Type: text/markdown
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = markdown_pdf
3
- version = 1.3
3
+ version = 1.3.1
4
4
  author = Vitaly Bogomolov
5
5
  author_email = mail@vitaly-bogomolov.ru
6
6
  description = Markdown to pdf renderer
markdown_pdf-1.3/PKG-INFO DELETED
@@ -1,141 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: markdown_pdf
3
- Version: 1.3
4
- Summary: Markdown to pdf renderer
5
- Home-page: https://github.com/vb64/markdown-pdf
6
- Author: Vitaly Bogomolov
7
- Author-email: mail@vitaly-bogomolov.ru
8
- Project-URL: Bug Tracker, https://github.com/vb64/markdown-pdf/issues
9
- Classifier: Programming Language :: Python :: 3
10
- Classifier: License :: OSI Approved :: MIT License
11
- Classifier: Operating System :: OS Independent
12
- Requires-Python: >=3.8
13
- Description-Content-Type: text/markdown
14
- License-File: LICENSE
15
- Requires-Dist: PyMuPDF==1.24.2
16
- Requires-Dist: markdown-it-py==3.0.0
17
-
18
- # Module markdown-pdf
19
-
20
- [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/vb64/markdown-pdf/pep257.yml?label=Pep257&style=plastic&branch=main)](https://github.com/vb64/markdown-pdf/actions?query=workflow%3Apep257)
21
- [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/vb64/markdown-pdf/py3.yml?label=Python%203.8-3.12&style=plastic&branch=main)](https://github.com/vb64/markdown-pdf/actions?query=workflow%3Apy3)
22
- [![Codacy Badge](https://app.codacy.com/project/badge/Grade/27b53043bff34f07bfb79ee1672b7ba0)](https://app.codacy.com/gh/vb64/markdown-pdf/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
23
- [![Codacy Badge](https://app.codacy.com/project/badge/Coverage/27b53043bff34f07bfb79ee1672b7ba0)](https://app.codacy.com/gh/vb64/markdown-pdf/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)
24
- [![PyPI - Downloads](https://img.shields.io/pypi/dm/markdown-pdf?label=pypi%20installs)](https://pypistats.org/packages/markdown-pdf)
25
-
26
- The free, open source Python module `markdown-pdf` will create a PDF file from your content in `markdown` format.
27
-
28
- When creating a PDF file you can:
29
-
30
- - Use `UTF-8` encoded text in `markdown` in any language
31
- - Embed images used in `markdown`
32
- - Break text into pages in the desired order
33
- - Create a TableOfContents (bookmarks) from markdown headings
34
- - Tune the necessary elements using your CSS code
35
- - Use different page sizes within single pdf
36
- - Create tables in `markdown`
37
-
38
- The module utilizes the functions of two great libraries.
39
-
40
- - [markdown-it-py](https://github.com/executablebooks/markdown-it-py) to convert `markdown` to `html`.
41
- - [PyMuPDF](https://github.com/pymupdf/PyMuPDF) to convert `html` to `pdf`.
42
-
43
- ## Installation
44
-
45
- ```bash
46
- pip install markdown-pdf
47
- ```
48
-
49
- ## Usage
50
-
51
- Create a pdf with TOC (bookmarks) from headings up to level 2.
52
-
53
- ```python
54
- from markdown_pdf import MarkdownPdf
55
-
56
- pdf = MarkdownPdf(toc_level=2)
57
- ```
58
-
59
- Add the first section to the pdf. The title is not included in the table of contents.
60
-
61
- ```python
62
- from markdown_pdf import Section
63
-
64
- pdf.add_section(Section("# Title\n", toc=False))
65
- ```
66
-
67
- Add a second section. In the pdf file it starts on a new page.
68
- The title is centered using CSS, included in the table of contents of the pdf file, and an image from the file `img/python.png` is embedded on the page.
69
-
70
- ```python
71
- pdf.add_section(
72
- Section("# Head1\n\n![python](img/python.png)\n\nbody\n"),
73
- user_css="h1 {text-align:center;}"
74
- )
75
- ```
76
-
77
- Add a third section. Two headings of different levels from this section are included in the TOC of the pdf file.
78
- The section has landscape orientation of A4 pages.
79
-
80
- ```python
81
- pdf.add_section(Section("## Head2\n\n### Head3\n\n", paper_size="A4-L"))
82
- ```
83
-
84
- Add a fourth section with a table.
85
-
86
- ```python
87
-
88
- text = """# Section with Table
89
-
90
- |TableHeader1|TableHeader2|
91
- |--|--|
92
- |Text1|Text2|
93
- |ListCell|<ul><li>FirstBullet</li><li>SecondBullet</li></ul>|
94
- """
95
-
96
- pdf.add_section(Section(text))
97
- ```
98
-
99
- Set the properties of the pdf document.
100
-
101
- ```python
102
- pdf.meta["title"] = "User Guide"
103
- pdf.meta["author"] = "Vitaly Bogomolov"
104
- ```
105
-
106
- Save to file.
107
-
108
- ```python
109
- pdf.save("guide.pdf")
110
- ```
111
-
112
- ![Pdf](img/with_toc.png)
113
-
114
- ## Settings and options
115
-
116
- The `Section` class defines a portion of `markdown` data,
117
- which is processed according to the same rules.
118
- The next `Section` data starts on a new page.
119
-
120
- The `Section` class can set the following attributes.
121
-
122
- - toc: whether to include the headers `<h1>` - `<h6>` of this section in the TOC. Default is True.
123
- - root: the name of the root directory from which the image file paths starts in markdown. Default ".".
124
- - paper_size: name of paper size, [as described here](https://pymupdf.readthedocs.io/en/latest/functions.html#paper_size). Default "A4".
125
- - borders: size of borders. Default (36, 36, -36, -36).
126
-
127
- The following document properties are available for assignment (dictionary `MarkdownPdf.meta`) with the default values indicated.
128
-
129
- - `creationDate`: current date
130
- - `modDate`: current date
131
- - `creator`: "PyMuPDF library: https://pypi.org/project/PyMuPDF"
132
- - `producer`: ""
133
- - `title`: ""
134
- - `author`: ""
135
- - `subject`: ""
136
- - `keywords`: ""
137
-
138
- ## Example
139
-
140
- As an example, you can download the [pdf file](examples/markdown_pdf.pdf) created from this md file.
141
- This [Python script](makepdf.py) was used to create the PDF file.
@@ -1,141 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: markdown_pdf
3
- Version: 1.3
4
- Summary: Markdown to pdf renderer
5
- Home-page: https://github.com/vb64/markdown-pdf
6
- Author: Vitaly Bogomolov
7
- Author-email: mail@vitaly-bogomolov.ru
8
- Project-URL: Bug Tracker, https://github.com/vb64/markdown-pdf/issues
9
- Classifier: Programming Language :: Python :: 3
10
- Classifier: License :: OSI Approved :: MIT License
11
- Classifier: Operating System :: OS Independent
12
- Requires-Python: >=3.8
13
- Description-Content-Type: text/markdown
14
- License-File: LICENSE
15
- Requires-Dist: PyMuPDF==1.24.2
16
- Requires-Dist: markdown-it-py==3.0.0
17
-
18
- # Module markdown-pdf
19
-
20
- [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/vb64/markdown-pdf/pep257.yml?label=Pep257&style=plastic&branch=main)](https://github.com/vb64/markdown-pdf/actions?query=workflow%3Apep257)
21
- [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/vb64/markdown-pdf/py3.yml?label=Python%203.8-3.12&style=plastic&branch=main)](https://github.com/vb64/markdown-pdf/actions?query=workflow%3Apy3)
22
- [![Codacy Badge](https://app.codacy.com/project/badge/Grade/27b53043bff34f07bfb79ee1672b7ba0)](https://app.codacy.com/gh/vb64/markdown-pdf/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
23
- [![Codacy Badge](https://app.codacy.com/project/badge/Coverage/27b53043bff34f07bfb79ee1672b7ba0)](https://app.codacy.com/gh/vb64/markdown-pdf/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)
24
- [![PyPI - Downloads](https://img.shields.io/pypi/dm/markdown-pdf?label=pypi%20installs)](https://pypistats.org/packages/markdown-pdf)
25
-
26
- The free, open source Python module `markdown-pdf` will create a PDF file from your content in `markdown` format.
27
-
28
- When creating a PDF file you can:
29
-
30
- - Use `UTF-8` encoded text in `markdown` in any language
31
- - Embed images used in `markdown`
32
- - Break text into pages in the desired order
33
- - Create a TableOfContents (bookmarks) from markdown headings
34
- - Tune the necessary elements using your CSS code
35
- - Use different page sizes within single pdf
36
- - Create tables in `markdown`
37
-
38
- The module utilizes the functions of two great libraries.
39
-
40
- - [markdown-it-py](https://github.com/executablebooks/markdown-it-py) to convert `markdown` to `html`.
41
- - [PyMuPDF](https://github.com/pymupdf/PyMuPDF) to convert `html` to `pdf`.
42
-
43
- ## Installation
44
-
45
- ```bash
46
- pip install markdown-pdf
47
- ```
48
-
49
- ## Usage
50
-
51
- Create a pdf with TOC (bookmarks) from headings up to level 2.
52
-
53
- ```python
54
- from markdown_pdf import MarkdownPdf
55
-
56
- pdf = MarkdownPdf(toc_level=2)
57
- ```
58
-
59
- Add the first section to the pdf. The title is not included in the table of contents.
60
-
61
- ```python
62
- from markdown_pdf import Section
63
-
64
- pdf.add_section(Section("# Title\n", toc=False))
65
- ```
66
-
67
- Add a second section. In the pdf file it starts on a new page.
68
- The title is centered using CSS, included in the table of contents of the pdf file, and an image from the file `img/python.png` is embedded on the page.
69
-
70
- ```python
71
- pdf.add_section(
72
- Section("# Head1\n\n![python](img/python.png)\n\nbody\n"),
73
- user_css="h1 {text-align:center;}"
74
- )
75
- ```
76
-
77
- Add a third section. Two headings of different levels from this section are included in the TOC of the pdf file.
78
- The section has landscape orientation of A4 pages.
79
-
80
- ```python
81
- pdf.add_section(Section("## Head2\n\n### Head3\n\n", paper_size="A4-L"))
82
- ```
83
-
84
- Add a fourth section with a table.
85
-
86
- ```python
87
-
88
- text = """# Section with Table
89
-
90
- |TableHeader1|TableHeader2|
91
- |--|--|
92
- |Text1|Text2|
93
- |ListCell|<ul><li>FirstBullet</li><li>SecondBullet</li></ul>|
94
- """
95
-
96
- pdf.add_section(Section(text))
97
- ```
98
-
99
- Set the properties of the pdf document.
100
-
101
- ```python
102
- pdf.meta["title"] = "User Guide"
103
- pdf.meta["author"] = "Vitaly Bogomolov"
104
- ```
105
-
106
- Save to file.
107
-
108
- ```python
109
- pdf.save("guide.pdf")
110
- ```
111
-
112
- ![Pdf](img/with_toc.png)
113
-
114
- ## Settings and options
115
-
116
- The `Section` class defines a portion of `markdown` data,
117
- which is processed according to the same rules.
118
- The next `Section` data starts on a new page.
119
-
120
- The `Section` class can set the following attributes.
121
-
122
- - toc: whether to include the headers `<h1>` - `<h6>` of this section in the TOC. Default is True.
123
- - root: the name of the root directory from which the image file paths starts in markdown. Default ".".
124
- - paper_size: name of paper size, [as described here](https://pymupdf.readthedocs.io/en/latest/functions.html#paper_size). Default "A4".
125
- - borders: size of borders. Default (36, 36, -36, -36).
126
-
127
- The following document properties are available for assignment (dictionary `MarkdownPdf.meta`) with the default values indicated.
128
-
129
- - `creationDate`: current date
130
- - `modDate`: current date
131
- - `creator`: "PyMuPDF library: https://pypi.org/project/PyMuPDF"
132
- - `producer`: ""
133
- - `title`: ""
134
- - `author`: ""
135
- - `subject`: ""
136
- - `keywords`: ""
137
-
138
- ## Example
139
-
140
- As an example, you can download the [pdf file](examples/markdown_pdf.pdf) created from this md file.
141
- This [Python script](makepdf.py) was used to create the PDF file.
File without changes
File without changes
File without changes