如何在xhtml2pdf中将阿拉伯字符导出为pdf?

我想使用 xhtml2pdf 将阿拉伯字符导出为 pdf,因为我在 django 应用程序中使用它。


我知道他们做了更新,说他们修复了它,但它对我不起作用,我使用了自定义字体但仍然不起作用。


因此,请任何人知道正确的加密方式或与之相关的任何方式,帮助我。


发票.html:


{% load static %}


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

    

    <meta http-equiv="content-type" content="text/html; charset=utf-8">

<head>

    <title>{{title}}</title>

<style type="text/css">

        @font-face {

        font-family: Amiri, "Amiri Font";

        src: url("{% static 'font/Amiri-Italic.ttf'%}");

        }

        body {

            font-weight: 200;

            font-size: 14px;

        }


    </style>

</head>


<body><pdf:language name="arabic"/>

    <div class='wrapper'>

        <div class='header'>

            <p class='title'>Invoice # {{ invoice_ID }} </p>

        </div>

        <div>

            <div class='details'>


                <pdf:language name="arabic"/>     

                <p> تجربة</p>           

                customername : {{customer_name}} <br><hr>

                <hr class='hrItem' />

            </div>

        </div>

</body>


</html>

utils.py :


from io import BytesIO

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("UTF-8")), result)

    if not pdf.err:

        return HttpResponse(result.getvalue(), content_type='application/pdf')

    return None

views.py :


from django.shortcuts import render, redirect, HttpResponse

import pandas as pd

from .models import SaleInvoice, TransferWays, User, InstallingCities

from siteManagment.models import MarketersManagment, WareHouses

from .utils import render_to_pdf

from django.core.validators import ValidationError


MMTTMM
浏览 101回答 1
1回答

慕标琳琳

我解决了它,文档实际上没有任何问题,问题出在静态中..因为当您使用 {% static 'whatever' %} 时,它将搜索静态文件,但由于某种原因它不起作用,我认为问题来自 django 本身,也许你无法从 pdf 文件中调用静态。这是我的新发票.html(我已将字体的整个路径放在其中):{% load static %}<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <meta http-equiv="content-type" content="text/html; charset=utf-8">&nbsp; &nbsp; <head>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <title>{{title}}</title>&nbsp; &nbsp; <style type="text/css">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; .header {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font-size: 20px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font-weight: 100;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text-align: center;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: #007cae;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; .title {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font-size: 22px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font-weight: 100;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* text-align: right;*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; padding: 10px 20px 0px 20px;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; .title span {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: #007cae;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; .details {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; padding: 10px 20px 0px 20px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text-align: right !important;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*margin-left: 40%;*/&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; .hrItem {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; border: none;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height: 1px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* Set the hr color */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: #333;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* old IE */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background-color: #fff;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* Modern Browsers */&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @font-face {font-family: RTLFont; src: url('C:\\Users\\BigBoy\\Desktop\\MyProjects\\ToysSite\\static\\font\\Amiri-Italic.ttf')} // Here&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; body {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font-weight: 200;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font-size: 14px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font-family: RTLFont;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; /* @page {&nbsp; &nbsp; &nbsp; &nbsp; background-image: url('C:\Users\BigBoy\Desktop\MyProjects\ToysSite\static\background.png');&nbsp; &nbsp; &nbsp; &nbsp; } */&nbsp; &nbsp; </style></head><body>&nbsp; &nbsp; <div class='wrapper'>&nbsp; &nbsp; &nbsp; &nbsp; <div class='header'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class='details'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <pdf:language name="arabic"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; اسم العميل : {{customer_name}} <br><hr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <hr class='hrItem' />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP