markdown-pdf 1.7__tar.gz → 1.8__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: markdown_pdf
3
- Version: 1.7
3
+ Version: 1.8
4
4
  Summary: Markdown to pdf renderer
5
5
  Author-email: Vitaly Bogomolov <mail@vitaly-bogomolov.ru>
6
6
  License: GNU Affero General Public License v3
@@ -134,6 +134,16 @@ Save to file.
134
134
  pdf.save("guide.pdf")
135
135
  ```
136
136
 
137
+ Or save to file-like object.
138
+
139
+ ```python
140
+ import io
141
+
142
+ out = io.BytesIO()
143
+ pdf.save_bytes(out)
144
+ assert out.getbuffer().nbytes > 0
145
+ ```
146
+
137
147
  ![Pdf](img/with_toc.png)
138
148
 
139
149
  ## Settings and options
@@ -109,6 +109,16 @@ Save to file.
109
109
  pdf.save("guide.pdf")
110
110
  ```
111
111
 
112
+ Or save to file-like object.
113
+
114
+ ```python
115
+ import io
116
+
117
+ out = io.BytesIO()
118
+ pdf.save_bytes(out)
119
+ assert out.getbuffer().nbytes > 0
120
+ ```
121
+
112
122
  ![Pdf](img/with_toc.png)
113
123
 
114
124
  ## Settings and options
@@ -3,9 +3,11 @@ import io
3
3
  import typing
4
4
  import pathlib
5
5
 
6
-
7
- from markdown_it import MarkdownIt
8
6
  import fitz
7
+ from markdown_it import MarkdownIt
8
+ from pymupdf import (
9
+ _as_pdf_document, mupdf, JM_embedded_clean, JM_ensure_identity, JM_new_output_fileptr, ASSERT_PDF,
10
+ )
9
11
 
10
12
 
11
13
  class Section:
@@ -94,16 +96,59 @@ class MarkdownPdf:
94
96
 
95
97
  return html
96
98
 
97
- def save(self, file_name: typing.Union[str, pathlib.Path]) -> None:
98
- """Save pdf to file."""
99
+ def _make_doc(self):
100
+ """Return fitz doc object."""
99
101
  self.writer.close()
100
102
  self.out_file.seek(0)
101
103
  doc = fitz.Story.add_pdf_links(self.out_file, self.hrefs)
102
104
  doc.set_metadata(self.meta)
103
105
  if self.toc_level > 0:
104
106
  doc.set_toc(self.toc)
107
+
108
+ return doc
109
+
110
+ def save(self, file_name: typing.Union[str, pathlib.Path]) -> None:
111
+ """Save pdf to file."""
112
+ doc = self._make_doc()
105
113
  if self.optimize:
106
114
  doc.ez_save(file_name)
107
115
  else:
108
116
  doc.save(file_name)
109
117
  doc.close()
118
+
119
+ def save_bytes(self, bytesio: io.BytesIO) -> int:
120
+ """Save pdf to file-like object and return byte size of the filled object."""
121
+ doc = self._make_doc()
122
+
123
+ pdf = _as_pdf_document(doc)
124
+ opts = mupdf.PdfWriteOptions()
125
+
126
+ opts.do_incremental = 0
127
+ opts.do_ascii = 0
128
+ opts.do_compress = 0
129
+ opts.do_compress_images = 0
130
+ opts.do_compress_fonts = 0
131
+ opts.do_decompress = 0
132
+ opts.do_garbage = 0
133
+ opts.do_pretty = 0
134
+ opts.do_linear = 0
135
+ opts.do_clean = 0
136
+ opts.do_sanitize = 0
137
+ opts.dont_regenerate_id = 0
138
+ opts.do_appearance = 0
139
+ opts.do_encrypt = 1
140
+ opts.permissions = 4095
141
+ opts.do_preserve_metadata = 1
142
+ opts.do_use_objstms = 0
143
+ opts.compression_effort = 0
144
+
145
+ ASSERT_PDF(pdf)
146
+ pdf.m_internal.resynth_required = 0
147
+ JM_embedded_clean(pdf)
148
+ JM_ensure_identity(pdf)
149
+
150
+ out = JM_new_output_fileptr(bytesio)
151
+ mupdf.pdf_write_document(pdf, out, opts)
152
+ out.fz_close_output()
153
+
154
+ return bytesio.getbuffer().nbytes
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: markdown_pdf
3
- Version: 1.7
3
+ Version: 1.8
4
4
  Summary: Markdown to pdf renderer
5
5
  Author-email: Vitaly Bogomolov <mail@vitaly-bogomolov.ru>
6
6
  License: GNU Affero General Public License v3
@@ -134,6 +134,16 @@ Save to file.
134
134
  pdf.save("guide.pdf")
135
135
  ```
136
136
 
137
+ Or save to file-like object.
138
+
139
+ ```python
140
+ import io
141
+
142
+ out = io.BytesIO()
143
+ pdf.save_bytes(out)
144
+ assert out.getbuffer().nbytes > 0
145
+ ```
146
+
137
147
  ![Pdf](img/with_toc.png)
138
148
 
139
149
  ## Settings and options
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
7
7
 
8
8
  [project]
9
9
  name = "markdown_pdf"
10
- version = "1.7"
10
+ version = "1.8"
11
11
  description = "Markdown to pdf renderer"
12
12
  readme = { file = "README.md", content-type = "text/markdown" }
13
13
  requires-python = ">=3.8"
File without changes
File without changes
File without changes