def create_title(self, title_text, element_type=None):
if element_type == "document":
title_text_style = self.text_styleT
elif element_type == "section":
title_text_style = self.text_styleH
else:
title_text_style = self.text_styleN
title = Paragraph('''<para align=center spaceb=3><b>''' + title_text + '''</b></para>''', title_text_style)
title_table = Table([[title]], colWidths=[5 * inch] * 1)
title_table.setStyle(self.title_style)
return title_table
python类Paragraph()的实例源码
pdf_generator.py 文件源码
项目:yahoo-fantasy-football-metrics
作者: uberfastman
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def add_question(self, question):
self.pdf_elements.append(
Paragraph(question, self.body_style),
)
self.pdf_elements.append(Spacer(1, 4))
def add_answer_list(self, answers):
for answer in answers:
self.pdf_elements.append(
Paragraph(answer, self.notes_style),
)
self.pdf_elements.append(Spacer(1, 1))
def hello(c, link):
c.translate(ORIGIN_X, ORIGIN_Y)
# Draw paragraph
stylesheet = getSampleStyleSheet()
style = stylesheet['BodyText']
style.fontName = 'LeagueGothic'
style.fontSize = 42
style.leading = 44
p = Paragraph('<b>print</b><br/>your<br/><b>badge</b><br/>here', style)
qr_left = 30*mm
p_w, p_h = p.wrap(qr_left, HEIGHT)
p.drawOn(c, 0, 0)
# Add QR Code
qr_code = qr.QrCodeWidget(link)
qr_bounds = qr_code.getBounds()
qr_width = qr_bounds[2] - qr_bounds[0]
qr_height = qr_bounds[3] - qr_bounds[1]
d = Drawing(HEIGHT, HEIGHT, transform=[HEIGHT/qr_width,0,0,HEIGHT/qr_height,0,0])
d.add(qr_code)
renderPDF.draw(d, c, qr_left, 0)
# Draw thin line between text and QR code
c.line(qr_left, 0, qr_left, HEIGHT)
c.line(qr_left + HEIGHT, 0, qr_left+HEIGHT, HEIGHT)
img_left = qr_left + HEIGHT
# Draw images
c.drawImage('images/ipv6.jpg', img_left, 0, 20*mm, 1/3 * HEIGHT, mask=None, preserveAspectRatio=True, anchor='c')
c.drawImage('images/ffrhein_logo_claim_line_rot.png', img_left, 1/3*HEIGHT, 20*mm, 2/3 * HEIGHT, mask=None, preserveAspectRatio=True, anchor='c')
def render(self):
style = getSampleStyleSheet()['Normal']
style.alignment = self.text_align
style.fontName = self.font_name
if self._layout.debug_fields:
style.backColor = "rgba(255, 0, 0, 0.5)"
if self.fit_text:
original_size = self.font_size
text_width = stringWidth(self.data, self.font_name, self.font_size)
while text_width > self.width:
self.font_size -= 1
text_width = stringWidth(self.data, self.font_name, self.font_size)
# Size has been adjusted. Lower text accordingly
if original_size > self.font_size:
self._offset_top = (original_size - self.font_size) / 2.0
if self.height == 0:
self.height = self.font_size
style.fontSize = self.font_size
style.leading = self.font_size
p = Paragraph('<font color="%s">%s</font>' % (self.color, self.data), style)
p.wrap(self.width, self.height)
top = self._layout.height - self.top - self.height - self._offset_top
p.drawOn(self._canvas, self.left, top)
def resize_images(self, story):
# replace images with resized ones fitting into the available width
W, H = (
self.width - self.lm - self.rm), self.height - self.tm - self.bm
for i, el in enumerate(story):
if el.__class__ == Image:
img = PIL.Image.open(el.filename)
h = W / img.size[0] * img.size[1]
img = Image(el.filename, width=w, height=h, kind='direct',
mask="auto", lazy=1)
story[i] = img
elif type(el) == str:
story[i] = Paragraph(el, bt) # Spacer(0, 0)
def markdown_to_platypus(path):
"Convert a specific MarkDown file into ReportLab Platypus items."
cover, qa = extract_markdown(path)
title, paras = cover
cover = [Paragraph(title, h1)] + [Paragraph(p, bt) for p in paras]
items = [{"q": Paragraph(q, bt),
"a": XPreformatted(pygments2xpre(a), code)} for q, a in qa]
return cover, items
def make_cards_platypus(cardSize, cover, items, verbose=False):
"Generate q/a sides of quiz cards from a list of quiz items."
# cover and items are Platypus flowables!
cw, ch = cardSize
kwDict = {
"lm": 4 * mm,
"rm": 4 * mm,
"text": "",
"width": cw,
"height": ch,
"verbose": verbose
}
q_side = QuizCard(**kwDict)
a_side = QuizCard(**kwDict)
# first cover card
questions = cover
answers = []
# rest of the normal cards
for i, item in enumerate(items):
q, a = item["q"], item["a"]
q_side.text = questions
a_side.text = answers
yield q_side, a_side
questions = [Paragraph("Question:", fine)] + [q]
answers = [Paragraph("Answer %d:<br/><br/>" % (i + 1), fine)] + [a]
q_side = QuizCard(**kwDict)
a_side = QuizCard(**kwDict)
q_side.text = questions
a_side.text = answers
yield q_side, a_side
def _header_footer(self, canvas, doc):
# Save the state of our canvas so we can draw on it
canvas.saveState()
styles = getSampleStyleSheet()
style_right = ParagraphStyle(name='right', parent=styles['Normal'], fontName='Helvetica',
fontSize=10, alignment=TA_RIGHT)
# Header
fieldsight_logo = Image('http://' + self.base_url +'/static/images/fs1.jpg')
fieldsight_logo._restrictSize(1.5 * inch, 1.5 * inch)
# headerleft = Paragraph("FieldSight", styles['Normal'])
headerright = Paragraph(self.project_name, style_right)
# w1, h1 = headerleft.wrap(doc.width, doc.topMargin)
w2, h2 = headerright.wrap(doc.width, doc.topMargin)
textWidth = stringWidth(self.project_name, fontName='Helvetica',
fontSize=10)
fieldsight_logo.drawOn(canvas, doc.leftMargin, doc.height + doc.topMargin + 12)
headerright.drawOn(canvas, doc.leftMargin, doc.height + doc.topMargin + 20)
project_logo = Image('http://' + self.base_url + self.project_logo)
project_logo._restrictSize(0.4 * inch, 0.4 * inch)
project_logo.drawOn(canvas, headerright.width + doc.leftMargin -0.5 * inch - textWidth, doc.height + doc.topMargin + 10)
# header.drawOn(canvas, doc.leftMargin + doc.width, doc.height + doc.topMargin +20)
# Footer
footer = Paragraph('Page no. '+str(canvas._pageNumber), style_right)
w, h = footer.wrap(doc.width, doc.bottomMargin)
footer.drawOn(canvas, doc.leftMargin, h + 40)
# Release the canvas
canvas.restoreState()
def tabla_encabezado(self, styles):
orden_compra = self.orden_compra
sp = ParagraphStyle('parrafos',
alignment = TA_CENTER,
fontSize = 14,
fontName="Times-Roman")
try:
archivo_imagen = os.path.join(settings.MEDIA_ROOT,str(EMPRESA.logo))
imagen = Image(archivo_imagen, width=90, height=50,hAlign='LEFT')
except:
imagen = Paragraph(u"LOGO", sp)
nro = Paragraph(u"ORDEN DE COMPRA", sp)
ruc = Paragraph("R.U.C."+EMPRESA.ruc, sp)
encabezado = [[imagen,nro,ruc],['',u"N°"+orden_compra.codigo,EMPRESA.distrito + " " + orden_compra.fecha.strftime('%d de %b de %Y')]]
tabla_encabezado = Table(encabezado,colWidths=[4 * cm, 9 * cm, 6 * cm])
tabla_encabezado.setStyle(TableStyle(
[
('ALIGN',(0,0),(2,1),'CENTER'),
('VALIGN',(0,0),(2,0),'CENTER'),
('VALIGN',(1,1),(2,1),'TOP'),
('SPAN',(0,0),(0,1)),
]
))
return tabla_encabezado
def tabla_datos(self, styles):
orden = self.orden_compra
izquierda = ParagraphStyle('parrafos',
alignment = TA_LEFT,
fontSize = 10,
fontName="Times-Roman")
cotizacion = orden.cotizacion
if cotizacion is None:
proveedor = orden.proveedor
else:
proveedor = orden.cotizacion.proveedor
razon_social_proveedor = Paragraph(u"SEÑOR(ES): "+proveedor.razon_social, izquierda)
ruc_proveedor = Paragraph(u"R.U.C.: "+proveedor.ruc, izquierda)
direccion = Paragraph(u"DIRECCIÓN: "+proveedor.direccion, izquierda)
try:
telefono = Paragraph(u"TELÉFONO: "+proveedor.telefono, izquierda)
except:
telefono = Paragraph(u"TELÉFONO: -", izquierda)
try:
referencia = Paragraph(u"REFERENCIA: "+orden.cotizacion.requerimiento.codigo+" - "+orden.cotizacion.requerimiento.oficina.nombre, izquierda)
except:
referencia = Paragraph(u"REFERENCIA: ",izquierda)
proceso = Paragraph(u"PROCESO: "+orden.proceso, izquierda)
nota = Paragraph(u"Sírvase remitirnos según especificaciones que detallamos lo siguiente: ", izquierda)
datos = [[razon_social_proveedor,ruc_proveedor],[direccion,telefono],[referencia,''],[proceso,''],[nota,'']]
tabla_detalle = Table(datos,colWidths=[11* cm, 9 * cm])
tabla_detalle.setStyle(TableStyle(
[
('SPAN',(0,2),(1,2)),
]
))
return tabla_detalle
def tabla_detalle(self):
orden = self.orden_compra
encabezados = ['Item', 'Cantidad', 'Unidad', u'Descripción','Precio','Total']
detalles = DetalleOrdenCompra.objects.filter(orden=orden).order_by('pk')
sp = ParagraphStyle('parrafos')
sp.alignment = TA_JUSTIFY
sp.fontSize = 8
sp.fontName="Times-Roman"
lista_detalles = []
for detalle in detalles:
try:
tupla_producto = [Paragraph(str(detalle.nro_detalle),sp),
Paragraph(str(detalle.cantidad), sp),
Paragraph(detalle.detalle_cotizacion.detalle_requerimiento.producto.unidad_medida.descripcion,sp),
Paragraph(detalle.detalle_cotizacion.detalle_requerimiento.producto.descripcion, sp),
Paragraph(str(detalle.precio),sp),
Paragraph(str(detalle.valor),sp)]
except:
tupla_producto = [Paragraph(str(detalle.nro_detalle),sp),
Paragraph(str(detalle.cantidad), sp),
Paragraph(detalle.producto.unidad_medida.descripcion,sp),
Paragraph(detalle.producto.descripcion, sp),
Paragraph(str(detalle.precio),sp),
Paragraph(str(detalle.valor),sp)]
lista_detalles.append(tupla_producto)
adicionales = [('','','','','')] * (15-len(lista_detalles))
tabla_detalle = Table([encabezados] + lista_detalles + adicionales,colWidths=[0.8 * cm, 2 * cm, 2.5 * cm,10.2* cm, 2 * cm, 2.5 * cm])
style = TableStyle(
[
('ALIGN',(0,0),(4,0),'CENTER'),
('GRID', (0, 0), (-1, -1), 1, colors.black),
('FONTSIZE', (0, 0), (-1, -1), 7),
('ALIGN',(4,1),(-1,-1),'LEFT'),
('VALIGN',(0,0),(-1,-1),'TOP'),
]
)
tabla_detalle.setStyle(style)
return tabla_detalle
def cuadro_observaciones(self,pdf,y,orden):
p = ParagraphStyle('parrafos')
p.alignment = TA_JUSTIFY
p.fontSize = 10
p.fontName="Times-Roman"
obs=Paragraph("Observaciones: "+orden.observaciones,p)
observaciones = [[obs]]
tabla_observaciones = Table(observaciones,colWidths=[18.50 * cm], rowHeights=1.8 * cm)
tabla_observaciones.setStyle(TableStyle(
[
('GRID', (0, 0), (0, 2), 1, colors.black),
('FONTSIZE', (0, 0), (-1, -1), 8),
('ALIGN',(0,0),(-1,-1),'LEFT'),
('VALIGN',(0,0),(-1,-1),'TOP'),
]
))
tabla_observaciones.wrapOn(pdf, 800, 600)
tabla_observaciones.drawOn(pdf, 40,y-58)
def tabla_detalle(self):
requerimiento = self.requerimiento
encabezados = ['Nro', 'Cantidad', 'Unidad', u'Descripción', 'Uso']
detalles = DetalleRequerimiento.objects.filter(requerimiento=requerimiento)
sp = ParagraphStyle('parrafos')
sp.alignment = TA_JUSTIFY
sp.fontSize = 8
sp.fontName = "Times-Roman"
lista_detalles = []
for detalle in detalles:
tupla_producto = [Paragraph(str(detalle.nro_detalle), sp),
Paragraph(str(detalle.cantidad), sp),
Paragraph(detalle.producto.unidad_medida.descripcion, sp),
Paragraph(detalle.producto.descripcion, sp),
Paragraph(detalle.uso, sp)]
lista_detalles.append(tupla_producto)
adicionales = [('', '', '', '', '')] * (15 - len(detalles))
tabla_detalle = Table([encabezados] + lista_detalles, colWidths=[0.8 * cm, 2 * cm, 2.5 * cm, 7 * cm, 7.7 * cm])
style = TableStyle(
[
('ALIGN', (0, 0), (4, 0), 'CENTER'),
('GRID', (0, 0), (-1, -1), 1, colors.black),
('FONTSIZE', (0, 0), (-1, -1), 7),
('ALIGN', (4, 1), (-1, -1), 'LEFT'),
('VALIGN', (0, 0), (-1, -1), 'TOP'),
]
)
tabla_detalle.setStyle(style)
return tabla_detalle
def tabla_encabezado(self, styles):
movimiento = self.movimiento
sp = ParagraphStyle('parrafos',
alignment = TA_CENTER,
fontSize = 14,
fontName="Times-Roman")
try:
archivo_imagen = os.path.join(settings.MEDIA_ROOT,str(EMPRESA.logo))
imagen = Image(archivo_imagen, width=90, height=50,hAlign='LEFT')
except:
imagen = Paragraph(u"LOGO", sp)
if movimiento.tipo_movimiento.incrementa:
nota = Paragraph(u"NOTA DE INGRESO N°", sp)
else:
nota = Paragraph(u"NOTA DE SALIDA N°", sp)
id_movimiento = Paragraph(movimiento.id_movimiento, sp)
fecha = Paragraph("FECHA: "+movimiento.fecha_operacion.strftime('%d/%m/%y'), sp)
encabezado = [[imagen,nota,fecha],
['',id_movimiento,'']
]
tabla_encabezado = Table(encabezado,colWidths=[4 * cm, 9 * cm, 6 * cm])
tabla_encabezado.setStyle(TableStyle(
[
('VALIGN',(0,0),(2,0),'CENTER'),
('VALIGN',(1,1),(2,1),'TOP'),
('SPAN',(0,0),(0,1)),
]
))
return tabla_encabezado
def tabla_datos(self, styles):
movimiento = self.movimiento
izquierda = ParagraphStyle('parrafos',
alignment = TA_LEFT,
fontSize = 10,
fontName="Times-Roman")
try:
if movimiento.referencia.cotizacion is not None:
proveedor = Paragraph(u"PROVEEDOR: "+movimiento.referencia.cotizacion.proveedor.razon_social,izquierda)
else:
proveedor = Paragraph(u"PROVEEDOR: "+movimiento.referencia.proveedor.razon_social,izquierda)
except:
proveedor = Paragraph(u"PROVEEDOR:",izquierda)
operacion = Paragraph(u"OPERACIÓN: "+movimiento.tipo_movimiento.descripcion,izquierda)
almacen = Paragraph(u"ALMACÉN: "+movimiento.almacen.codigo+"-"+movimiento.almacen.descripcion,izquierda)
try:
orden_compra = Paragraph(u"ORDEN DE COMPRA: "+movimiento.referencia.codigo,izquierda)
except:
orden_compra = Paragraph(u"REFERENCIA: -",izquierda)
try:
documento = Paragraph(u"DOCUMENTO: "+movimiento.tipo_documento.descripcion + " SERIE:" + movimiento.serie + u" NÚMERO:" + movimiento.numero,
izquierda)
except:
documento = ""
try:
pedido = Paragraph(u"PEDIDO: "+movimiento.pedido.codigo, izquierda)
except:
pedido = ""
encabezado = [[operacion,''],
[almacen,''],
[proveedor,''],
[orden_compra,''],
[documento,''],
[pedido,'']]
tabla_datos = Table(encabezado,colWidths=[11 * cm, 9 * cm])
tabla_datos.setStyle(TableStyle(
[
]
))
return tabla_datos
def _header(self, canvas, doc):
canvas.saveState()
sp = ParagraphStyle('parrafos',
alignment=TA_CENTER,
fontSize=14,
fontName="Times-Roman")
try:
archivo_imagen = os.path.join(settings.MEDIA_ROOT, str(EMPRESA.logo))
imagen = Image(archivo_imagen, width=90, height=50, hAlign='LEFT')
except:
imagen = Paragraph(u"LOGO", sp)
ruc_empresa = "RUC: " + EMPRESA.ruc
if self.valorizado:
titulo = Paragraph(u"REGISTRO DEL INVENTARIO PERMANENTE VALORIZADO", sp)
else:
titulo = Paragraph(u"REGISTRO DEL INVENTARIO PERMANENTE EN UNIDADES FÍSICAS",sp)
pagina = u"Página " + str(doc.page) + " de " + str(self.total_paginas)
encabezado = [[imagen, titulo, pagina], [ruc_empresa, "", ""]]
tabla_encabezado = Table(encabezado, colWidths=[3 * cm, 20 * cm, 3 * cm])
style = TableStyle(
[
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
]
)
tabla_encabezado.setStyle(style)
tabla_encabezado.wrapOn(canvas, 50, 510)
tabla_encabezado.drawOn(canvas, 50, 510)
canvas.restoreState()
def get(self, request, *args, **kwargs):
response = HttpResponse(content_type='application/pdf')
pdf_name = "clientes.pdf" # llamado clientes
# la linea 26 es por si deseas descargar el pdf a tu computadora
# response['Content-Disposition'] = 'attachment; filename=%s' % pdf_name
buff = BytesIO()
doc = SimpleDocTemplate(buff,
pagesize=letter,
rightMargin=40,
leftMargin=40,
topMargin=60,
bottomMargin=18,
)
clientes = []
styles = getSampleStyleSheet()
header = Paragraph("Listado de Clientes", styles['Heading1'])
clientes.append(header)
headings = ('Nombre', 'Email', 'Edad', 'Direccion')
allclientes = [(p.codigo, p.descripcion, p.precio_mercado, p.grupo_suministros) for p in Producto.objects.all()]
t = Table([headings] + allclientes)
t.setStyle(TableStyle(
[
('GRID', (0, 0), (3, -1), 1, colors.dodgerblue),
('LINEBELOW', (0, 0), (-1, 0), 2, colors.darkblue),
('BACKGROUND', (0, 0), (-1, 0), colors.dodgerblue)
]
))
clientes.append(t)
doc.build(clientes)
response.write(buff.getvalue())
buff.close()
return response
def leftTop(self, e):
if e is None:
return ''
return reportlab.platypus.Paragraph(
'<font face="%s" size=%d>%s</font><br/>' %
(FONT_FAMILY, FONT_SIZE['header'], ExportDocument.header(
e, self.Lang)), self.style)
def leftBottom(self, e):
story = []
if e is None:
return story
b = model.Board()
if 'algebraic' in e:
b.fromAlgebraic(e['algebraic'])
x = unicode(self.board2Html(b).decode("ISO-8859-1"))
story.append(
BorderedParagraph(
'<para autoLeading="max">' +
x +
'</para>',
self.style))
s_left = ''
if 'stipulation' in e:
s_left = e['stipulation']
story.append(self.subscript(s_left, b.getPiecesCount()))
story.append(
reportlab.platypus.Paragraph(
'<font face="%s" size=%d>%s</font>' %
(FONT_FAMILY,
FONT_SIZE['footer'],
ExportDocument.solver(
e,
self.Lang) +
'<br/>' +
ExportDocument.legend(b)),
self.style))
return story