2014-02-20 69 views
0

我已经能够成功创建一个pdf文件,其中包含一个简单的例子here,它的工作完美无瑕。我也意识到,只需添加一些参数即可使用写入命令创建链接。但是,我不确定如何(最有效/最恰当)将其添加到模板中。理想情况下,我想将其添加到元素字典中。PyFPDF在模板中创建链接

编辑:其实我甚至不认为模板对象允许使用Write()选项,以便有可能没有办法做一个模板的链接,它看起来像我不得不写我自己对象,如果我想要一个网址。

from pyfpdf import Template 

#this will define the ELEMENTS that will compose the template. 
elements = [ 
    { 'name': 'company_logo', 'type': 'I', 'x1': 20.0, 'y1': 17.0, 'x2': 78.0, 'y2': 30.0, 'font': None, 'size': 0.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'logo', 'priority': 2, }, 
    { 'name': 'company_name', 'type': 'T', 'x1': 17.0, 'y1': 32.5, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, 
    { 'name': 'box', 'type': 'B', 'x1': 15.0, 'y1': 15.0, 'x2': 185.0, 'y2': 260.0, 'font': 'Arial', 'size': 0.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': None, 'priority': 0, }, 
    { 'name': 'box_x', 'type': 'B', 'x1': 95.0, 'y1': 15.0, 'x2': 105.0, 'y2': 25.0, 'font': 'Arial', 'size': 0.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': None, 'priority': 2, }, 
    { 'name': 'line1', 'type': 'L', 'x1': 100.0, 'y1': 25.0, 'x2': 100.0, 'y2': 57.0, 'font': 'Arial', 'size': 0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': None, 'priority': 3, }, 
    { 'name': 'barcode', 'type': 'BC', 'x1': 20.0, 'y1': 246.5, 'x2': 140.0, 'y2': 254.0, 'font': 'Interleaved 2of5 NT', 'size': 0.75, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '200000000001000159053338016581200810081', 'priority': 3, }, 
] 

#here we instantiate the template and define the HEADER 
f = Template(format="A4",elements=elements, 
      title="Sample Invoice") 
f.add_page() 

#we FILL some of the fields of the template with the information we want 
#note we access the elements treating the template instance as a "dict" 
f["company_name"] = "Sample Company" 
f["company_logo"] = "pyfpdf/tutorial/logo.png" 

#and now we render the page 
f.render("./template.pdf") 

上面的代码是在link中提供的示例。

回答

0

所以我查了一下源代码,看起来好像没有模板中的链接。添加以下代码:

def write(self, pdf, x1=0, y1=0, x2=0, y2=0, text='', font="arial", size=1, 
      bold=False, italic=False, underline=False, align="", link='http://example.com', 
     foreground=0, *args, **kwargs): 
    if pdf.text_color!=rgb(foreground): 
     pdf.set_text_color(*rgb(foreground)) 
    font = font.strip().lower() 
    if font == 'arial black': 
     font = 'arial' 
    style = "" 
    for tag in 'B', 'I', 'U': 
     if (text.startswith("<%s>" % tag) and text.endswith("</%s>" %tag)): 
      text = text[3:-4] 
      style += tag 
    if bold: style += "B" 
    if italic: style += "I" 
    if underline: style += "U" 
    align = {'L':'L','R':'R','I':'L','D':'R','C':'C','':''}.get(align) # D/I in spanish 
    pdf.set_font(font,style,size) 
    ##m_k = 72/2.54 
    ##h = (size/m_k) 
    pdf.set_xy(x1,y1) 
    pdf.write(5,text,link) 

成templates.py并在元件自处理程序改变了线

-       'B': self.rect, 'BC': self.barcode, } 
+       'B': self.rect, 'BC': self.barcode, 'W' self.write, } 

。有了这个,您可以使用类似的语法在元素dict对象中编写文本行。只需改变类型:'T'键入:'W'并添加一个链接:'http://code.google.com/p/pyfpdf/'到它或任何你想要的链接。我提交了这个补丁,它应该在下一个版本中提供。我在参数中留下了x2 y2,因为我不确定它们是否需要解析,但是我相信Write()方法只使用x1 y1,如果它与PHP版本类似。 K thx bye