本文整理汇总了Python中reportlab.pdfgen.canvas.Canvas.setFontSize方法的典型用法代码示例。如果您正苦于以下问题:Python Canvas.setFontSize方法的具体用法?Python Canvas.setFontSize怎么用?Python Canvas.setFontSize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reportlab.pdfgen.canvas.Canvas
的用法示例。
在下文中一共展示了Canvas.setFontSize方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFontSize [as 别名]
def run(rfc_list= ["1111","22222"], pdf_file="barcode.pdf"):
try:
os.remove(pdf_file)
except:
pass
c = Canvas(pdf_file)
c.setFontSize(size="7")
print " %s pag para %s rfcs " % (round(len(rfc_list)/114.0,0), len(rfc_list))
for times in range(0,int(round(len(rfc_list)/114.0,0))):
for i in range(0,6):
for j in range(1,20):
st = code128.Code128()
if len(rfc_list) >0 :
rfc = rfc_list.pop()
else:
c.save()
sys.exit()
st.value = rfc
pos_x = i*30*mm
pos_y = j*15*mm
#print pos_x/mm,pos_y/mm
st.drawOn(c, x = pos_x, y = pos_y)
c.drawString(pos_x+10*mm, pos_y+7*mm , rfc )
c.showPage()
c.setFontSize(size="7")
try:
f = open(pdf_file, "wb")
except IOError:
easygui.msgbox("El archivo pdf esta abierto, por lo que no se puede guardar", "Error")
sys.exit(0)
c.save()
示例2: InvoiceTemplate
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFontSize [as 别名]
class InvoiceTemplate(object):
def __init__(self, filename, logo_filename=LOGO_FILENAME,
from_address=Address(**settings.INVOICE_FROM_ADDRESS),
to_address=None, project_name='',
invoice_date=None, invoice_number='',
terms=settings.INVOICE_TERMS,
due_date=None, date_start=None, date_end=None,
bank_name=settings.BANK_NAME,
bank_address=Address(**settings.BANK_ADDRESS),
account_number=settings.BANK_ACCOUNT_NUMBER,
routing_number=settings.BANK_ROUTING_NUMBER,
swift_code=settings.BANK_SWIFT_CODE,
applied_credit=None,
subtotal=None, tax_rate=None, applied_tax=None, total=None):
self.canvas = Canvas(filename)
self.canvas.setFontSize(DEFAULT_FONT_SIZE)
self.logo_filename = os.path.join(os.getcwd(), logo_filename)
self.from_address = from_address
self.to_address = to_address
self.project_name = project_name
self.invoice_date = invoice_date
self.invoice_number = invoice_number
self.terms = terms
self.due_date = due_date
self.date_start = date_start
self.date_end = date_end
self.bank_name = bank_name
self.bank_address = bank_address
self.account_number = account_number
self.routing_number = routing_number
self.swift_code = swift_code
self.applied_credit = applied_credit
self.subtotal = subtotal
self.tax_rate = tax_rate
self.applied_tax = applied_tax
self.total = total
self.items = []
def add_item(self, description, quantity, unit_cost, subtotal, credits, total):
self.items.append(PdfLineItem(description, quantity, unit_cost,
subtotal, credits, total))
def get_pdf(self):
self.draw_logo()
self.draw_from_address()
self.draw_to_address()
self.draw_project_name()
self.draw_statement_period()
self.draw_invoice_label()
self.draw_details()
self.draw_table()
self.draw_footer()
self.canvas.showPage()
self.canvas.save()
def draw_logo(self):
self.canvas.drawImage(self.logo_filename, inches(0.5), inches(2.5),
width=inches(1.5), preserveAspectRatio=True)
def draw_text(self, string, x, y):
text = self.canvas.beginText()
text.setTextOrigin(x, y)
for line in string.split('\n'):
text.textLine(line)
self.canvas.drawText(text)
def draw_from_address(self):
if self.from_address is not None:
self.draw_text(unicode(self.from_address), inches(3), inches(11))
def draw_to_address(self):
origin_x = inches(1)
origin_y = inches(9.2)
self.canvas.translate(origin_x, origin_y)
left = inches(0)
right = inches(4.5)
top = inches(0.3)
middle_horizational = inches(0)
bottom = inches(-1.7)
self.canvas.rect(left, bottom, right - left, top - bottom)
self.canvas.setFillColorRGB(*LIGHT_GRAY)
self.canvas.rect(left, middle_horizational, right - left,
top - middle_horizational, fill=1)
self.canvas.setFillColorRGB(*BLACK)
self.draw_text("Bill To", left + inches(0.2),
middle_horizational + inches(0.1))
if self.to_address is not None:
self.draw_text(unicode(self.to_address), inches(0.1), inches(-0.2))
self.canvas.translate(-origin_x, -origin_y)
def draw_project_name(self):
origin_x = inches(1)
origin_y = inches(7.4)
#.........这里部分代码省略.........
示例3: InvoiceTemplate
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFontSize [as 别名]
class InvoiceTemplate(object):
def __init__(self, filename, logo_filename=LOGO_FILENAME,
from_address=Address(**settings.INVOICE_FROM_ADDRESS),
to_address=None, project_name='',
invoice_date=None, invoice_number='',
terms=settings.INVOICE_TERMS,
due_date=None, date_start=None, date_end=None,
bank_name=settings.BANK_NAME,
bank_address=Address(**settings.BANK_ADDRESS),
account_number=settings.BANK_ACCOUNT_NUMBER,
routing_number_ach=settings.BANK_ROUTING_NUMBER_ACH,
routing_number_wire=settings.BANK_ROUTING_NUMBER_WIRE,
swift_code=settings.BANK_SWIFT_CODE,
applied_credit=None,
subtotal=None, tax_rate=None, applied_tax=None, total=None,
is_wire=False, is_prepayment=False):
self.canvas = Canvas(filename)
self.canvas.setFontSize(DEFAULT_FONT_SIZE)
self.logo_filename = os.path.join(os.getcwd(), logo_filename)
self.from_address = from_address
self.to_address = to_address
self.project_name = project_name
self.invoice_date = invoice_date
self.invoice_number = invoice_number
self.terms = terms
self.due_date = due_date
self.date_start = date_start
self.date_end = date_end
self.bank_name = bank_name
self.bank_address = bank_address
self.account_number = account_number
self.routing_number_ach = routing_number_ach
self.routing_number_wire = routing_number_wire
self.swift_code = swift_code
self.applied_credit = applied_credit
self.subtotal = subtotal
self.tax_rate = tax_rate
self.applied_tax = applied_tax
self.total = total
self.is_wire = is_wire
self.is_prepayment = is_prepayment
self.items = []
def add_item(self, description, quantity, unit_cost, subtotal, credits, total):
self.items.append(PdfLineItem(description, quantity, unit_cost,
subtotal, credits, total))
def get_pdf(self):
self.draw_logo()
self.draw_from_address()
self.draw_to_address()
self.draw_project_name()
if not self.is_prepayment:
self.draw_statement_period()
self.draw_invoice_label()
self.draw_details()
if not self.is_wire or self.is_prepayment:
self.draw_table()
self.draw_footer()
self.canvas.showPage()
self.canvas.save()
def draw_logo(self):
self.canvas.drawImage(self.logo_filename, inches(0.5), inches(2.5),
width=inches(1.5), preserveAspectRatio=True)
def draw_text(self, string, x, y):
text = self.canvas.beginText()
text.setTextOrigin(x, y)
for line in string.split('\n'):
text.textLine(line)
self.canvas.drawText(text)
def draw_from_address(self):
if self.from_address is not None:
self.draw_text(unicode(self.from_address), inches(3), inches(11))
def draw_to_address(self):
origin_x = inches(1)
origin_y = inches(9.2)
self.canvas.translate(origin_x, origin_y)
left = inches(0)
right = inches(4.5)
top = inches(0.3)
middle_horizational = inches(0)
bottom = inches(-1.7)
self.canvas.rect(left, bottom, right - left, top - bottom)
self.canvas.setFillColorRGB(*LIGHT_GRAY)
self.canvas.rect(left, middle_horizational, right - left,
top - middle_horizational, fill=1)
self.canvas.setFillColorRGB(*BLACK)
self.draw_text("Bill To", left + inches(0.2),
middle_horizational + inches(0.1))
#.........这里部分代码省略.........
示例4: Canvas
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFontSize [as 别名]
The pdfgen API allows you to draw text, shapes and images directly to a canvas
object. The canvas object <i>is</i> your PDF.
All drawing operations are performed by calling various methods of the canvas
object.
"""
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.units import mm
# create a Canvas object. This is your PDF.
c = Canvas("03_02_low_level_pdf_operations.pdf", pagesize=(210*mm, 297*mm))
# The canvas is very stateful. Here we adjust the font size. This will affect
# all further text until a new font size is set. (If font size is not set it
# will default to 12pt. A bit small for this demo.)
c.setFontSize(60)
# The native unit of measurement in PDFs is the postscript point (The unit of
# measurement most often used for defining font sizes.) To use measurements in
# mm, cm, inch or pica, simply multiply the value by the appropriate unit from
# reportlab.lib.units. This converts the measurement to points.
width, height = 210*mm, 297*mm
# When placing objects on the page keep in mind that by default, measurements
# are taken from the lower left-hand corner of the page.
c.drawCentredString(width / 2, height / 2, "Hello World")
c.showPage() # Finish page 1
# Any extra drawing commands entered here would appear on page 2!
示例5: Canvas
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFontSize [as 别名]
state stack.
There are also a few other canvas states which get saved to the state stack by
this operation, such as fill colour, stroke colour, and font face.
"""
from os.path import join
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.units import mm, cm
c = Canvas("04_03_low_level_pdf_operations_II.pdf", pagesize=(210*mm, 297*mm))
text_file = open("04_02_The_Raven.txt")
text = (l.strip() for l in text_file if l.strip())
c.setFontSize(10)
leading = 14 # line spacing.
c.saveState()
# translate halfway across page
c.translate(105*mm, 0)
# draw a light grey vertical line
c.setStrokeColorRGB(255, 0, 0)
c.line(0, 0, 0, 297*mm)
# move up the page
c.translate(0, 260*mm)
c.drawString(-100*mm, 0, "Canvas.drawString")
示例6: create_pdf
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFontSize [as 别名]
def create_pdf(hocr, filename, font="Courier", author=None, keywords=None, subject=None, title=None, image_path=None, draft=False):
""" transform hOCR information into a searchable PDF.
@param hocr the hocr structure as coming from extract_hocr.
@param filename the name of the PDF generated in output.
@param font the default font (e.g. Courier, Times-Roman).
@param author the author name.
@param subject the subject of the document.
@param title the title of the document.
@param image_path the default path where images are stored. If not specified
relative image paths will be resolved to the current directory.
@param draft whether to enable debug information in the output.
"""
def adjust_image_size(width, height):
return max(width / A4[0], height / A4[1])
canvas = Canvas(filename)
if author:
canvas.setAuthor(author)
if keywords:
canvas.setKeywords(keywords)
if title:
canvas.setTitle(title)
if subject:
canvas.setSubject(subject)
for bbox, image, lines in hocr:
if not image.startswith('/') and image_path:
image = os.path.abspath(os.path.join(image_path, image))
img_width, img_height = bbox[2:]
ratio = adjust_image_size(img_width, img_height)
if draft:
canvas.drawImage(image, 0, A4[1] - img_height / ratio , img_width / ratio, img_height / ratio)
canvas.setFont(font, 12)
for bbox, line in lines:
if draft:
canvas.setFillColor(red)
x0, y0, x1, y1 = bbox
width = (x1 - x0) / ratio
height = ((y1 - y0) / ratio)
x0 = x0 / ratio
#for ch in 'gjpqy,(){}[];[email protected]':
#if ch in line:
#y0 = A4[1] - (y0 / ratio) - height
#break
#else:
y0 = A4[1] - (y0 / ratio) - height / 1.3
#canvas.setFontSize(height * 1.5)
canvas.setFontSize(height)
text_width = canvas.stringWidth(line)
if text_width:
## If text_width != 0
text_object = canvas.beginText(x0, y0)
text_object.setHorizScale(1.0 * width / text_width * 100)
text_object.textOut(line)
canvas.drawText(text_object)
else:
info('%s, %s has width 0' % (bbox, line))
if draft:
canvas.setStrokeColor(green)
canvas.rect(x0, y0, width, height)
if draft:
canvas.circle(0, 0, 10, fill=1)
canvas.circle(0, A4[1], 10, fill=1)
canvas.circle(A4[0], 0, 10, fill=1)
canvas.circle(A4[0], A4[1], 10, fill=1)
canvas.setFillColor(green)
canvas.setStrokeColor(green)
canvas.circle(0, A4[1] - img_height / ratio, 5, fill=1)
canvas.circle(img_width / ratio, img_height /ratio, 5, fill=1)
else:
canvas.drawImage(image, 0, A4[1] - img_height / ratio , img_width / ratio, img_height / ratio)
canvas.save()