ปัญหา เกี่ยวกับ reportlab-python

  • warning: realpath() [function.realpath]: SAFE MODE Restriction in effect. The script whose uid is 1005 is not allowed to access /tmp owned by uid 0 in /var/www/sites/sugree/codenone.com/subdomains/www/html/includes/file.inc on line 190.
  • warning: realpath() [function.realpath]: SAFE MODE Restriction in effect. The script whose uid is 1005 is not allowed to access /tmp owned by uid 0 in /var/www/sites/sugree/codenone.com/subdomains/www/html/includes/file.inc on line 190.

ตอนนี้กำลังทำ reportlab ใช้ tables แสดงข้อมูลเป็นภาษาไทยไม่ได้คะ ตาม code ด้านล่างนี้ค่ะรบกวนท่านผู้รู้ช่วยตอบให้ด้วยนะคะ

ตัวอย่าง code

-*- coding: utf-8

from reportlab.platypus import SimpleDocTemplate,Paragraph,Spacer,tables,flowables from reportlab.lib.styles import getSampleStyleSheet from reportlab.rl_config import defaultPageSize from reportlab.lib.units import inch from reportlab.pdfbase.ttfonts import TTFont from reportlab.pdfbase import pdfmetrics import reportlab

PAGE_HEIGHT = defaultPageSize[1] PAGE_WIDTH = defaultPageSize[0] styles = getSampleStyleSheet() Title = “สวัสดี” reportlab.rl_config.TTFSearchPath.append(‘.’) pdfmetrics.registerFont(TTFont(‘Tahoma’,’tahoma.ttf’))

def myFirstPage(canvas,doc): canvas.saveState() canvas.setFont(‘Tahoma’,10) canvas.drawCentredString(PAGE_WIDTH/2.0,PAGE_HEIGHT-108,Title) canvas.restoreState()

def myLaterPages(canvas,doc): canvas.saveState() canvas.setFont(‘Tahoma’,9) canvas.drawString(inch,0.75*inch,”— หน้าที่ %d ” % (doc.page)) canvas.restoreState()

def go(): doc = SimpleDocTemplate(“Test1.pdf”) Story = [Spacer(1,5*inch)] style = styles[“Normal”]

    head  = [["เลขที่ PO","เลขที่เอกสาร","รหัสวัตถุดิบ","ชื่อวัตถุดิบ","จำนวน"]]
    lowidths = [1*inch,1*inch,2*inch,3*inch,1*inch]
    h = tables.Table(head,rowHeights = None,colWidths = lowidths,)
    Story.append(h)                        
    doc.build(Story,onFirstPage = myFirstPage, onLaterPages = myLaterPages)

go()


ขอบคุณนะคะ หมายเหตุ พยายามหาในเวบแล้ว ก็ยังแก้ปัญหาไม่ได้

sugree's picture

แล้วออกมาเป็นยังไงครับ

veer's picture

blockcode ช่วยทำให้ code อ่านง่ายขึ้นได้ครับ http://blog.vee-u.com/2008/02/17/blockcode/

ผลที่ออกมา เป็นภาษาไทย แค่คำว่า สวัสดี ที่ Title แต่ข้อมูล list head แสดงออกเป็นภาษาที่อ่านไม่ได้ค่ะ

ตัวอย่าง code ค่ะ

#-*- coding: utf-8
from reportlab.platypus import SimpleDocTemplate,Paragraph,Spacer,tables,flowables
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.lib.units import inch
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase import pdfmetrics
import reportlab

PAGE_HEIGHT = defaultPageSize[1]
PAGE_WIDTH = defaultPageSize[0]
styles = getSampleStyleSheet()
Title = "สวัสดี"

reportlab.rl_config.TTFSearchPath.append('.')
pdfmetrics.registerFont(TTFont('Tahoma','tahoma.ttf'))

def myFirstPage(canvas,doc):
canvas.saveState()
canvas.setFont('Tahoma',14)
canvas.drawCentredString(PAGE_WIDTH/2.0,PAGE_HEIGHT-180,Title)
canvas.restoreState()

def myLaterPages(canvas,doc):
canvas.saveState()
canvas.setFont('Tahoma',9)
canvas.drawString(inch,0.75*inch,"-- หน้าที่ %d " % (doc.page))
canvas.restoreState()

def go():
doc = SimpleDocTemplate("Test1.pdf")
Story = [Spacer(1,5*inch)]
style = styles["Normal"]
head = [["เลขที่ PO","เลข","รหัส","ชื่อ","จำนวน"]]
lowidths = [1*inch,1*inch,1*inch,1*inch,1*inch]
h = tables.Table(head,rowHeights = None,colWidths = lowidths,)
Story.append(h)
doc.build(Story,onFirstPage = myFirstPage, onLaterPages = myLaterPages)

go()

sugree's picture

ไม่ได้เซ็ตฟอนต์ให้ table

ลองทำ setfont ให้ table แล้ว เป็นภาษาไทยแล้วค่ะ ขอบคุณนะคะ

ย้าย Codenone

ประกาศย้าย Codenone ไปใช้ Forum ของ Blognone แทนครับ ตามไปตั้งกระทู้ต่อได้ที่ Codenone Forum (รายละเอียดอ่านจากกระทู้ ย้าย Codenone ไปรวมกับ Blognone)

กระทู้เก่าๆ จะย้ายตามไปในภายหลัง ตอนนี้ปิดการโพสต์กระทู้ไว้ เหลือไว้เฉพาะอ้างอิงเท่านั้น