الأخبار التكنولوجية والاستعراضات والنصائح!

كيفية إنشاء ملف PDF برمجيًا في Magento 2

ستساعدك المقالة التالية: كيفية إنشاء ملف PDF برمجيًا في Magento 2

<؟ php

مورد مساحة الاسم \ ملحق \ وحدة تحكم \ فهرس ؛

استخدام Magento \ Framework \ App \ Action \ Action ؛

استخدام Magento \ Framework \ App \ Action \ Context ؛

استخدم Magento \ Framework \ App \ Response \ Http \ FileFactory ؛

يمتد مؤشر فئة العمل

{

fileFactory دولار محمي؛

الوظيفة العامة __construct (

سياق $ سياق ،

FileFactory $ fileFactory

) {

$ this-> fileFactory = $ fileFactory ؛

الأصل :: __ بناء (سياق $) ؛

}

تنفيذ الوظيفة العامة ()

{

pdf = جديد \ Zend_Pdf () ؛

$ pdf-> صفحات[] = $ pdf-> newPage (\ Zend_Pdf_Page :: SIZE_A4) ؛

$ page = $ pdf-> صفحات[0]؛ // سيحصل هذا على إشارة إلى الصفحة الأولى.

$ style = new \ Zend_Pdf_Style () ؛

$ style-> setLineColor (جديد \ Zend_Pdf_Color_Rgb (0،0،0)) ؛

$ font = \ Zend_Pdf_Font :: fontWithName (\ Zend_Pdf_Font :: FONT_TIMES) ؛

$ style-> setFont ($ font، 13) ؛

الصفحة $-> setStyle ($ style) ؛

العرض بالدولار = $ page-> getWidth () ؛

هايت $ = page-> getHeight () ؛

× دولار = 30 ؛

pageTopalign دولار = 850 ؛

$ this-> y = 850-100 ؛

$ style-> setFont ($ font، 14)؛

الصفحة $-> setStyle ($ style) ؛

صفحة $-> drawRectangle (30، $ this-> y + 10، $ page-> getWidth () – 30، $ this-> y +70، \ Zend_Pdf_Page :: SHAPE_DRAW_STROKE) ؛

$ style-> setFont ($ font، 13) ؛

الصفحة $-> setStyle ($ style) ؛

$ page-> drawText (__ (“تفاصيل Cutomer”)، $ x + 5، $ this-> y + 50، ‘UTF-8’)؛

$ style-> setFont ($ font، 11) ؛

الصفحة $-> setStyle ($ style) ؛

$ page-> drawText (__ (“Name:٪ 1″، “Test Meetanshi”)، $ x + 5، $ this-> y + 33، ‘UTF-8’)؛

الصفحة $-> drawText (__ (“البريد الإلكتروني:٪ 1″، “[email protected]”)، $ x + 5، $ this-> y + 16، ‘UTF-8’)؛

$ style-> setFont ($ font، 11) ؛

الصفحة $-> setStyle ($ style) ؛

$ page-> drawText (__ (“اسم المنتج”)، $ x + 60، $ this-> y-10، ‘UTF-8’)؛

$ page-> drawText (__ (“سعر المنتج”)، $ x + 200، $ this-> y-10، ‘UTF-8’)؛

$ page-> drawText (__ (“QTY”)، $ x + 310، $ this-> y-10، ‘UTF-8’)؛

$ page-> drawText (__ (“SUB TOTAL”)، $ x + 440، $ this-> y-10، ‘UTF-8’)؛

$ style-> setFont ($ font، 10)؛

الصفحة $-> setStyle ($ style) ؛

إضافة دولار = 9 ؛

الصفحة $-> drawText (“$ 12.00″، $ x + 210، $ this-> y-30، ‘UTF-8’)؛

الصفحة $-> drawText (10، $ x + 330، $ this-> y-30، ‘UTF-8’)؛

الصفحة $-> drawText (“$ 120.00″، $ x + 470، $ this-> y-30، ‘UTF-8’)؛

$ pro = “منتج تجريبي” ؛

الصفحة $-> drawText ($ pro، $ x + 65، $ this-> y-30، ‘UTF-8’)؛

صفحة $-> drawRectangle (30، $ this-> y -62، $ page-> getWidth () – 30، $ this-> y + 10، \ Zend_Pdf_Page :: SHAPE_DRAW_STROKE) ؛

صفحة $-> drawRectangle (30، $ this-> y -62، $ page-> getWidth () – 30، $ this-> y – 100، \ Zend_Pdf_Page :: SHAPE_DRAW_STROKE) ؛

$ style-> setFont ($ font، 15)؛

الصفحة $-> setStyle ($ style) ؛

$ page-> drawText (__ (“Total:٪ 1″، “$ 50.00”)، $ x + 435، $ this-> y-85، ‘UTF-8’)؛

$ style-> setFont ($ font، 10)؛

الصفحة $-> setStyle ($ style) ؛

$ page-> drawText (__ (“مثال اختبار التذييل”)، ($ page-> getWidth () / 2) -50، $ this-> y-200)؛

$ fileName = ‘meetanshi.pdf’؛

$ this-> fileFactory-> إنشاء (

$ fileName ،

pdf-> عرض ()،

\ Magento \ Framework \ App \ Filesystem \ DirectoryList :: VAR_DIR، // سيتم حفظ ملف pdf هذا في دليل var بالاسم meetanshi.pdf

“التطبيق / pdf”

) ؛

}

}