-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from rleyton/pdf-results
Pdf results
- Loading branch information
Showing
34 changed files
with
720 additions
and
473 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
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,79 @@ | ||
import pdfkit | ||
import tempfile | ||
from pypdf import PdfMerger | ||
|
||
from .utils_config import RESULTS_DIR | ||
from .utils_consts import PDF_DIR | ||
|
||
|
||
def get_options(header): | ||
return { | ||
"page-size": "A4", | ||
"margin-top": "0.9in", | ||
"margin-right": "0.9in", | ||
"margin-bottom": "0.9in", | ||
"margin-left": "0.9in", | ||
"encoding": "UTF-8", | ||
"header-center": f"{header}", | ||
"custom-header": [("Accept-Encoding", "gzip")], | ||
"no-outline": None, | ||
} | ||
|
||
|
||
def do_html_to_pdf(file, html, summary): | ||
if type(file) is str: | ||
filename = file | ||
else: | ||
filename = file.name | ||
|
||
pdfkit.from_file(html, filename, options=get_options(header=summary)) | ||
|
||
|
||
def generate_pdf(competition, gender, resultshtml, teamhtml, summary=None): | ||
output_file = RESULTS_DIR + PDF_DIR + f"/{competition}-{gender}.pdf" | ||
result_file = tempfile.NamedTemporaryFile( | ||
suffix=".pdf", | ||
) | ||
team_file = tempfile.NamedTemporaryFile( | ||
suffix=".pdf", | ||
) | ||
|
||
do_html_to_pdf( | ||
file=result_file, | ||
html=resultshtml, | ||
summary=f"{competition} {gender} - race results", | ||
) | ||
do_html_to_pdf( | ||
file=team_file, html=teamhtml, summary=f"{competition} {gender} - team results" | ||
) | ||
|
||
merger = PdfMerger() | ||
for file in [result_file, team_file]: | ||
merger.append(file.name) | ||
|
||
merger.write(output_file) | ||
merger.close() | ||
|
||
return output_file | ||
|
||
|
||
def generate_single_pdf(filename, html, summary=None): | ||
output_file = RESULTS_DIR + PDF_DIR + "/" + filename + ".pdf" | ||
|
||
do_html_to_pdf(file=output_file, html=html, summary=summary) | ||
|
||
return output_file | ||
|
||
|
||
def combined_pdf(pdf_list, summary, target): | ||
merger = PdfMerger() | ||
summary_file = tempfile.NamedTemporaryFile(suffix=".pdf") | ||
|
||
if summary is not None: | ||
pdfkit.from_string(input=summary, output_path=summary_file.name) | ||
merger.append(summary_file.name) | ||
for pdf in pdf_list: | ||
merger.append(pdf) | ||
|
||
merger.write(target) | ||
merger.close() |
Oops, something went wrong.