polytext 0.1.1__tar.gz → 0.1.2__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.
- {polytext-0.1.1 → polytext-0.1.2}/PKG-INFO +1 -1
- {polytext-0.1.1 → polytext-0.1.2}/polytext/generator/pdf.py +20 -8
- {polytext-0.1.1 → polytext-0.1.2}/polytext.egg-info/PKG-INFO +1 -1
- {polytext-0.1.1 → polytext-0.1.2}/setup.py +1 -1
- {polytext-0.1.1 → polytext-0.1.2}/LICENSE +0 -0
- {polytext-0.1.1 → polytext-0.1.2}/README.md +0 -0
- {polytext-0.1.1 → polytext-0.1.2}/polytext/__init__.py +0 -0
- {polytext-0.1.1 → polytext-0.1.2}/polytext/converter/__init__.py +0 -0
- {polytext-0.1.1 → polytext-0.1.2}/polytext/converter/pdf.py +0 -0
- {polytext-0.1.1 → polytext-0.1.2}/polytext/exceptions/__init__.py +0 -0
- {polytext-0.1.1 → polytext-0.1.2}/polytext/exceptions/base.py +0 -0
- {polytext-0.1.1 → polytext-0.1.2}/polytext/generator/__init__.py +0 -0
- {polytext-0.1.1 → polytext-0.1.2}/polytext/loader/__init__.py +0 -0
- {polytext-0.1.1 → polytext-0.1.2}/polytext/loader/text.py +0 -0
- {polytext-0.1.1 → polytext-0.1.2}/polytext.egg-info/SOURCES.txt +0 -0
- {polytext-0.1.1 → polytext-0.1.2}/polytext.egg-info/dependency_links.txt +0 -0
- {polytext-0.1.1 → polytext-0.1.2}/polytext.egg-info/not-zip-safe +0 -0
- {polytext-0.1.1 → polytext-0.1.2}/polytext.egg-info/requires.txt +0 -0
- {polytext-0.1.1 → polytext-0.1.2}/polytext.egg-info/top_level.txt +0 -0
- {polytext-0.1.1 → polytext-0.1.2}/pyproject.toml +0 -0
- {polytext-0.1.1 → polytext-0.1.2}/setup.cfg +0 -0
- {polytext-0.1.1 → polytext-0.1.2}/tests/test_extract_text_from_file.py +0 -0
- {polytext-0.1.1 → polytext-0.1.2}/tests/test_get_customized_pdf_from_markdown.py +0 -0
- {polytext-0.1.1 → polytext-0.1.2}/tests/test_get_document_text.py +0 -0
|
@@ -9,17 +9,20 @@ from weasyprint.text.fonts import FontConfiguration
|
|
|
9
9
|
logger = logging.getLogger(__name__)
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
def get_customized_pdf_from_markdown(input_markdown):
|
|
12
|
+
def get_customized_pdf_from_markdown(input_markdown, output_file=None, use_custom_css=True):
|
|
13
13
|
"""
|
|
14
14
|
Convenience function to convert Markdown content to a PDF with custom styling.
|
|
15
15
|
|
|
16
16
|
Args:
|
|
17
17
|
input_markdown: The Markdown content to convert.
|
|
18
|
+
output_file: Optional; if provided, the PDF will be saved to this file.
|
|
19
|
+
use_custom_css (bool, optional): Whether to use custom CSS for styling. Defaults to True.
|
|
20
|
+
|
|
18
21
|
Returns:
|
|
19
22
|
A byte string containing the generated PDF.
|
|
20
23
|
"""
|
|
21
24
|
generator = PDFGenerator()
|
|
22
|
-
return generator.get_customized_pdf_from_markdown(input_markdown)
|
|
25
|
+
return generator.get_customized_pdf_from_markdown(input_markdown, output_file, use_custom_css)
|
|
23
26
|
|
|
24
27
|
|
|
25
28
|
class PDFGenerator:
|
|
@@ -184,15 +187,20 @@ class PDFGenerator:
|
|
|
184
187
|
"""
|
|
185
188
|
return css_template
|
|
186
189
|
|
|
187
|
-
def get_customized_pdf_from_markdown(self, input_markdown, output_file=None):
|
|
190
|
+
def get_customized_pdf_from_markdown(self, input_markdown, output_file=None, use_custom_css=True):
|
|
188
191
|
"""
|
|
189
192
|
Convert Markdown content to a PDF with custom styling.
|
|
190
193
|
|
|
191
194
|
Args:
|
|
192
195
|
input_markdown: The Markdown content to convert.
|
|
193
196
|
output_file: Optional; if provided, the PDF will be saved to this file.
|
|
197
|
+
use_custom_css (bool, optional): Whether to use custom CSS for styling. Defaults to True.
|
|
198
|
+
|
|
194
199
|
Returns:
|
|
195
200
|
A byte string containing the generated PDF.
|
|
201
|
+
|
|
202
|
+
Raises:
|
|
203
|
+
Exception: If an error occurs during PDF generation.
|
|
196
204
|
"""
|
|
197
205
|
try:
|
|
198
206
|
html_content = markdown.markdown(input_markdown, extensions=['extra', 'codehilite', 'toc'])
|
|
@@ -200,11 +208,15 @@ class PDFGenerator:
|
|
|
200
208
|
# Generate PDF from HTML with Custom Styles
|
|
201
209
|
pdf_buffer = BytesIO()
|
|
202
210
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
211
|
+
if use_custom_css:
|
|
212
|
+
custom_css = self.generate_custom_css()
|
|
213
|
+
font_config = FontConfiguration()
|
|
214
|
+
html = HTML(string=html_content)
|
|
215
|
+
css = CSS(string=custom_css, font_config=font_config)
|
|
216
|
+
html.write_pdf(pdf_buffer, stylesheets=[css], font_config=font_config)
|
|
217
|
+
else:
|
|
218
|
+
html = HTML(string=html_content)
|
|
219
|
+
html.write_pdf(pdf_buffer)
|
|
208
220
|
|
|
209
221
|
pdf_value = pdf_buffer.getvalue()
|
|
210
222
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|