Advertisement
Guest User

Untitled

a guest
Jan 28th, 2013
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. For working with fpdf templates and Web2Py you need to understand the following statements:
  2.  
  3. * A Template is a header (some definitions like page size and title) and collection of elements
  4.  
  5. * Each element contains attributes that define where it is in the template and how the element behaves. This attributes are: keys = ('name','type','x1','y1','x2','y2','font','size',
  6. 'bold','italic','underline','foreground','background',
  7. 'align','text','priority', 'multiline')
  8. To define a template you NEED this elements
  9.  
  10. You will need a controller that will use this template.
  11.  
  12. To have a template that a controller can use you have 3 ways:
  13. 1 - defining all your template in a dictionary and doing:
  14. from fpdf import Template
  15.  
  16. elements = [
  17. { '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, },
  18. { '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, },
  19. { '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, },
  20. { '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, },
  21. { '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, },
  22. ]
  23.  
  24. def test_template():
  25. f = Template(format="A4",elements=elements,
  26. title="Sample Invoice")
  27. f.add_page()
  28. f["company_name"] = "Sample Company"
  29. response.headers['Content-Type']='application/pdf'
  30. return f.render("./template.pdf",dest='S')
  31.  
  32. 2 - defining your template (by hand or with the designer) in a csv file and loading it with:
  33. from template import Template
  34. def test_template():
  35. f = Template(format="A4",
  36. title="Sample Invoice")
  37. f.parse_csv("mycsvfile.csv")
  38. f.add_page()
  39. f["company_name"] = "Sample Company"
  40. response.headers['Content-Type']='application/pdf'
  41. return f.render("./template.pdf",dest='S')
  42.  
  43. 3 - defining the templates in a database and FILL them.
  44. For that you will need to copy in your models the following (warning, some of the fields might conflict with some reserved words for some of the databases engines, sqlite, mysql and postgres work correctly):
  45.  
  46. from gluon.tools import Auth, Crud, Service, PluginManager, prettydate
  47. auth = Auth(db)
  48. crud, service, plugins = Crud(db), Service(), PluginManager()
  49.  
  50. db.define_table("pdf_template",
  51. Field("pdf_template_id","id"),
  52. Field("title"),
  53. Field("format", requires=IS_IN_SET(["A4","legal","letter"])),
  54. )
  55.  
  56. db.define_table("pdf_element",
  57. Field("pdf_template_id", db.pdf_template, requires=IS_IN_DB(db,'pdf_template.pdf_template_id', 'pdf_template.title')),
  58. Field("name", requires=IS_NOT_EMPTY()),
  59. Field("type", length=2, requires=IS_IN_SET(['T', 'L', 'I', 'B', 'BC'])),
  60. Field("x1", "double", requires=IS_NOT_EMPTY()),
  61. Field("y1", "double", requires=IS_NOT_EMPTY()),
  62. Field("x2", "double", requires=IS_NOT_EMPTY()),
  63. Field("y2", "double", requires=IS_NOT_EMPTY()),
  64. Field("font", default="Arial", requires=IS_IN_SET(['Courier','Arial','Times','Symbol','Zapfdingbats'])),
  65. Field("size", "double", default="10", requires=IS_NOT_EMPTY()),
  66. Field("bold", "boolean"),
  67. Field("italic", "boolean"),
  68. Field("underline", "boolean"),
  69. Field("foreground", "integer", default=0x000000, comment="Color text"),
  70. Field("background", "integer", default=0xFFFFFF, comment="Fill color"),
  71. Field("align", "string", length=1, default="L", requires=IS_IN_SET(['L', 'R', 'C', 'J'])),
  72. Field("text", "text", comment="Default text"),
  73. Field("priority", "integer", default=0, comment="Z-Order"),
  74. )
  75.  
  76. For filling them there is no code available yet
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement