Skip to content

Commit

Permalink
Code fixes and hacks for using wkhtmltopdf 0.12.6 (Github release, wi…
Browse files Browse the repository at this point in the history
…th patched qt, .deb for Ubuntu 20.04)
  • Loading branch information
nathanchrs committed Sep 19, 2020
1 parent 8d3af42 commit d0f65ae
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 31 deletions.
6 changes: 4 additions & 2 deletions Translation/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@
USE_X_FORWARDED_HOST = True

WKHTMLTOPDF_CMD_OPTIONS = {
'enable-local-file-access': '',
'encoding': 'utf-8',
'page-size': 'A4',
'margin-left': '0.75in',
'margin-right': '0.75in',
'margin-top': '0.62in',
'margin-bottom': '1in',
'print-media-type': '',
'no-stop-slow-scripts': '',
'javascript-delay': '30000', # max wait until rendering finishes
'javascript-delay': '5000', # max wait until rendering finishes
}


Expand Down Expand Up @@ -201,4 +203,4 @@
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
HOST_URL = 'http://127.0.0.1:9000/'

CUSTOM_PRINT_ENABLED = False
CUSTOM_PRINT_ENABLED = False
17 changes: 11 additions & 6 deletions trans/static/css/pdf.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
body {
margin: 3em 5em;
zoom: .9;
text-align: justify;
text-align: start;
background-color: rgb(82, 86, 89);
}

Expand All @@ -25,7 +25,7 @@ div.pdf {
}

