-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender_to_pdf.py
122 lines (100 loc) · 2.94 KB
/
render_to_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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# HTML File
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Mushak - 6.4</title>
<style media="all">
body {
font-weight: 100;
font-size: 9.3px;
}
.hrItem {
border: none;
height: 1px;
/* Set the hr color */
color: #333; /* old IE */
background-color: #fff; /* Modern Browsers */
}
{# Table Header and Footer Fixed #}
table {
width: 100%
}
thead tr,
tfoot tr {
position: absolute;
left: 0;
right: 15px;
/* to not cover the scrollbar*/
background: red
}
thead th,
tfoot td {
display: inline-block;
}
thead tr {
top: 0
}
tfoot tr {
top: 500px /* same value has max-height from div */
}
</style>
</head>
<body>
<header>
<table>
<tr style="border: none;">
<td style="width: 20%;border: none;">
<img width="100px" height="100px" src="http://127.0.0.1:8000/folder/media/government_image.png">
</td>
<td style="text-align: center; width: 60%;border: none;">
<p>Test
<p>
<p>Test</p>
<p>Test
<p>
<p>Test</p>
</td>
<td style="width: 20%;text-align: center;">
Test
</td>
</tr>
</table>
</header>
<script type="text/css">
</script>
</body>
</html>
# views.py
def get(self, request, *args, **kwargs):
tempate = get_template('demo/render_to_pdf.html')
context = {
'order_id': 12345,
'img_url': 'http://127.0.0.1:8000/folder/media/government_image.png',
'amount': 39.99,
'registered_person': 'Hasan Mahmud',
'registered_person_bin_no': '1234567895789',
'registered_person_address': 'Khilgaon, Dhaka, Bangladesh',
'bill_no': 1233434,
'diclist': {
'id': [1, 2, 3],
},
'range': range(1, 5)
}
pdf = render_to_pdf('demo/render_to_pdf.html', context)
return HttpResponse(pdf, content_type='application/pdf')
# utils.py
from io import BytesIO, StringIO
from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa
def render_to_pdf(template_src, context_dict={}):
template = get_template(template_src)
html = template.render(context_dict)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf')
return None
# URL
path('render_to_pdf', render_to_pdf.as_view(), name="render_to_pdf"),