Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###################### views.py
- from django.shortcuts import render
- # import cStringIO as StringIO
- from io import BytesIO
- # from typing import ContextManager
- from django.http import HttpResponse
- from django.template.loader import get_template
- from xhtml2pdf import pisa
- # from bidi import algorithm as bidialg
- # import win_unicode_console
- # win_unicode_console.enable()
- # from pyarabic import araby
- # import pyarabic.araby as araby #, normalize_ligature
- # import pyarabic.number as number
- # from pyarabic.araby import strip_diacritics, normalize_ligature
- def render_to_pdf(template_src, context_dict={}):
- template = get_template(template_src)
- html = template.render(context_dict)
- result = BytesIO()
- ''' important notes about rendring html to pdf via 'pisa' next three lines are working good to fix the problem
- of Arabic language except the letters are separated
- '''
- # pdf = pisa.CreatePDF(bidialg.get_display(html, base_dir="L"), result, encoding='iso-8859-6') # CreatePDF = pisaDocument
- # pdf = pisa.pisaDocument(BytesIO(bidialg.get_display(html.encode('utf-8'), base_dir='L')), result)
- # pdf = pisa.pisaDocument(bidialg.get_display(html.encode('UTF-8')), result)
- pdf = pisa.CreatePDF(BytesIO(html.encode("UTF-8")), result)
- #
- # pdf = pisa.pisaDocument(BytesIO(html.encode("iso-8859-6")), result)
- # pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
- # print(type(html.encode('utf8')))#(var.encode('utf8'))
- # print((bytes(b,'utf-8')))
- # https://stackoverflow.com/questions/60139294/rendering-pdf-template-in-django-in-arabic-language
- # pdf = pisa.pisaDocument(BytesIO(html.encode('cp1252')), result)
- if not pdf.err:
- return HttpResponse(result.getvalue(), content_type='application/pdf')
- return None
- #This is method based to render to pdf
- def get_pdf(request, visit_id, *args, **kwargs):
- vis_id = Visits.objects.get(id=visit_id)
- qs = Medicine.objects.filter(visit=visit_id).order_by('-id')
- plan = Medicine.objects.values('plan').filter(visit=visit_id).first()
- plan1 = Medicine.objects.filter(plan=plan['plan'])
- vdate = Visits.objects.values('visitdate').filter(id=visit_id).first()
- visitdate = vdate['visitdate']
- patname = Medicine.objects.values('patient').filter(visit=visit_id).first()
- patient = Patients.objects.get(id=patname['patient'])#patname['patient']
- print(patient , plan1)
- query = Medicine.objects.all().order_by('-id')
- # print()
- # zero_remain = LaundryBill.objects.filter(sumtotal__gt=0, returns=False, remain=0).order_by('-id')
- template = get_template('clinic/pdf.html')
- context = {
- 'qs': qs,
- "query":query,
- "visit_no": vis_id,
- "name": patient,
- "date": visitdate,
- "today": "Today",
- }
- html = template.render(context)
- pdf = render_to_pdf('clinic/pdf.html', context)
- if pdf:
- response = HttpResponse(pdf, content_type='application/pdf')
- filename = "Prescription_"+str(time.strftime('%d-%m-%Y'))+".pdf" #%('%Y-%m-%d')
- content = "inline; filename=%s" %(filename)
- download = request.GET.get("download")
- if download:
- content = "attachment; filename='%s'" %(filename)
- response['Content-Disposition'] = content
- return response
- return HttpResponse(pdf, content_type='application/pdf')
- ###################################### pdf.html
- <!-- # -*- coding: utf-8 -*- -->
- <!-- -->
- {% load render_table from django_tables2 %} {% load querystring from django_tables2 %} {% load static %}
- <!-- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> -->
- <!DOCTYPE html>
- <html dir="auto" lang="ar">
- <!-- <html xmlns="" xml:lang="ar" lang="ar" dir="rtl"> -->
- <!-- dir="rtl" http://www.w3.org/1999/xhtml -->
- <head>
- <title>عرض الملفات</title>
- <meta charset="UTF-8">
- <!-- <meta name="viewport" content="width=device-width, initial-scale=1.0"> -->
- <!-- <meta http-equiv="content-type" content="text/html; charset=windows-1256"> -->
- <!-- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-6"> -->
- <meta http-equiv=Content-Type content="text/html;charset=utf-8">
- <meta http-equiv="content-type" content="text/html" charset="utf-8">
- <meta http-equiv="content-language" content="ar">
- <link rel="" href="{% static 'fonts/Raleway.css' %}">
- <link rel="stylesheet" href="chrome://resources/css/text_defaults_md.css">
- <style type="text/css">
- @font-face {
- font-family: 'TimesNewRoman', 'Lucida Grande', Verdana, Helvetica, Arial, sans-serif, Courier, 'serif', Tahoma, "DejaVu Sans";
- /* src: url("D:/Django_Project/Projects/gitlab_django_clinic/myproject/static/fonts/RobotoRegular.ttf"); */
- src: url("D:/Django_Project/Projects/gitlab_django_clinic/myproject/static/fonts/DejaVuSans.ttf");
- /* src: url("D:/Django_Project/Projects/gitlab_django_clinic/myproject/static/fonts/Amiri-Bold.ttf"); */
- /* src: url("D:/Django_Project/Projects/gitlab_django_clinic/myproject/static/fonts/roboto-regular.woff2") format('woff'); */
- /* font-family: 'mywebfont'; */
- /* src: url('adobe_regular.eot');
- src: url('adobe_regular.eot?#iefix') format('embedded-opentype'), url('adobe_regular.woff') format('woff'), url('adobe_regular.ttf') format('truetype'), url('adobe_regular.svg#adobe_regular') format('svg'); */
- /* font-weight: normal;
- font-style: normal; */
- }
- /* html {
- font-family: 'Droid Arabic Kufi', serif, Tahoma, "DejaVu Sans";
- font-size: 100%;
- } */
- body {
- font-family: 'TimesNewRoman', 'Lucida Grande', Verdana, Helvetica, Arial, sans-serif, Courier, 'serif', Tahoma, "DejaVu Sans";
- font-weight: 200;
- font-size: 14px;
- text-align: right;
- /* unicode-bidi: bidi-override !important; */
- }
- .header {
- font-size: 20px;
- font-weight: 100;
- text-align: center;
- color: #007cae;
- }
- .title {
- font-size: 22px;
- font-weight: 100;
- /* text-align: right;*/
- padding: 10px 20px 0px 20px;
- }
- .title span {
- color: #007cae;
- }
- .details {
- padding: 10px 20px 0px 20px;
- text-align: left !important;
- /* text-align: left !important; */
- /*margin-left: 40%;*/
- }
- .hrItem {
- border: none;
- height: 1px;
- /* Set the hr color */
- color: #333;
- /* old IE */
- background-color: #fff;
- /* Modern Browsers */
- }
- </style>
- <button>Print Me</button>
- </head>
- <body>
- <button>Print Me</button>
- <!-- <div dir="auto" style="text-align:left">اى حاجة عشان اعرف</div> -->
- <div class='wrapper' dir="rtl" lang="ar">
- <div class='header' dir="auto">
- <p class='title' dir="auto">الروشتة</p>
- </div>
- <!-- {% comment %}
- <div> {% endcomment %} -->
- <div class='details' dir="auto">
- <hr class='hrItem' /> {% for format in table.export_formats %}
- <a href="{% querystring '_export'=format %}">
- download <code>.{{ format }}</code>
- </a> {% endfor %}
- <!-- -->
- Visit No: {{ visit_no }}<br /> Name: {{ name }}<br /> Visit Date: {{ date }}
- <!-- {% now "Y-m-d" %} -->
- <!-- {% comment %} {% querystring '_export'=format %} {% endcomment %} {% comment %} {{billtable}} {% endcomment %} -->
- <table class="fl-table" id="table">
- <!-- class="fl-table" -->
- <thead>
- <!-- <tr>
- <th>Drug</th>
- <th>Plan</th>
- </tr> -->
- </thead>
- {% for cl in qs %}
- <tbody>
- <tr dir="auto" style="text-align: center;">
- <!-- <td>{{cl.id}}</td> -->
- <!-- <td>{{cl.recieveDate}}</td> -->
- <td>{{cl.name}}</td>
- <td>{{cl.plan}}</td>
- </tr>
- </tbody>
- {% endfor %}
- </table>
- </div>
- </div>
- </body>
- </html>
Add Comment
Please, Sign In to add comment