اذهب إلى المحتوى

السؤال

نشر

سلام عليكم

عندما استخدم مكتبة تحويل من HTML الى PDF  بلغة بايثون وضمن الخط العربي يظهر الخط العربي: 

image.thumb.png.190fb56e0f0bf4121078aaf7a75d41c9.png

<!DOCTYPE html>
<html lang="ar">
<head>
    <meta charset="utf-8">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Arabic:wght@100..900&display=swap" rel="stylesheet">
    <style>

        .font {
            font-family: "Noto Sans Arabic", sans-serif;
            direction: rtl;
            text-align: right;
            font-size: 18px;
        }
    </style>
</head>
<body>
<h1 class="font">عربي </h1>
<h1>english</h1>
</body>
</html>
@app.route("/pdf")
def pdf():
    html = render_template("template.html")
    result = io.BytesIO()

    pisa_status = pisa.CreatePDF(
        html,
        dest=result,
        encoding='utf-8',
        link_callback=link_callback
    )

    if pisa_status.err:
        return "PDF generation failed", 500

    result.seek(0)
    response = make_response(result.getvalue())
    response.headers["Content-Type"] = "application/pdf"
    response.headers["Content-Disposition"] = "attachment; filename=arabic.pdf"
    return response

 

Recommended Posts

  • 0
نشر

المشكلة أن مكتبة pisa لا تدعم الخطوط العربية بشكل جيد افتراضيًا، خاصة عند استخدام خطوط Google Fonts مباشرة. بالإضافة إلى ذلك، قد تكون هناك مشكلة في طريقة معالجة اتجاه النص (RTL) أو الترميز و ايضا حاول تنزيل ملفات الخط محليا على جهازك ووضعها في المشروع و استدعائها بهذا الشكل 

@font-face {
            font-family: 'Noto Sans Arabic';
            src: url('static/fonts/NotoSansArabic-Regular.ttf') format('truetype');
            font-weight: normal;
            font-style: normal;
        }

        .font {
            font-family: 'Noto Sans Arabic', sans-serif;
            direction: rtl;
            text-align: right;
            font-size: 18px;
        }

و اخباري بالنتيجة لو سمحت هل تم اصلاح مشكلة اللغة العربية أو لا.

  • 0
نشر

وعليكم السلام ورحمة الله وبركاته.

يرجى تجربة تحميل الملفات على جهازك محليا حيث يبدوا أن المكتبة xhtml2pdf أو بايثون لا يقومان بإرسال الطلب الخاص بالخط في ملف html.

لذلك يرجى تحميل ملف الخط محليا ووضعه في مشروعك وفي ملف html يرجى إستخدام التالي: 

<style>
    @font-face {
        font-family: "Noto Sans Arabic";
        src: url("/static/fonts/NotoSansArabic-Regular.ttf");
    }

    .font {
        font-family: "Noto Sans Arabic", sans-serif;
        direction: rtl;
        text-align: right;
        font-size: 18px;
    }
</style>

بعد تحميل الخط وتنفيذ التعديل السابق يرجى إعادة تشغيل المشروع والمحاولة مرة أخرى.

  • 0
نشر

عملت جميع مقرحات لكن بدون فائدة 

 

  html = render_template_string("""
        <html>
        <head>
            <style>
              @font-face {
            font-family: 'Noto Sans Arabic';
            src: url('static/fonts/NotoSansArabic-Regular.ttf') format('truetype');
            font-weight: normal;
            font-style: normal;
        }

        .font {
            font-family: 'Noto Sans Arabic', sans-serif;
            direction: rtl;
            text-align: right;
            font-size: 18px;
        }
            </style>
        </head>
        <body>
            <h1 class="amiri">عربي</h1>
            <h1 class="font">PDF</h1>
        </body>
        </html>
    """)
    result = io.BytesIO()
    pisa_status = pisa.CreatePDF(html, dest=result)

    if pisa_status.err:
        return "خطأ في توليد PDF", 500
    result.seek(0)
    response = make_response(result.read())
    response.headers['Content-Type'] = 'application/pdf'
    response.headers['Content-Disposition'] = 'inline; filename=document.pdf'
    return response

 

  • 0
نشر
بتاريخ منذ ساعة مضت قال Mansour Hussen:

عملت جميع مقرحات لكن بدون فائدة 

 

  html = render_template_string("""
        <html>
        <head>
            <style>
              @font-face {
            font-family: 'Noto Sans Arabic';
            src: url('static/fonts/NotoSansArabic-Regular.ttf') format('truetype');
            font-weight: normal;
            font-style: normal;
        }

        .font {
            font-family: 'Noto Sans Arabic', sans-serif;
            direction: rtl;
            text-align: right;
            font-size: 18px;
        }
            </style>
        </head>
        <body>
            <h1 class="amiri">عربي</h1>
            <h1 class="font">PDF</h1>
        </body>
        </html>
    """)
    result = io.BytesIO()
    pisa_status = pisa.CreatePDF(html, dest=result)

    if pisa_status.err:
        return "خطأ في توليد PDF", 500
    result.seek(0)
    response = make_response(result.read())
    response.headers['Content-Type'] = 'application/pdf'
    response.headers['Content-Disposition'] = 'inline; filename=document.pdf'
    return response

 

سحتاج إلى مكتبة WeasyPrint لأنها تدعم العربية بشكل ممتاز وتتعامل مع CSS و@font-face بشكل أفضل بكثير من xhtml2pdf.

أولاً عليك تثبيت gtk3-runtime-3.24.31-2022-01-04-ts-win64.exe حيث تعتمد عليه المكتبة، فقط اضغط next next:

ثم قم بغلق محرر الأكواد ومنفذ الأوامر، ثم إعادة تشغيلهم.

ثم استخدم المشروع التالي أرفقته لك، كل ما تحتاجه تثبيت الحزم من خلال:

pip install -r requirements.txt 

html_2_pdf.rar

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...