-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf.py
28 lines (24 loc) · 859 Bytes
/
pdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import pdfkit
from django.conf import settings
from django.http import HttpResponse
def render_to_pdf(html, pdf_out_file, wkhtmltopdf_options=None):
wkhtmltopdf_path = settings.WKHTMLTOPDF_PATH
options = {
'page-size': 'A4',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
}
if wkhtmltopdf_options:
options.update(wkhtmltopdf_options)
pdfkit.from_string(
html,
pdf_out_file,
configuration=pdfkit.configuration(wkhtmltopdf=wkhtmltopdf_path) if wkhtmltopdf_path else None,
options=options
)
def pdf_response(pdf_filename, contents):
response = HttpResponse(contents, content_type='application/pdf')
response['Content-Disposition'] = f'attachment; filename={pdf_filename}'
return response