Advertisement
Ankit-Kulkarni

Sample pdf generation using FPDF in python: Processing Lists

Nov 24th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. from fpdf import FPDF
  2. lists =  [['a.html', 'b.html', 0, '[x,y]'], ['c.qbl', 'p.mbl', 'q.dbl']]
  3.  
  4. # Method 1 - printing inside the list
  5. def createPDF(lists) :
  6.     c = ""
  7.     pdf=FPDF()
  8.     pdf.add_page()
  9.     pdf.set_font('Arial','B',16)
  10.     pdf.cell(10,10, "")  # telling where the first line is-  Note: not necessary
  11.    
  12.     # loop to fetch the list :
  13.     for i in lists:
  14.         for j in i :
  15.             pdf.cell(10,20 ,str(j) )
  16.             pdf.ln(20)
  17.     pdf.output('tuto3.pdf', 'F')       
  18.    
  19. createPDF(lists)
  20.  
  21.  
  22.  
  23. # Method 2 - printing without loop: used in complex processing
  24.  
  25. #helper function to process a list
  26. def createPrintableList(lists,c):
  27.     # processing list
  28.     for i in lists:
  29.         for j in i :
  30.             c = c + "    " + str(j) # cocatenating list elements
  31.     return c
  32.  
  33. def createPDF(lists) :
  34.    
  35.     c = ""
  36.     pdf=FPDF()
  37.     pdf.add_page()
  38.     pdf.set_font('Arial','B',16)
  39.     c = createPrintableList(lists, c) # calling function to process list
  40.     pdf.cell(10, 10, c)
  41.     pdf.output('tuto4.pdf', 'F')       
  42.    
  43.  
  44.  
  45. print createPDF(lists)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement