Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

python - Reportlab generate pdf from center

Following is code from views that generate a pdf file and return it.

def pdf_file(request,id):
     
     pti=Pti.objects.get(pk=id)
     po=PurchaseOrder.objects.get(pk=id)
     inv=Invoices.objects.get(pk=id)
     reimbinv=Reimbursement.objects.get(pk=id)

     buffer=io.BytesIO()
     pdf=canvas.Canvas(buffer)

     pdf.pagewidth=A4[0]
     pdf.pageheight=A4[1]

     pdf.setTitle(inv.title)

     MARGIN_LEFT=MARGIN_RIGHT=10
     MARGIN_TOP=MARGIN_BOTTOM=10


     MT_WIDTH=A4[0]-(MARGIN_LEFT+MARGIN_RIGHT)
     MT_HEIGHT=A4[1]-(MARGIN_BOTTOM+MARGIN_TOP)
    

     HT_HEIGHT=MT_HEIGHT*35/100
     BT_HEIGHT=MT_HEIGHT*30/100
     FT_HEIGHT=MT_HEIGHT*35/100

     mainTable=Table(
          [
               #[jazz_top_header(MT_WIDTH,HT_HEIGHT*15/100)],
               [genHeader(MT_WIDTH,HT_HEIGHT,po.poNumber)],
          ],
          MT_WIDTH,HT_HEIGHT
     )
     mainTable.setStyle([
          style.BOTTOM_PADDING_ALL_ZERO,
          style.LEFT_PADDING_ALL_ZERO,
          style.TOP_PADDING_ALL_ZERO,
          style.VALIGN_ALL,
     ])

     mainTable.wrapOn(pdf,0,0)
     mainTable.drawOn(pdf,MARGIN_LEFT,MARGIN_BOTTOM)
     
     pdf.showPage()
     pdf.save()

     #response=HttpResponse(content_type='application/pdf')
     buffer.seek(0)
     return FileResponse(buffer,as_attachment=True,filename='hello.pdf')

and it calls following functions

def genHeader(width,height,po_number): 
    heightList=[
            #height*10/100, # 10% for Jazz
            height*10/100, #20% for PTI heading 
            height*10/100, #10% for agreement etc
            height*10/100, #10% for location etc
            height*10/100, #10% for certificate of PTI
           #height*50/100  #40% for text
        ]

    data_row=[
                [generateTable(width,heightList[0],'''<b>Permission To Invoice</b>''',style.TEXT_ALIGN_CENTER)],
                ['Department:','''<u>Marketing</u>''','Agreement/PO No:',str(po_number)],
                ['Location:','''<u>Islamabad</u>''','Contractor Name:','Fish Bowl Pvt. Ltd'],
                ['Certificate Of Permission to Invoice:-','','','',''],
                
            ]

    head_section_table=Table(
            data_row,
              #[jazz_top_header(width,heightList[0])],
              #[generateTable(width,heightList[0],'<b>Permission To Invoice</b>',style.TEXT_ALIGN_CENTER)],
              #[custom_colBasedTables(width,heightList[1],arr_data_row1,style.TEXT_ALIGN_RIGHT)],
              #[custom_colBasedTables(width,heightList[2],arr_data_row2,style.TEXT_ALIGN_RIGHT)],
            width/4,
            heightList
        )
    return head_section_table

and another method

def generateTable(width,height,txt,txt_align_position):
    res_table=Table([[txt]], width,height)
    res_table.setStyle([txt_align_position,style.VALIGN_ALL])
    return res_table

Here styles are properly adjusted in styles.py

class style:
    TEXT_ALIGN_CENTER=(
        'ALIGN',(0,0),(-1,-1),'CENTER'
    )
    TEXT_ALIGN_RIGHT=(
        'ALIGN',(0,0),(-1,-1),'RIGHT'
    )
    TEXT_ALIGN_LEFT=(
        'ALIGN',(0,0),(-1,-1),'LEFT'
    )
    BOTTOM_PADDING_ALL_ZERO=(
        'BOTTOMPADDING',(0,0),(-1,-1),0
    )
    TOP_PADDING_ALL_ZERO=(
        'BOTTOMPADDING',(0,0),(-1,-1),0
    )
    LEFT_PADDING_ALL_ZERO=(
        'LEFTPADDING',(0,0),(-1,-1),0
    )
    RIGHT_PADDING_ALL_ZERO=(
        'RIGHTPADDING',(0,0),(-1,-1),0
    )

    VALIGN_ALL=(
         'VALIGN', (0,0), (-1,-1), 'TOP'
    )

Code works perfectly fine but pdf is generated in the middle vertically. I tried to set VALIGN also still it does not works. Any suggestions to resolve this issue.

question from:https://stackoverflow.com/questions/66059862/reportlab-generate-pdf-from-center

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...