class Default_Pdf_Creater(object):
	"""
	This class will create the pdf.
	"""
	def __init__(file_name, pagesize = None):
		self.file_name = file_name
		self.path = os.path.abspath(file_name)
		
		self.pagesize = None
		if pagesize is not None:
			if pagesize is "letter":
				self.pagesize = letter
			else:
				self.pagesize = A4
		
		self.document = SimpleDocTemplate(self.path, pagesize = self.pagesize)
		self.elements = []		
		
	def add_title_page(title):
		
		raise NotImplementedError
	
	def add_index(name_of_list, list):
		"""
		name_of_list = "Contents"
		list = (("Chapter 1", Page),("Chapter 2", Page2))
		
		"""
	
	def add_paragraph(text, title = None):
		"""
		example:
			text = ("this is some random text.", "Normal"), title = ("This is a title.", "Heading1")
		"""
		if title is not None:
			self.elements.append(Paragraph())
		
		self.elements.append(Paragraph(text, style_sheet[style]))
	
	def add_page_break():
		"""
		This method will insert a page break
		"""
		self.document.append(PageBreak())
	
	def build():
		self.document.build()
示例#2
0
 def report(self,title,tipo=None,logopath=None):
     # set some characteristics for pdf document
     if tipo is not None:
         doc = SimpleDocTemplate(
             self.buffer,
             rightMargin=72,
             leftMargin=72,
             topMargin=30,
             bottomMargin=72,
             pagesize=self.pageSize)
         if logopath is not None:
             im = Image(logopath, 2 * inch, 2 * inch)
             doc.append(im)
         # a collection of styles offer by the library
         styles = getSampleStyleSheet()
         # add custom paragraph style
         styles.add(ParagraphStyle(
             name="TableHeader", fontSize=11, alignment=TA_CENTER))
         styles.add(ParagraphStyle(
             name="ParagraphTitle", fontSize=11, alignment=TA_JUSTIFY,))
         styles.add(ParagraphStyle(
             name="Justify", alignment=TA_JUSTIFY))
         #GET DATA
         if tipo == 'Socio':
             modelos = models.Socio.objects.all()
         if tipo == 'Evento':
             modelos = models.Evento.objects.all()
         if tipo == 'Alquiler':
             modelos = models.Alquila.objects.all()
         # list used for elements added into document
         data = []
         data.append(Paragraph(title, styles['Title']))
         # insert a blank space
         data.append(Spacer(1, 12))
         table_data = []
         # table header
         if tipo == "Socio":
             table_data.append([
                 Paragraph('Nombre', styles['TableHeader']),
                 Paragraph('Email', styles['TableHeader'])
             ])
         if tipo == "Evento":
             table_data.append([
                 Paragraph('Nombre', styles['TableHeader']),
                 Paragraph('Fecha Inicio', styles['TableHeader']),
                 Paragraph('Fecha Fin', styles['TableHeader']),
                 Paragraph('Coste', styles['TableHeader']),
                 Paragraph('Beneficio', styles['TableHeader'])
             ])
         if tipo == "Alquiler":
             table_data.append([
                 Paragraph('Nombre', styles['TableHeader']),
                 Paragraph('Fecha alquiler', styles['TableHeader']),
                 Paragraph('Fecha devolución', styles['TableHeader'])
             ])
         for i, modelo in enumerate(modelos):
             # Add a row to the table
             if tipo == "Socio":
                 table_data.append([modelo.nombre,modelo.mail])
             if tipo == "Evento":
                 table_data.append([modelo.nombre,modelo.fecha_alta,modelo.fecha_baja,modelo.coste,modelo.beneficio])
             if tipo == "Alquiler":
                 table_data.append([modelo.nombre,modelo.fecha_alquiler,modelo.fecha_devolucion])
         # create table
         wh_table = Table(table_data, colWidths=[doc.width/7.0]*7)
         wh_table.hAlign = 'LEFT'
         wh_table.setStyle(TableStyle(
             [('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
              ('BOX', (0, 0), (-1, -1), 0.5, colors.black),
              ('VALIGN', (0, 0), (-1, 0), 'MIDDLE'),
              ('BACKGROUND', (0, 0), (-1, 0), colors.gray)]))
         data.append(wh_table)
         data.append(Spacer(1, 48))
         # create document
         doc.build(data, onFirstPage=self.pageNumber,
                   onLaterPages=self.pageNumber)
         pdf = self.buffer.getvalue()
         self.buffer.close()
         return pdf
     else:
         return None