2014-02-11 29 views
2

我正在生成PDF,但我無法爲不同的頁面提供不同的不同topmargins。如何爲使用reportlab的不同頁面分配doc.topMargin

有什麼方法或任何方法可以解決這個問題嗎?

response = HttpResponse(mimetype='application/pdf') 
response['Content-Disposition'] = 'attachments;filename='aa.pdf' 
doc = SimpleDocTemplate(response) 
elements = [] 
table_data = [["Cash Management -"], 
       ["individuelle Kond-"], 
       ["Cash Mas -"], 
       ["Terms and Condppe"], 
       ] 
table_dimension = Table(table_data, colWidths=[19.8 * cm], rowHeights=(.23*inch)) 
table_dimension.setStyle(TableStyle([ 
          ('BACKGROUND',(0,0),(0,3),'0x2E8B57'), 
          ('TEXTCOLOR',(0,0),(0,3),'0xFFFFFF'), 
          ('FONT', (0,0), (0,3), 'Times-Italic'), 
          ('ALIGN',(0,0),(0,3),'CENTER'), 
          ("VALIGN",(0,2),(0,2),"BOTTOM"), 
          ('FONTSIZE',(0,0),(0,1),14) 
       ])) 
elements.append(table_dimension) 
doc.topMargin=.13* inch 
doc.build(elements) 
return response 

現在所有的頁面上邊距保持不變,但我希望在每個頁面上有不同的邊距。

回答

3

一個DocTemplate類(如SimpleDocTemplate)充滿PageTemplate的然後將其充滿Frame的,然後充滿Flowable的(如您的Table)。 SimpleDocTemplate(顧名思義),簡化的東西買爲您提供最基礎的設置:一個DocTemplate已經具有PageTemplate和基本Frame剛剛有充滿Flowable的。所以問題在於:DocTemplate頁邊距對於整個文檔是固定的,順便說一句。你想調整你的Frame的利潤!爲此,您可以嘗試獲取當前使用的幀的句柄,或者開始使用更精細的BaseDocTemplate並定義您自己的PageTemplateFrame's。我建議您閱讀第5章reportlab user guide中的相應頁面,並搜索BaseDocTemplate示例,其中顯示FramePageTemplate的生成。

另一種選擇是不直接使用邊距。你可以使用Spacer()添加垂直空間和你的表的colWidths來調整水平寬度,但這並不是很有趣。 SimpleDocTemplate在構建函數中添加PageTemplatesFrames,所以我們不能在此之前修改它們。如果你需要一個更廣泛的例子,你需要發佈更多的代碼,所以我可以看到你真正想要的,因爲你的代碼只產生一個頁面。

UPDATE

下面是關於如何使用幀尺寸喲調整在不同的頁面邊緣的例子。也可以調整PageTemplates的大小。我認爲你應該能夠理解代碼,但是又一次:thie是BaseDocTemplate,而不是SimpleDocTemplate。如果您在第一頁的第一頁上有一個表格,則不需要添加PageBreak(),但NextPageTemplate()是強制性的!

""" 
Example how to adjust the Frame-size in a BaseDocTemplate 
""" 
from reportlab.platypus import BaseDocTemplate, Frame, Paragraph, NextPageTemplate, PageBreak, PageTemplate 
from reportlab.lib.units import inch 
from reportlab.lib.styles import getSampleStyleSheet 

def on_first_page(canvas,doc): 
    canvas.saveState() 
    canvas.setFont('Times-Roman',19) 
    canvas.drawString(inch, 0.75 * inch, "Page %d" % doc.page) 
    canvas.restoreState() 

def on_remaining_pages(canvas,doc): 
    canvas.saveState() 
    canvas.setFont('Times-Roman',9) 
    canvas.drawString(inch, 0.75 * inch, "Page %d" % doc.page) 
    canvas.restoreState() 

# creation of the BaseDocTempalte. showBoundary=0 to hide the debug borders 
doc = BaseDocTemplate('basedoc.pdf',showBoundary=1) 

# create the frames. Here you can adjust the margins 
frame_first_page = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height, id='first') 
frame_remaining_pages = Frame(doc.leftMargin + 1*inch, doc.bottomMargin + 1*inch, doc.width - 2*inch, doc.height - 2*inch, id='remaining') 

# add the PageTempaltes to the BaseDocTemplate. You can also modify those to adjust the margin if you need more control over the Frames. 
doc.addPageTemplates([PageTemplate(id='first_page',frames=frame_first_page, onPage=on_first_page), 
         PageTemplate(id='remaining_pages',frames=frame_remaining_pages, onPage=on_remaining_pages), 
         ]) 

styles=getSampleStyleSheet() 
# start the story... 
Elements=[] 

Elements.append(Paragraph("Frame first page!",styles['Normal'])) 
Elements.append(NextPageTemplate('remaining_pages')) #This will load the next PageTemplate with the adjusted Frame. 
Elements.append(PageBreak()) # This will force a page break so you are guarented to get the next PageTemplate/Frame 

Elements.append(Paragraph("Frame remaining pages!, "*500,styles['Normal'])) 

#start the construction of the pdf 
doc.build(Elements) 
+0

感謝fookatchu但有沒有使用除了basedoctemplate – user2501134

+0

你好fookatchu相同的文檔模板的任何方式,我解釋你我的代碼: – user2501134

+0

你好fookatchu,我解釋你我的代碼:1)我創建3個表和存儲或者將數據插入到元素數組中。2)我使用兩個標題頁1的一個標題和剩餘頁的另一個標題3)並且我在表中動態添加行4)我設置了一個doc.topmargin和它對於所有人來說都是一樣的,但我希望第一頁的不同。5)我的代碼非常大,這就是爲什麼我不發帖,如果你無法得到它,然後告訴我,我會在最小化後發佈....提前致謝 – user2501134

相關問題