使用 django 创建阿拉伯语报告

问题描述 投票:0回答:1

iam 尝试使用 django 创建报告,但由于某种原因阿拉伯语单词被编辑了 这是 pdf 的示例

这是我的查看功能 这个功能和模板在下载 pdf 时工作得很好,但是当它渲染阿拉伯语时它不起作用 我已经下载了这个阿拉伯字体 NotoKufiArabic-VariableFont_wght.ttf 但由于某种原因它无法工作

from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa
from io import BytesIO
from django.shortcuts import get_object_or_404
from ..models import Supplier, TransactionModel
from django.db.models import Sum

def generate_pdf_report(request, supplier_name):
    supplier = get_object_or_404(Supplier, supplier_name=supplier_name)
    transactions = TransactionModel.objects.filter(supplier_name=supplier).order_by('payment_date')
    
    total_paid = transactions.aggregate(Sum('payment_amount'))['payment_amount__sum'] or 0
    remaining_amount = supplier.total_amount - total_paid

    context = {
        'supplier': supplier,
        'transactions': transactions,
        'total_paid': total_paid,
        'remaining_amount': remaining_amount,
    }

    template = get_template('supplier_report.html')
    html = template.render(context)

    result = BytesIO()
    pdf = pisa.pisaDocument(BytesIO(html.encode("UTF-8")), result, encoding='UTF-8')
    
    

    if not pdf.err:
        response = HttpResponse(result.getvalue(), content_type='application/pdf')
        response['Content-Disposition'] = f'attachment; filename="{supplier_name}_report.pdf"'
        return response
    return HttpResponse('Error generating PDF', status=400)

这是我的模板

<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <title>تقرير المورد - {{ supplier.supplier_name }}</title>
    <style>
        @font-face {
            font-family: 'Arabic Font';
            src: url('../../NotoKufiArabic-VariableFont_wght.ttf') format('truetype');
        }
        body {
            font-family: 'ArialArabic', Arial, sans-serif;
            direction: rtl;
            text-align: right;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: right;
        }
        th {
            background-color: #f2f2f2;
        }
    </style>
</head>
<body>
    <h1>تقرير المورد - {{ supplier.supplier_name }}</h1>
    <p>نوع المورد: {{ supplier.get_supplier_type_display }}</p>
    <p>طريقة الدفع: {{ supplier.get_payment_method_display }}</p>
    <p>المبلغ الكلي: {{ supplier.total_amount }}</p>
    <p>المبلغ المدفوع: {{ total_paid }}</p>
    <p>المبلغ المتبقي: {{ remaining_amount }}</p>
    <p>تاريخ الدفعة القادمة: {{ supplier.next_payment_date }}</p>
    <p>اسم المصدر: {{ supplier.issuer_name }}</p>
    <p>البريد الإلكتروني: {{ supplier.email }}</p>
    
    <h2>جدول المعاملات</h2>
    <table>
        <thead>
            <tr>
                <th>رقم الدفعة</th>
                <th>تاريخ الدفع</th>
                <th>قيمة الدفعة</th>
            </tr>
        </thead>
        <tbody>
            {% for transaction in transactions %}
            <tr>
                <td>{{ transaction.payment_num }}</td>
                <td>{{ transaction.payment_date }}</td>
                <td>{{ transaction.payment_amount }}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</body>
</html>```
python html django
1个回答
0
投票

这里有一个小错字

from ..models import Supplier, TransactionModel

你在这里输入“..” 更新代码:

from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa
from io import BytesIO
from django.shortcuts import get_object_or_404
from .models import Supplier, TransactionModel
from django.db.models import Sum

def generate_pdf_report(request, supplier_name):
    supplier = get_object_or_404(Supplier, supplier_name=supplier_name)
    transactions = TransactionModel.objects.filter(supplier_name=supplier).order_by('payment_date')
    
    total_paid = transactions.aggregate(Sum('payment_amount'))['payment_amount__sum'] or 0
    remaining_amount = supplier.total_amount - total_paid

    context = {
        'supplier': supplier,
        'transactions': transactions,
        'total_paid': total_paid,
        'remaining_amount': remaining_amount,
    }

    template = get_template('supplier_report.html')
    html = template.render(context)

    result = BytesIO()
    pdf = pisa.pisaDocument(BytesIO(html.encode("UTF-8")), result, encoding='UTF-8')
    
    

    if not pdf.err:
        response = HttpResponse(result.getvalue(), content_type='application/pdf')
        response['Content-Disposition'] = f'attachment; filename="{supplier_name}_report.pdf"'
        return response
    return HttpResponse('Error generating PDF', status=400)
© www.soinside.com 2019 - 2024. All rights reserved.