2009-01-16 65 views

回答

3

[我的回答不再是正確的,現在梯度可在ReportLab的,看到這個頁面的詳細信息,對方的回答。]

抱歉ressurect這個問題,但我偶然發現了它,它hadn沒有被正確回答。

答案是否定的,截至今日,ReportLab的當前版本不支持漸變。但是,PDF支持漸變。如果你看看ReportLab的Canvas類,你會發現它的很多方法都是基礎PDF代碼生成的相對較小的包裝。要訪問RL中的漸變,您需要擴展Canvas類並添加其他方法來生成正確的PDF代碼。這是可行的,但顯然不是微不足道的,這意味着你必須閱讀PDF規範。

有兩種選擇。首先生成漸變作爲光柵圖像並使用它,其次通過繪製一系列不同顏色的矩形生成漸變。

start_color = (1,0,0) 
end_color = (0,1,0) 
for i in range(100): 
    p = i * 0.01 
    canvas.setFillColorRGB(*[start_color[i]*(1.0-p)+end_color[i]*p for i in range(3)]) 
    canvas.rect(i, 0, 2, 100) 

例如,不幸的是,漸變平滑需要大量的矩形,這可能會導致PDF變大並呈現緩慢。採用柵格方法會更好。

最後,你可能會考慮使用PyCairo。這更好地支持大量的圖形元素,並可以呈現爲PDF或PNG。但是,它缺少reportlabs高級槓桿結構(如頁面佈局)。

6

ReportLab現在支持PDF漸變。

Peter Johnson在2012年8月6日爲posted to the ReportLab mailing list提供了梯度支持補丁,第二天是added to the source。我不能在the release notes for ReportLab 2.6中發現任何東西,但是自2012年10月1日發佈以來,它可能在那裏。它絕對存在於2.7中。

可以指定具有多個停止點的線性和徑向梯度。在文檔中搜索梯度沒有任何變化。然而,message with the first version of the patch有幾個例子,它們是some tests in the ReportLab source的基礎。在此基礎上我已經工作了一個快速演示腳本:

from reportlab.pdfgen.canvas import Canvas 
from reportlab.lib.colors import red, yellow, green 
from reportlab.lib.units import mm 

c = Canvas("gradient.pdf") 

# Linear gradient with the endpoints extending over the page. 
c.linearGradient(105*mm, 200*mm, 180*mm, 100*mm, (red, yellow)) 
c.drawString(5*mm, 290*mm, "c.linearGradient(105*mm, 200*mm, 180*mm, 100*mm, (red, yellow))") 
c.line(105*mm, 200*mm, 180*mm, 100*mm) 
c.showPage() 

# Linear gradient constrained within the endpoints. 
c.linearGradient(105*mm, 200*mm, 180*mm, 100*mm, (red, yellow), extend=False) 
c.drawString(5*mm, 290*mm, "c.linearGradient(105*mm, 200*mm, 180*mm, 100*mm, (red, yellow), extend=False)") 
c.line(105*mm, 200*mm, 180*mm, 100*mm) 
c.showPage() 

# Linear gradient with multiple stops. 
c.linearGradient(105*mm, 200*mm, 180*mm, 100*mm, (red, yellow, green), (0, 0.8, 1), extend=False) 
c.drawString(5*mm, 290*mm, "c.linearGradient(105*mm, 200*mm, 180*mm, 100*mm, (red, yellow, green), (0, 0.8, 1), extend=False)") 
c.line(105*mm, 200*mm, 180*mm, 100*mm) 
c.line(141*mm, 102*mm, 189*mm, 138*mm) 
c.showPage() 

# Radial gradient with the endpoint extending over the page. 
c.radialGradient(105*mm, 200*mm, 60*mm, (red, yellow)) 
c.drawString(5*mm, 290*mm, "c.radialGradient(105*mm, 200*mm, 60*mm, (red, yellow))") 
c.circle(105*mm, 200*mm, 60*mm) 
c.showPage() 

# Radial gradient constrained within the circle. 
c.radialGradient(105*mm, 200*mm, 60*mm, (red, yellow), extend=False) 
c.drawString(5*mm, 290*mm, "c.radialGradient(105*mm, 200*mm, 60*mm, (red, yellow), extend=False)") 
c.circle(105*mm, 200*mm, 60*mm) 
c.showPage() 

# Radial gradient with multiple stops. 
c.radialGradient(105*mm, 200*mm, 60*mm, (red, yellow, green), (0, 0.8, 1)) 
c.drawString(5*mm, 290*mm, "c.radialGradient(105*mm, 200*mm, 60*mm, (red, yellow, green), (0, 0.8, 1))") 
c.circle(105*mm, 200*mm, 48*mm) 
c.circle(105*mm, 200*mm, 60*mm) 
c.showPage() 

c.save() 

此輸出六頁各種梯度加梯度方法調用和線/圓圈表示在端點和站是:

Basic linear gradient extending over page Linear gradient constrained within endpoints Linear gradient with multiple stops Basic radial gradient extending over page Radial gradient constrained within radius Radial gradient with multiple stops

0

要填充矩形(鄰r其他路徑)用漸變代替 一個純色?

沒問題。使用剪裁將漸變綁定/限制到路徑。請記住在設置漸變之前在之前設置剪輯路徑。 (並將其包裹在saveState()/restoreState()之後以重置剪輯和漸變。)

c = canvas.Canvas (filename) 
#c.translate (8*cm, 8*cm) # use this to move the rectangle 
p = c.beginPath() 
p.rect (0,0 , 5*cm,5*cm) 
c.clipPath (p, stroke=0) 
c.linearGradient (0,0 , 5*cm, 5*cm , (red, yellow)) 

對於徑向梯度可能是足夠的extend關鍵字參數設置爲False

+0

儘管這裏有有用的信息,但它應該解決實際問題,而不是在另一個答案中提出的問題。 – 2014-06-19 20:32:25