.header {
font-family: IRANSans, Helvetica, Arial, sans-serif;
font-family: Arial, sans-serif;
font-size: 14px;
letter-spacing: -.04em;
font-weight: 400;
Expand All @@ -51,19 +51,21 @@ div.pdf {
margin-top: -2px;
}

.header .logo img {
.header .logo .logo-image {
display: inline-block;
height: 70px;
margin-top: 0;
width: 256px;
padding-right: 15px;
}

.header .meta {
display: inline-block;
float: right;
text-align: right;
margin-top: 14px; /* ioi-2019 */
font-weight: bold; /* ioi-2019 */
margin-top: 6px;
font-weight: bold; /* ioi-2019 */
font-size: 12px;
white-space: nowrap;
}

.header .meta div:first-child {
Expand Down Expand Up @@ -117,4 +119,7 @@ div.pdf {
margin-top: 1px;
}

.katex {
margin-left: 0.2em;
}
}
39 changes: 24 additions & 15 deletions trans/static/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ Global
============================= */

@import url(../fonts/SourceSansPro/SourceSansPro.css);
@import url(../fonts/IRANSans/IRANSans.css);

/*@import url(https://fonts.googleapis.com/css?family=Source+Sans+Pro:200,300,400,600);*/


body {
font-family: Arial, serif;
font-family: Arial, sans-serif;
color: #666;
font-size: 16px;
font-weight: 400;
-webkit-font-smoothing: antialiased;
font-smoothing: antialiased;
font-smooth: antialiased;
}

p {
Expand All @@ -30,7 +29,7 @@ a:hover {
}

.container h1, .container h2, .container h3, .container h4 {
font-family: 'Source Sans Pro', arial, sans-serif;
font-family: Arial, sans-serif;
}

.container h1 {
Expand Down Expand Up @@ -81,7 +80,7 @@ a:hover {
}

.table th {
font-family: "Source Sans Pro", arial, sans-serif;
font-family: Arial, sans-serif;
font-size: 1.2em;
font-weight: 400;
}
Expand Down Expand Up @@ -210,7 +209,7 @@ div.CodeMirror {
}

#left_rtl_plain_text_box .editor-content {
font-family: IRANSans, Tahoma, Arial;
font-family: Arial, sans-serif;
font-size: 90%;
color: #333;
}
Expand Down Expand Up @@ -242,11 +241,13 @@ code, pre {
/* Default text fonts */

.rendered_content {
font-family: CustomTextFont, Arial, serif;
font-family: "CustomTextFont", Arial, "Noto Sans", sans-serif;
font-weight: normal;
}

.rendered_content[dir="rtl"] {
font-family: CustomTextFont, IRANSans, Tahoma, Arial;
font-family: "CustomTextFont", Arial, "Noto Sans", sans-serif;
font-weight: normal;
}

.rendered_content[dir="rtl"] th {
Expand All @@ -255,26 +256,34 @@ code, pre {

/* Default text fonts per language */

.rendered_content[lang="ar"] {
font-family: "CustomTextFont", Arial, "Noto Naskh Arabic", "Noto Sans", serif;
}

.rendered_content[lang="fa"] {
font-family: "CustomTextFont", Arial, "Noto Sans", serif;
}

.rendered_content[lang="ja"] {
font-family: CustomTextFont, 'TakaoPGothic', Arial, serif;
font-family: "CustomTextFont", 'TakaoPGothic', Arial, "Noto Sans", "Noto Sans CJK JP", serif;
}

.rendered_content[lang="kr"] {
font-family: CustomTextFont, 'NanumMyeongjo', Arial, serif;
font-family: "CustomTextFont", 'NanumMyeongjo', Arial, "Noto Sans", "Noto Sans CJK KR", serif;
}

.rendered_content[lang="th"] {
font-family: CustomTextFont, 'Loma', Arial, serif;
font-family: "CustomTextFont", 'Loma', Arial, "Noto Sans", serif;
}

.rendered_content[lang="zh-Hans"] {
font-family: CustomTextFont, 'Source Han Serif CN', Arial, serif;
font-family: "CustomTextFont", 'Source Han Serif CN', Arial, "Noto Sans", "Noto Sans CJK SC", serif;
}

.rendered_content[lang="zh"] {
font-family: CustomTextFont, 'PMingLiU', Arial, serif;
font-family: "CustomTextFont", 'PMingLiU', Arial, "Noto Sans", "Noto Sans CJK", serif;
}

.rendered_content[lang="si"] {
font-family: CustomTextFont, 'NotoSansSinhala-Regular', Arial, serif;
}
font-family: "CustomTextFont", Arial, 'NotoSansSinhala-Regular', "Noto Sans", serif;
}
3 changes: 2 additions & 1 deletion trans/templates/pdf-template.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<style>
@font-face {
font-family: 'CustomTextFont';
font-weight: normal;
src: url(data:font/truetype;charset=utf-8;base64,{{ text_font_base64 }})
}
</style>
Expand All @@ -32,7 +33,7 @@
<div class="markdown-body pdf">
<div class="header">
<div class="logo">
<img src="{{ static_path }}/img/logo_text.svg" />
<span class="logo-image" style="background:url('{{ static_path }}/img/logo_text.svg') no-repeat;"></span>
</div>
<!--div class="title">
<div>{{settings.CONTEST_FULL_TITLE}}</div>
Expand Down
13 changes: 6 additions & 7 deletions trans/utils/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import pdfkit
from shutil import copyfile
from xvfbwrapper import Xvfb

from trans.context_processors import ioi_settings

Expand Down Expand Up @@ -69,17 +68,18 @@ def build_pdf(translation, task_type):

last_edit_time = translation.get_latest_change_time()
rebuild_needed = not os.path.exists(pdf_file_path) or os.path.getmtime(pdf_file_path) < last_edit_time
if not rebuild_needed:
return pdf_file_path
#if not rebuild_needed:
# return pdf_file_path # DEBUG !!!!!!!!!!!!!!!!!!!!!!!

html = render_pdf_template(
translation, task_type,
static_path=settings.STATIC_ROOT,
images_path=settings.MEDIA_ROOT + 'images/',
pdf_output=True,
pdf_output=False,
)
convert_html_to_pdf(html, pdf_file_path)
add_page_numbers_to_pdf(pdf_file_path, task.name)

return pdf_file_path


Expand All @@ -105,8 +105,7 @@ def convert_html_to_pdf(html, pdf_file_path):
html_file_path = '/tmp/{}.html'.format(str(uuid4()))
with open(html_file_path, 'wb') as f:
f.write(html.encode('utf-8'))
with Xvfb():
pdfkit.from_file(html_file_path, pdf_file_path, options=settings.WKHTMLTOPDF_CMD_OPTIONS)
pdfkit.from_file(html_file_path, pdf_file_path, options=settings.WKHTMLTOPDF_CMD_OPTIONS)
os.remove(html_file_path)
except Exception as e:
logger.error(e)
Expand Down Expand Up @@ -177,4 +176,4 @@ def merge_final_pdfs(task_names, username):
cmd = cmd + '-o {}'.format(output_pdf_path)

os.system(cmd)
return output_pdf_path
return output_pdf_path

0 comments on commit d0f65ae

Please sign in to comment.