-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/microsoft/intelligence-toolkit
- Loading branch information
Showing
8 changed files
with
91 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import tempfile | ||
import markdown2 | ||
import pdfkit | ||
from util.wkhtmltopdf import config_pdfkit, pdfkit_options | ||
import streamlit as st | ||
|
||
css = '''body { | ||
font-family: 'helvetica'; | ||
} | ||
''' | ||
#itk-label | ||
text_label = 'Report generated using Intelligence Toolkit (https://aka.ms/itk)' | ||
|
||
def add_download_pdf(name, text, button_text='Download PDF', is_markdown=True, disabled=False): | ||
if not name.endswith('.pdf'): | ||
name += '.pdf' | ||
text = f'<style>{css}</style>{text}<hr>{text_label}' | ||
# Convert text to HTML if it's in Markdown format | ||
text = markdown2.markdown(text) if is_markdown else text | ||
|
||
# Generate PDF from HTML string | ||
config_pdf = config_pdfkit() | ||
with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as temp_file: | ||
pdfkit.from_string(text, temp_file.name, options=pdfkit_options, configuration=config_pdf) | ||
|
||
# Provide download button for the generated PDF | ||
with open(temp_file.name, 'rb') as f: | ||
st.download_button(button_text, f, file_name=name, mime='application/pdf', disabled=disabled) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import os | ||
import pdfkit | ||
|
||
# Specify the name of the executable | ||
executable = 'wkhtmltopdf' | ||
|
||
# Check if the executable is in the system PATH | ||
def is_in_path(executable): | ||
for path in os.environ["PATH"].split(os.pathsep): | ||
if os.path.exists(os.path.join(path.strip('"'), executable)): | ||
return True | ||
return False | ||
|
||
def config_pdfkit(): | ||
path_wkhtmltopdf = 'C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe' | ||
# Verify if wkhtmltopdf is in PATH | ||
if is_in_path(executable): | ||
path_wkhtmltopdf='' | ||
else: | ||
path_wkhtmltopdf = 'C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe' | ||
|
||
return pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf) | ||
|
||
pdfkit_options = { | ||
'encoding': 'UTF-8', | ||
'enable-local-file-access': True, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.