我正在使用 Django Web 应用程序中的 weasyprint 将 HTML 转换为 PDF 文件。一切正常,但将 HTML 转换为 PDF 时运行速度非常慢。当我转换它时,它需要两分钟多的时间。我在我的 HTML 文件中使用 Bangla 字体。
这是我的views.py代码
def get_pdf_file(request, customer_id, sys_type):
sys_type = customer_id
area = "pdf"
site_credit = site_credit1
time_now = timezone.now()
customers = get_object_or_404(CustomerInfo, pk=customer_id)
due_taka_track = customers.duetaka_set.all()
if due_taka_track == None:
due_taka_track = 0
unpaid_taka = int(customers.customer_price -
customers.customer_due_taka_info)
due_taka_track = customers.duetaka_set.all()
sum_cost_taka = due_taka_track.aggregate(
sp=Sum('customer_due')).get('sp', 0)
if sum_cost_taka == None:
sum_cost_taka = 0
total_paid_taka = sum_cost_taka + customers.customer_due_taka_info
payment_status = 'complete'
payment_message = 'Full Paid'
remain_taka='PAID'
remain_msg=''
if customers.customer_due_taka_info < customers.customer_price:
payment_status = 'incomplete'
payment_message = 'সম্পূর্ন টাকা পরিষোধ করা হয়নি'
remain_msg='টাকা বাকী আছে'
baki_ase="পাওনা আছে "
remain_taka = customers.customer_price - customers.customer_due_taka_info
context = {'customers': customers,
'sys_type': sys_type,
'area': area,
'site_credit': site_credit,
'site_name': 'Moon Telecom',
'sys_type': sys_type,
'due_taka_track': due_taka_track,
'total_paid_taka': total_paid_taka,
'payment_message': payment_message,
'time_now': time_now,
'unpaid_taka': unpaid_taka,
'payment_message': payment_message,
'remain_taka': remain_taka,
'sum_cost_taka': sum_cost_taka,
'remain_msg': remain_msg}
html_string = render_to_string('shop/pdf_invoice.html', context)
html = HTML(string=html_string)
result = html.write_pdf()
response = HttpResponse(content_type='application/pdf;')
response['Content-Disposition'] = 'inline; filename=invoice'+sys_type+'.pdf'
response['Content-Transfer-Encoding'] = 'UTF-8'
with tempfile.NamedTemporaryFile(delete=True) as output:
output.write(result)
output.flush()
output = open(output.name, 'rb')
response.write(output.read())
return response
这是我的 HTML 文件。这个文件我正在将 HTML 转换为 PDF
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Moon Telecom</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://fonts.maateen.me/kalpurush/font.css" rel="stylesheet">
<style>
p,
td,
tr {
font-size: 10px;
line-height: 12px;
}
body {
font-family: 'Kalpurush', Arial, sans-serif !important;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="site_title" style="text-align:center; margin:0 auto">
<p style="font-size:15px">মুন টেলিকম</p>
<div class="invoice_info_one" style="width:100%; text-align:center">
<p>সকল প্রকার মোবাইল সেট, সীম কার্ড, মেমোরী কার্ড, MP-3, সোলার চার্জার, সোলার ফ্যান, মোবাইল ফোনের ব্যাটারী,
চার্জার, ক্যাচিং,কাভার,হেডফোন, রেবন, ডিসপ্লে এবং ইলেট্রিক মালামাল বিক্রেতা</p>
</div>
<div class="invoice_location">
<p>জাহাঙ্গীর সুপার মার্কেট, ব্রীজ রোড, ফুলহাতা বাজার, মোডেলগঞ্জ।</p>
</div>
<div class="invoice_contact">
<p>01717-051200(সুকান্ত) 01828-163858(দোকান)</p>
<p>
<b>Email:</b> moontelecom2008@gmail.com</p>
</div>
<hr>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="customer_info">
{% if customers.customer_name %}
<p style="font-size:15px">Customer Name:
<b> {{customers.customer_name}}</b>
<span style="color:red">{{customers.customer_first_due_info}}</span>
</p>
{% else %}
<p> Name: দেওয়া হয়নি</p> {% endif %}
<p style="font-size:14px">Phone Number: {% if customers.customer_mobile_no %} {{customers.customer_mobile_no}} {% else %} No Mobile
Number {% endif %}
</p>
<p style="font-size:14px">Purchase Time: {{customers.customer_sell_date}}</p>
<p style="font-size:14px">invoice id:
<b>{{customers.customer_uid}}</b>
</p>
<p>{{customers.product_warrenty}}</p>
</div>
</div>
</div>
</div>
<div class="customer_product_and_paid_info">
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="">
<table class="table table-bordered">
<thead>
<tr>
<th>Product Name</th>
<th colspan="">Price</th>
<th>ID or IME</th>
<th>Warrenty</th>
<th>QN</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<b>{{customers.customer_product_name}}</b>
</td>
<td>
<b>{{customers.customer_price}}</b> TK
<i>{% if customers.customer_discount_taka %} Discount added TK: {{customers.customer_discount_taka}}
TK {% else %} {% endif %}</i>
</td>
<td>{{customers.customer_product_id}}</td>
<td>
{% if customers.customer_product_warrenty %} {{customers.customer_product_warrenty}} Months {% else %} No {% endif %}
</td>
<td>{{customers.customer_product_quantity}}</td>
</tr>
<th>
<hr> First Time Payment</th>
<th>
<hr> {{customers.customer_first_time_payment}} TK
<span style="color:red">{{customers.customer_first_due_info}}</span>
</th>
<td>
<hr> {{customers.customer_sell_date}}
</td>
{% if due_taka_track %}
<thead>
<tr>
<th>SL</th>
<th>Taka</th>
<th>Paid Date</th>
<td>Due Info</td>
</tr>
</thead>
<tbody>
<hr> {% for track in due_taka_track %}
<tr>
<td>{{forloop.counter}}</td>
<td>
<i>{{track.customer_due}}</i> TK</td>
<td>{{track.customer_due_date}}</td>
<td>{{track.customer_due_info}}</td>
</tr>
{% endfor %}
</tbody>
{% else %} {% endif %} {% if sum_cost_taka %}
<tr>
<td>Total Due Complete </td>
<th>{{sum_cost_taka}} TK</th>
</tr>
{% else %} {% endif %}
<tr>
<td>
<b>Total Paid</b>
</td>
<th>
{% if payment_message %}
{{customers.customer_due_taka_info}} TK
<span style="color:red">{{payment_message}}</span>
<br>
<button type="button" class="btn btn-danger">{{remain_msg}} {{remain_taka}} TK</button>
{%else %}
{{customers.customer_due_taka_info}} TK
<button type="button" class="btn btn-success">Payment Completed</button>
{% endif %}
</th>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="customer_notifications">
<div class="container">
<div class="row">
<div class="col-lg-6">
{% if customers.customer_conditions %}
<div class="warning">
<p style="font-size:8px; width:500px;line-height:15px;">
<i>{{customers.customer_product_name}} {{customers.customer_conditions}}</i>
</p>
</div>
{% else %} {% endif %}
</div>
</div>
</div>
</div>
<div class="footer" style="display:block">
<div class="footer_info">
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="footer_info">
<p>Print date: {{time_now}}</p>
<p>
<b>বিঃদ্রঃ ডেলিভারির সময় মাল বুঝিয়া নিবেন। পরে কোন আপত্তী গ্রহনযোগ্য নয়</b>
</p>
<p>বিক্রিত মাল ফেরত নেওয়া হয় না</p>
</div>
<div class="copy_right">
{% if site_credit %}
<p>{{site_credit}}</p>
{% else %}
{% endif %}
</div>
</div>
</div>
</div>
</div>
</div>
</body>