本文整理汇总了Python中reportlab.pdfgen.canvas.Canvas.setFillColor方法的典型用法代码示例。如果您正苦于以下问题:Python Canvas.setFillColor方法的具体用法?Python Canvas.setFillColor怎么用?Python Canvas.setFillColor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reportlab.pdfgen.canvas.Canvas
的用法示例。
在下文中一共展示了Canvas.setFillColor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFillColor [as 别名]
class AwardPrinter:
def __init__(self, output_filename, background_image, page_renderer):
self.background_image = background_image
self.page_renderer = page_renderer
self.pdf = Canvas(output_filename, pagesize = A4)
def draw(self, students):
for student in students:
self._draw_page(student)
self.pdf.save()
def _draw_page(self, student):
self.pdf.setFillColor(colors.black)
# export the image as a higter resolution image 1280x920 recommended, which is then reduced
# in size to have a higher resolution for printing
self.pdf.drawImage(self.background_image, .1 * inch, .3 * inch, width=580, height=800, preserveAspectRatio=True)
self.pdf.rotate(270)
self.page_renderer(self, student)
self.pdf.showPage()
def _draw_award(self, student):
name = student.split(',')[0].strip()
award = student.split(',')[1].strip()
self.pdf.setFont("Helvetica", 28)
# play with these dimensions if you want to move around the text in the screen
self.pdf.drawCentredString(-5.4 * inch, 4.5 * inch, name.encode('latin-1'))
self.pdf.setFont("Helvetica", 18)
# play with these dimensions if you want to move around the text in the screen
self.pdf.drawCentredString(-5.4 * inch, 3.5 * inch, award)
def _draw_certificate(self, student):
name = student.split(',')[0].strip()
self.pdf.setFont("Helvetica", 32)
# play with these dimensions if you want to move around the text in the screen
self.pdf.drawCentredString(-5.75 * inch, 5.5 * inch, name.encode('latin-1'))
示例2: test_06_fontsize
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFillColor [as 别名]
def test_06_fontsize(self):
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.units import inch
from reportlab.lib.colors import red, magenta
c = Canvas('demo.pdf', pagesize=A4)
c.translate(inch, inch)
c.setFont("Times-Roman", 20)
c.setFillColor(red)
c.saveState()
c.drawCentredString(2.75*inch, 2.5*inch,"Font size excmples")
c.setFillColor(magenta)
size = 7
x = 2.3 * inch
y = 1.3 * inch
for line in range(7):
c.setFont("Helvetica", size)
c.drawRightString(x, y, "%s points" % size)
c.drawString(x,y, "test")
y = y-size*1.2
size = size+1.5
c.restoreState()
c.drawString(0,0, "%s" % c.getAvailableFonts())
c.showPage()
c.save()
示例3: SquareTest
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFillColor [as 别名]
class SquareTest(unittest.TestCase):
""" Construct and draw image with ColorSpiral colours placed in a
square, with jitter
"""
def setUp(self):
""" Set up canvas for drawing"""
output_filename = os.path.join("Graphics", "square_test.pdf")
self.c = Canvas(output_filename, pagesize=(500, 500))
def test_colorspiral(self):
""" Set of 625 colours, with jitter, using get_colors()."""
boxedge = 20
boxes_per_row = 25
rows = 0
for i, c in enumerate(get_colors(625)):
self.c.setFillColor(c)
x1 = boxedge * (i % boxes_per_row)
y1 = rows * boxedge
self.c.rect(x1, y1, boxedge, boxedge, fill=1, stroke=0)
if not (i+1) % boxes_per_row:
rows += 1
self.finish()
def finish(self):
""" Clean up and save image."""
self.c.save()
示例4: generateEntrancePDF
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFillColor [as 别名]
def generateEntrancePDF(self,response):
#Attach name.pdf file to responses content disposition
response['Content-Disposition'] = 'attachment; filename=entrance.pdf'
#Create empty pdf document, hook pdf with response
pdf = Canvas(response)
pdf.setFillColor(colors.black) #sets Line/Rectangle Colors
pdf.roundRect(10, 755, 575, 75, 10, 1, 0)
pdf.setFont("Helvetica-Bold", 40)
pdf.setStrokeColorRGB(0, 0, 0) #sets Line/Rectangle Colors
pdf.drawCentredString(300, 790, "GroupFinder")
pdf.setFont("Helvetica-Bold", 20)
pdf.drawString(15, 765, "The following spaces are reserved during scheduled hours")
pdf.drawCentredString(300,725, datetime.datetime.now().strftime("%A, %B %d, %Y"))
#Get Todays Events
brains = sorted(util.gatherTodaysEvents(self), key=attrgetter('start','Title'))
index = 700
i = 0
for brain in brains:
pdf.rect(45, index-30, 510, 42, stroke=1, fill=0) #Schedule List Rectangles
if util.isPublic(self,brain.id):
title = brain.Title
else:
title = "Private Group"
pdf.setFont("Helvetica-Bold", 17)
pdf.drawString(50, index-5, DateTime(brain.start).strftime("%I:%M %p").lower() +
" - " + DateTime(brain.end).strftime("%I:%M %p").lower() +
" : " + title)
pdf.setFont("Helvetica", 17)
l = self.locationLookup(brain.location)
pdf.drawString(50, index-25, "Location: " + l['Name'] + " - " + l['DirectionsShort'])
index -= 42
i += 1
if i == 13:
pdf.setFont("Helvetica", 17)
pdf.drawCentredString(300, index-5, "See Website For More Study Groups!")
break
pdf.setFont("Helvetica-Bold", 28)
pdf.drawCentredString(300, 90, "Use GroupFinder to Reserve a Study Space.")
pdf.setFont("Helvetica", 24)
pdf.drawCentredString(300, 60, "http://www.uwosh.edu/library/groupfinder")
pdf = self.tableFooter(pdf)
pdf.showPage() #next page, finalize last page.
pdf.save() #save the pdf content
return response #return response with hooked pdf.
示例5: generateNumberedPages
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFillColor [as 别名]
def generateNumberedPages(numPages, pageSize, orientation, bgColor, outPath):
"Generate a 10 page document with one big number per page."
if orientation == "landscape":
pageSize = landscape(pageSize)
canv = Canvas(outPath, pagesize=pageSize)
for i in range(numPages):
canv.setFont("Helvetica", 500)
text = u"%s" % i
if i % 2 == 0:
canv.setStrokeColor(bgColor)
canv.setFillColor(bgColor)
canv.rect(0, 0, pageSize[0], pageSize[1], stroke=True, fill=True)
canv.setFillColor(black)
elif i % 2 == 1:
canv.setStrokeColor(black)
canv.setFillColor(black)
canv.rect(0, 0, pageSize[0], pageSize[1], stroke=True, fill=True)
canv.setFillColor(bgColor)
if orientation == "portrait":
canv.drawCentredString(pageSize[0]/2.0, pageSize[1]*0.3, u"%s" % i)
elif orientation == "landscape":
canv.drawCentredString(pageSize[0]/2.0, pageSize[1]*0.21, u"%s" % i)
canv.showPage()
canv.save()
示例6: test0
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFillColor [as 别名]
def test0(self):
"This should open in full screen mode."
import os
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.colors import PCMYKColor, PCMYKColorSep
filename = 'test_pdfgen_overprint.pdf'
desc = "Overprint/knockout tests for ReportLab library"
black = PCMYKColor(0,0,0,100)
cyan = PCMYKColorSep(100,0,0,0,spotName='myCyan')
magenta = PCMYKColorSep(0,100,0,0,spotName='myMagenta')
c = Canvas(filename)
c.setFillColor(black)
c.setFont('Helvetica', 20)
c.drawString(100, 700, desc)
c.setFont('Helvetica', 10)
c.drawString(100, 670, "To view is page properly you probably need to enable 'overprint preview' in Acrobat Reader")
c.drawString(100, 658, "or use a tool like Illustrator, Quark or Acrobat to view separated plates. Starting in")
c.drawString(100, 646, "Acrobat Reader 9 there is a setting that lets you turn on the overprint preview, although")
c.drawString(100, 634, "it's not on by default (Preferences > Page Display > Use Overprint Preview: Always).")
c.drawString(100, 616, "In the top example, the magenta rectangle overprints the cyan one. In the lower one, it")
c.drawString(100, 604, "'knocks out' the cyan underneath which is the default in PDF. This means that the overlap")
c.drawString(100, 592, "area of the first example should show blue, because the two colours merge. However, in many")
c.drawString(100, 580, "PDF viewers and cheap printers, both examples will probably look the same - magenta")
c.drawString(100, 568, "rectangle knocks out part of the cyan one.")
c.drawString(100, 550, "If you can view the separated CMYK plates in a tool like Illustrator, on the cyan plate")
c.drawString(100, 538, "you should see the top rectangle as complete and the bottom one has a chunk knocked out of")
c.drawString(100, 526, "the top right corner.")
c.setFillOverprint(True)
c.setFillColor(cyan)
c.rect(100, 300, 200, 100, fill=True, stroke=False)
c.setFillColor(magenta)
c.rect(200, 350, 200, 100, fill=True, stroke=False)
c.setFillOverprint(False)
c.setFillColor(cyan)
c.rect(100, 100, 200, 100, fill=True, stroke=False)
c.setFillColor(magenta)
c.rect(200, 150, 200, 100, fill=True, stroke=False)
c.save()
assert os.path.exists(filename)
示例7: _build_omr_layer
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFillColor [as 别名]
def _build_omr_layer(marks):
padding_x = 4.2 * mm
padding_y = 8.5 * mm
top_mark_x = 7 * mm
top_mark_y = 220 * mm
mark_y_spacing = 4 * mm
mark_width = 6.5 * mm
marks_height = (len(marks) - 1) * mark_y_spacing
logger.info('Mailer DS-75i OMR Settings: 1={} 2={}'.format(
(297 * mm - top_mark_y) / mm,
(top_mark_x + mark_width / 2) / mm + 0.5
))
omr_buffer = StringIO.StringIO()
omr_canvas = Canvas(omr_buffer)
omr_canvas.setLineWidth(0.2 * mm)
# add a white background for the omr code
omr_canvas.setFillColor(white)
omr_canvas.rect(
x=top_mark_x - padding_x,
y=top_mark_y - marks_height - padding_y,
width=mark_width + 2 * padding_x,
height=marks_height + 2 * padding_y,
fill=True,
stroke=False
)
for offset, mark in enumerate(marks):
mark_y = top_mark_y - offset * mark_y_spacing
if mark:
omr_canvas.line(top_mark_x, mark_y,
top_mark_x + mark_width, mark_y)
# Close the PDF object cleanly.
omr_canvas.showPage()
omr_canvas.save()
# move to the beginning of the StringIO buffer
omr_buffer.seek(0)
omr_pdf = PdfFileReader(omr_buffer)
return omr_pdf.getPage(0)
示例8: __init__
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFillColor [as 别名]
class BadgePrinter:
def __init__(self, registration_list, filename='Badges_rendered.pdf'):
self.registration_list = registration_list
self.pdf = Canvas(filename, pagesize = letter)
def drawBadges(self):
for six_registrants in self._chunkRegistrantsIntoSixes():
self._drawOnePage(six_registrants)
self.pdf.save()
def _drawOnePage(self, registrants):
left_column_x = 2.25
right_column_x = 6.25
top_y = 9
middle_y = 6
bottom_y = 3
i = 0
for x in [left_column_x, right_column_x]:
for y in [top_y, middle_y, bottom_y]:
if i < len(registrants):
self._drawOneNameBadge(x, y, registrants[i])
i += 1
self.pdf.showPage()
def _drawOneNameBadge(self, x, y, registrant):
self.pdf.setFillColor(colors.black)
# self.pdf.setFont("Helvetica", 18)
# self.pdf.drawImage("testing_logo.jpg", (x + 1.1) * inch, (y - .75) * inch)
self.pdf.setFont("Helvetica", 19)
self.pdf.drawCentredString(x * inch, y * inch, str(registrant.full_name.encode('latin-1')))
self.pdf.setFont("Helvetica", 12)
self.pdf.drawCentredString(x * inch, (y - .25) * inch, str(registrant.home_office))
self.pdf.setFont("Helvetica", 8)
self.pdf.drawCentredString(x * inch, (y - .5) * inch, str(registrant.job_role))
self.pdf.setFont("Helvetica", 6)
self.pdf.setFillColor(colors.green)
self.pdf.drawCentredString(x * inch, (y - .7) * inch, 'Test Engineering NYC Summit 2008')
def _chunkRegistrantsIntoSixes(self):
chunked = []
for i in range(0, len(self.registration_list), 6):
chunked.append(self.registration_list[i:i + 6])
return chunked
开发者ID:jawspeak,项目名称:google-docs-spreadsheet-namebadge-printer,代码行数:45,代码来源:spreadsheet_badge_printer.py
示例9: test_05_coordinates
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFillColor [as 别名]
def test_05_coordinates(self):
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.units import inch
from reportlab.lib.colors import pink, black, red, blue, green
c = Canvas('demo.pdf', pagesize=A4)
c.translate(inch,inch)
c.setStrokeColor(pink)
c.grid([1*inch,2*inch,3*inch,4*inch],[0.5*inch, 1*inch, .5*inch, 2*inch, 2.5*inch])
c.setFont("Times-Roman", 20)
c.drawString(0,0, "(0,0) the Origin")
c.drawString(2.5*inch, 1*inch, "(2.5,1) in inches")
c.drawString(4*inch, 2.5*inch, "(4,2.5)")
c.setFillColor(red)
c.rect(0,2*inch,0.2*inch, 0.3*inch, fill=1)
c.setFillColor(green)
c.circle(4.5*inch, 0.4*inch, 0.2*inch, fill=1)
c.showPage()
c.save()
示例10: __init__
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFillColor [as 别名]
class DesignShirtsFromCalifornia:
CHICAGO_HEADLINE=["ThoughtWorks Chicago", "Bringing it since 1992", "CHICAGO", "Rainey's People"]
ADJECTIVES=["Awesomist", "Best looking", "Happiest", "Legendary", "Transparent", "Shiny", "Dangerous", "Fastest"]
NOUNS=["Chicagoans", "Agilistas", "Wonderlic Masters", "Bears Fans"]
ATTRIBUTION="Designed by ThoughtWorks in California"
GIT_REF="Fork me on Github https://github.com/jawspeak/tw-california-tshirt-generator"
def __init__(self, output_filename):
self.pdf = Canvas(output_filename, pagesize = LETTER)
# self.headlines =
def draw(self):
for image in glob.glob('awesomeness/*.[jpg|png|gif]*'):
self._draw_page(image)
self.pdf.save()
def _draw_page(self, image):
self.pdf.setFillColor(colors.black)
# To have a higher resolution for printing export the image as a higter resolution image which is then reduced
self.pdf.drawImage(image, 2.25 * inch, 3.7 * inch, width=300, height=300, preserveAspectRatio=True)
height = 8
# print self.pdf.getAvailableFonts()
self.pdf.setFont("Times-Bold", 28)
random.shuffle(self.CHICAGO_HEADLINE, random.random)
self.pdf.drawCentredString(4.25* inch, height*inch, self.CHICAGO_HEADLINE[0].encode('latin-1'))
self.pdf.setFont("Times-Italic", 16)
if len(image.split('=')) == 2:
self.pdf.drawCentredString(4.25* inch, (height - 4.5)* inch, image.split('=')[1].split('.')[0].encode('latin-1'))
else:
random.shuffle(self.ADJECTIVES, random.random)
random.shuffle(self.NOUNS, random.random)
flattering_tagline = "Home of the %s %s!" % (', '.join(self.ADJECTIVES[:2]), self.NOUNS[0])
self.pdf.drawCentredString(4.25* inch, (height - 4.5)* inch, flattering_tagline.encode('latin-1'))
self.pdf.setFont("Times-Italic", 10)
self.pdf.drawCentredString(4.25 * inch, (height - 4.8) * inch, self.ATTRIBUTION)
self.pdf.drawCentredString(4.25 * inch, (height - 4.95) * inch, self.GIT_REF)
self.pdf.showPage()
示例11: SpiralTest
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFillColor [as 别名]
class SpiralTest(unittest.TestCase):
""" Construct and draw image with ColorSpiral colours placed on the
HSV spiral
"""
def setUp(self):
""" Set up canvas for drawing"""
output_filename = os.path.join("Graphics", "spiral_test.pdf")
self.c = Canvas(output_filename, pagesize=A4)
# co-ordinates of the centre of the canvas
self.x_0, self.y_0 = 0.5 * A4[0], 0.5 * A4[1]
def test_colorlist(self):
""" Get set of eight colours, no jitter, using ColorSpiral."""
cs = ColorSpiral(a=4, b=0.33, jitter=0)
colours = list(cs.get_colors(8))
cstr = ["(%.2f, %.2f, %.2f)" % (r, g, b)
for r, g, b in colours]
expected = \
['(0.64, 0.74, 0.81)', '(0.68, 0.52, 0.76)', '(0.72, 0.41, 0.55)',
'(0.68, 0.39, 0.31)', '(0.63, 0.54, 0.22)', '(0.48, 0.59, 0.13)',
'(0.24, 0.54, 0.06)', '(0.01, 0.50, -0.00)']
self.assertEqual(cstr, expected)
def test_colorspiral(self):
""" Get set of 16 colours, no jitter, using ColorSpiral."""
cs = ColorSpiral(a=4, b=0.33, jitter=0)
radius = A4[0] * 0.025
for r, g, b in cs.get_colors(16):
self.c.setFillColor((r, g, b))
# Convert HSV colour to rectangular coordinates on HSV disc
h, s, v = colorsys.rgb_to_hsv(r, g, b)
coords = cmath.rect(s * A4[0] * 0.45, h * 2 * pi)
x, y = self.x_0 + coords.real, self.y_0 + coords.imag
self.c.ellipse(x - radius, y - radius, x + radius, y + radius,
stroke=0, fill=1)
self.finish()
def finish(self):
""" Clean up and save image."""
self.c.save()
示例12: genTestFile
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFillColor [as 别名]
def genTestFile(path, numPages):
"Generate a PDF doc with *very* big page numbers on all pages."
size = landscape(A4)
canv = Canvas(path, pagesize=size)
for i in range(numPages):
canv.setFont("Helvetica", size[1]*1.2)
x, y = size[0]/2.0, size[1]*0.1
text = u"%s" % i
if i % 2 == 1:
canv.setStrokeColor(black)
canv.setFillColor(black)
canv.rect(0, 0, size[0], size[1], fill=True)
if i % 2 == 1:
canv.setFillColor(white)
else:
canv.setFillColor(black)
canv.drawCentredString(x, y, text)
canv.showPage()
canv.save()
示例13: create_pdf
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFillColor [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()
示例14: test0
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFillColor [as 别名]
def test0(self):
"A basic document drawing some strings"
# if they do not have the Japanese font files, go away quietly
try:
from reportlab.pdfbase.cidfonts import CIDFont, findCMapFile
findCMapFile("90ms-RKSJ-H")
findCMapFile("90msp-RKSJ-H")
findCMapFile("UniJIS-UCS2-H")
findCMapFile("EUC-H")
except:
# don't have the font pack. return silently
return
pdfmetrics.registerFont(CIDFont("HeiseiMin-W3", "90ms-RKSJ-H"))
pdfmetrics.registerFont(CIDFont("HeiseiKakuGo-W5", "90ms-RKSJ-H"))
c = Canvas(outputfile("test_multibyte_jpn.pdf"))
c.setFont("Helvetica", 30)
c.drawString(100, 700, "Japanese Font Support")
c.setStrokeColor(colors.red)
# the two typefaces
c.setFont("HeiseiMin-W3-90ms-RKSJ-H", 16)
# this says "This is HeiseiMincho" in shift-JIS. Not all our readers
# have a Japanese PC, so I escaped it. On a Japanese-capable
# system, print the string to see Kanji
message1 = "\202\261\202\352\202\315\225\275\220\254\226\276\222\251\202\305\202\267\201B"
c.drawString(100, 675, message1)
wid = pdfmetrics.stringWidth(message1, "HeiseiMin-W3-90ms-RKSJ-H", 16)
c.rect(100, 675, wid, 16, stroke=1, fill=0)
c.setFont("HeiseiKakuGo-W5-90ms-RKSJ-H", 16)
# this says "This is HeiseiKakugo" in shift-JIS
message2 = "\202\261\202\352\202\315\225\275\220\254\212p\203S\203V\203b\203N\202\305\202\267\201B"
c.drawString(100, 650, message2)
wid = pdfmetrics.stringWidth(message2, "HeiseiKakuGo-W5-90ms-RKSJ-H", 16)
c.rect(100, 650, wid, 16, stroke=1, fill=0)
self.hDraw(c, "\223\214\213\236 says Tokyo in Shift-JIS", "HeiseiMin-W3-90ms-RKSJ-H", 100, 600)
pdfmetrics.registerFont(CIDFont("HeiseiMin-W3", "90msp-RKSJ-H"))
self.hDraw(c, "\223\214\213\236, but in proportional Shift-JIS.", "HeiseiMin-W3-90msp-RKSJ-H", 100, 575)
pdfmetrics.registerFont(CIDFont("HeiseiMin-W3", "EUC-H"))
self.hDraw(c, "\xC5\xEC\xB5\xFE says Tokyo in EUC", "HeiseiMin-W3-EUC-H", 100, 550)
if 0:
# this is super-slow until we do encoding caching.
pdfmetrics.registerFont(CIDFont("HeiseiMin-W3", "UniJIS-UCS2-H"))
def asciiToUCS2(text):
s = ""
for ch in text:
s = s + chr(0) + ch
return s
self.hDraw(
c, "\x67\x71\x4E\xAC" + asciiToUCS2(" says Tokyo in UCS2"), "HeiseiMin-W3-UniJIS-UCS2-H", 100, 525
)
# now try verticals
pdfmetrics.registerFont(CIDFont("HeiseiMin-W3", "90ms-RKSJ-V"))
c.setFont("HeiseiMin-W3-90ms-RKSJ-V", 16)
c.drawString(400, 650, "\223\214\213\236 vertical Shift-JIS")
height = c.stringWidth("\223\214\213\236 vertical Shift-JIS", "HeiseiMin-W3-90ms-RKSJ-V", 16)
c.rect(400 - 8, 650, 16, -height)
pdfmetrics.registerFont(CIDFont("HeiseiMin-W3", "EUC-V"))
c.setFont("HeiseiMin-W3-EUC-V", 16)
c.drawString(425, 650, "\xC5\xEC\xB5\xFE vertical EUC")
height = c.stringWidth("\xC5\xEC\xB5\xFE vertical EUC", "HeiseiMin-W3-EUC-V", 16)
c.rect(425 - 8, 650, 16, -height)
c.setFillColor(colors.purple)
tx = c.beginText(100, 250)
tx.setFont("Helvetica", 12)
tx.textLines(
"""This document shows sample output in Japanese
from the Reportlab PDF library. This page shows the two fonts
available and tests our ability to measure the width of glyphs
in both horizontal and vertical writing, with proportional and
fixed-width characters. The red boxes should be the same width
(or height) as the character strings they surround.
The next pages show more samples and information.
"""
)
c.drawText(tx)
c.setFont("Helvetica", 10)
c.drawCentredString(297, 36, "Page %d" % c.getPageNumber())
c.showPage()
# realistic text sample
sample = """Adobe Acrobat
\x83h\x83L\x83\x85\x83\x81\x83\x93\x83g\x82\xaa\x8aJ\x82\xa9\x82\xc8\x82\xad\x82\xc4\x8d\xa2\x82\xc1\x82\xbd\x82\xb1\x82\xc6\x82\xcd
\x82\xa0\x82\xe8\x82\xdc\x82\xb9\x82\xf1\x82\xa9\x81B\x8e\x96\x8b\xc6\x8cv\x89\xe6\x8f\x91\x81A\x89c\x8b\xc6\x83\x8c\x83|\x81[\x83g
\x81A\x83J\x83^\x83\x8d\x83O\x82\xe2\x83p\x83\x93\x83t\x83\x8c\x83b\x83g\x82\xc8\x82\xc7\x90\xa7\x8d\xec\x95\xa8\x82\xcc\x8e\xed
\x97\xde\x82\xc9\x82\xa9\x82\xa9\x82\xed\x82\xe7\x82\xb8\x81A
#.........这里部分代码省略.........
示例15: test0
# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import setFillColor [as 别名]
def test0(self):
"A basic document drawing some strings"
c = Canvas(outputfile('test_multibyte_jpn.pdf'))
c.setFont('Helvetica', 30)
c.drawString(100,700, 'Japanese Font Support')
c.setStrokeColor(colors.red)
#unicode font automatically supplies the encoding
pdfmetrics.registerFont(UnicodeCIDFont('HeiseiMin-W3'))
msg = u'\u6771\u4EAC : Unicode font, unicode input'
self.hDraw(c, msg, 'HeiseiMin-W3', 100, 600)
msg = u'\u6771\u4EAC : Unicode font, utf8 input'.encode('utf8')
self.hDraw(c, msg, 'HeiseiMin-W3', 100, 575)
# now try verticals - this is broken, not sure how to make it
# work in post Unicode world.
pdfmetrics.registerFont(CIDFont('HeiseiMin-W3','90ms-RKSJ-V'))
c.setFont('HeiseiMin-W3-90ms-RKSJ-V', 16)
c.drawString(450, 650, '\223\214\213\236 vertical Shift-JIS')
height = c.stringWidth('\223\214\213\236 vertical Shift-JIS', 'HeiseiMin-W3-90ms-RKSJ-V', 16)
c.rect(450-8,650,16,-height)
pdfmetrics.registerFont(CIDFont('HeiseiMin-W3','EUC-V'))
c.setFont('HeiseiMin-W3-EUC-V', 16)
c.drawString(475, 650, '\xC5\xEC\xB5\xFE vertical EUC')
height = c.stringWidth('\xC5\xEC\xB5\xFE vertical EUC', 'HeiseiMin-W3-EUC-V', 16)
c.rect(475-8,650,16,-height)
from reportlab.platypus.paragraph import Paragraph
from reportlab.lib.styles import ParagraphStyle
jStyle = ParagraphStyle('jtext',
fontName='HeiseiMin-W3',
fontSize=12,
wordWrap="CJK"
)
gatwickText = '\xe3\x82\xac\xe3\x83\x88\xe3\x82\xa6\xe3\x82\xa3\xe3\x83\x83\xe3\x82\xaf\xe7\xa9\xba\xe6\xb8\xaf\xe3\x81\xa8\xe9\x80\xa3\xe7\xb5\xa1\xe9\x80\x9a\xe8\xb7\xaf\xe3\x81\xa7\xe7\x9b\xb4\xe7\xb5\x90\xe3\x81\x95\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x84\xe3\x82\x8b\xe5\x94\xaf\xe4\xb8\x80\xe3\x81\xae\xe3\x83\x9b\xe3\x83\x86\xe3\x83\xab\xe3\x81\xa7\xe3\x81\x82\xe3\x82\x8b\xe5\xbd\x93\xe3\x83\x9b\xe3\x83\x86\xe3\x83\xab\xe3\x81\xaf\xe3\x80\x81\xe8\xa1\x97\xe3\x81\xae\xe4\xb8\xad\xe5\xbf\x83\xe9\x83\xa8\xe3\x81\x8b\xe3\x82\x8930\xe5\x88\x86\xe3\x81\xae\xe5\xa0\xb4\xe6\x89\x80\xe3\x81\xab\xe3\x81\x94\xe3\x81\x96\xe3\x81\x84\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82\xe5\x85\xa8\xe5\xae\xa2\xe5\xae\xa4\xe3\x81\xab\xe9\xab\x98\xe9\x80\x9f\xe3\x82\xa4\xe3\x83\xb3\xe3\x82\xbf\xe3\x83\xbc\xe3\x83\x8d\xe3\x83\x83\xe3\x83\x88\xe7\x92\xb0\xe5\xa2\x83\xe3\x82\x92\xe5\xae\x8c\xe5\x82\x99\xe3\x81\x97\xe3\x81\xa6\xe3\x81\x8a\xe3\x82\x8a\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82\xe3\x83\x95\xe3\x82\xa1\xe3\x83\x9f\xe3\x83\xaa\xe3\x83\xbc\xe3\x83\xab\xe3\x83\xbc\xe3\x83\xa0\xe3\x81\xaf5\xe5\x90\x8d\xe6\xa7\x98\xe3\x81\xbe\xe3\x81\xa7\xe3\x81\x8a\xe6\xb3\x8a\xe3\x82\x8a\xe3\x81\x84\xe3\x81\x9f\xe3\x81\xa0\xe3\x81\x91\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82\xe3\x81\xbe\xe3\x81\x9f\xe3\x80\x81\xe3\x82\xa8\xe3\x82\xb0\xe3\x82\xbc\xe3\x82\xaf\xe3\x83\x86\xe3\x82\xa3\xe3\x83\x96\xe3\x83\xab\xe3\x83\xbc\xe3\x83\xa0\xe3\x81\xae\xe3\x81\x8a\xe5\xae\xa2\xe6\xa7\x98\xe3\x81\xaf\xe3\x80\x81\xe3\x82\xa8\xe3\x82\xb0\xe3\x82\xbc\xe3\x82\xaf\xe3\x83\x86\xe3\x82\xa3\xe3\x83\x96\xe3\x83\xa9\xe3\x82\xa6\xe3\x83\xb3\xe3\x82\xb8\xe3\x82\x92\xe3\x81\x94\xe5\x88\xa9\xe7\x94\xa8\xe3\x81\x84\xe3\x81\x9f\xe3\x81\xa0\xe3\x81\x91\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82\xe4\xba\x8b\xe5\x89\x8d\xe3\x81\xab\xe3\x81\x94\xe4\xba\x88\xe7\xb4\x84\xe3\x81\x84\xe3\x81\x9f\xe3\x81\xa0\xe3\x81\x91\xe3\x82\x8b\xe3\x82\xbf\xe3\x82\xa4\xe3\x83\xa0\xe3\x83\x88\xe3\x82\xa5\xe3\x83\x95\xe3\x83\xa9\xe3\x82\xa4\xe3\x83\xbb\xe3\x83\x91\xe3\x83\x83\xe3\x82\xb1\xe3\x83\xbc\xe3\x82\xb8\xe3\x81\xab\xe3\x81\xaf\xe3\x80\x81\xe7\xa9\xba\xe6\xb8\xaf\xe3\x81\xae\xe9\xa7\x90\xe8\xbb\x8a\xe6\x96\x99\xe9\x87\x91\xe3\x81\x8c\xe5\x90\xab\xe3\x81\xbe\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x8a\xe3\x82\x8a\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82'
gatwickText2= '\xe3\x82\xac\xe3\x83\x88\xe3\x82\xa6\xe3\x82\xa3\xe3\x83\x83\xe3\x82\xaf<font color=red>\xe7\xa9\xba\xe6\xb8\xaf\xe3\x81\xa8\xe9\x80\xa3\xe7\xb5\xa1\xe9\x80\x9a\xe8\xb7\xaf\xe3\x81\xa7\xe7\x9b\xb4\xe7\xb5\x90</font>\xe3\x81\x95\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x84\xe3\x82\x8b\xe5\x94\xaf\xe4\xb8\x80\xe3\x81\xae\xe3\x83\x9b\xe3\x83\x86\xe3\x83\xab\xe3\x81\xa7\xe3\x81\x82\xe3\x82\x8b\xe5\xbd\x93\xe3\x83\x9b\xe3\x83\x86\xe3\x83\xab\xe3\x81\xaf\xe3\x80\x81\xe8\xa1\x97\xe3\x81\xae\xe4\xb8\xad\xe5\xbf\x83\xe9\x83\xa8\xe3\x81\x8b\xe3\x82\x8930\xe5\x88\x86\xe3\x81\xae\xe5\xa0\xb4\xe6\x89\x80\xe3\x81\xab\xe3\x81\x94\xe3\x81\x96\xe3\x81\x84\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82\xe5\x85\xa8\xe5\xae\xa2\xe5\xae\xa4\xe3\x81\xab\xe9\xab\x98\xe9\x80\x9f\xe3\x82\xa4\xe3\x83\xb3\xe3\x82\xbf\xe3\x83\xbc\xe3\x83\x8d\xe3\x83\x83\xe3\x83\x88<link fg="blue" href="http://www.reportlab.com">\xe7\x92\xb0\xe5\xa2\x83\xe3\x82\x92\xe5\xae\x8c\xe5\x82\x99</link>\xe3\x81\x97\xe3\x81\xa6<u>\xe3\x81\x8a\xe3\x82\x8a\xe3\x81\xbe\xe3\x81\x99</u>\xe3\x80\x82\xe3\x83\x95\xe3\x82\xa1\xe3\x83\x9f\xe3\x83\xaa\xe3\x83\xbc\xe3\x83\xab\xe3\x83\xbc\xe3\x83\xa0\xe3\x81\xaf5\xe5\x90\x8d\xe6\xa7\x98\xe3\x81\xbe\xe3\x81\xa7\xe3\x81\x8a\xe6\xb3\x8a\xe3\x82\x8a\xe3\x81\x84\xe3\x81\x9f\xe3\x81\xa0\xe3\x81\x91\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82\xe3\x81\xbe\xe3\x81\x9f\xe3\x80\x81\xe3\x82\xa8\xe3\x82\xb0\xe3\x82\xbc\xe3\x82\xaf\xe3\x83\x86\xe3\x82\xa3\xe3\x83\x96\xe3\x83\xab\xe3\x83\xbc\xe3\x83\xa0\xe3\x81\xae\xe3\x81\x8a\xe5\xae\xa2\xe6\xa7\x98\xe3\x81\xaf\xe3\x80\x81\xe3\x82\xa8\xe3\x82\xb0\xe3\x82\xbc\xe3\x82\xaf\xe3\x83\x86\xe3\x82\xa3\xe3\x83\x96\xe3\x83\xa9\xe3\x82\xa6\xe3\x83\xb3\xe3\x82\xb8\xe3\x82\x92\xe3\x81\x94\xe5\x88\xa9\xe7\x94\xa8\xe3\x81\x84\xe3\x81\x9f\xe3\x81\xa0\xe3\x81\x91\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82\xe4\xba\x8b\xe5\x89\x8d\xe3\x81\xab\xe3\x81\x94\xe4\xba\x88\xe7\xb4\x84\xe3\x81\x84\xe3\x81\x9f\xe3\x81\xa0\xe3\x81\x91\xe3\x82\x8b\xe3\x82\xbf\xe3\x82\xa4\xe3\x83\xa0\xe3\x83\x88\xe3\x82\xa5\xe3\x83\x95\xe3\x83\xa9\xe3\x82\xa4\xe3\x83\xbb\xe3\x83\x91\xe3\x83\x83\xe3\x82\xb1\xe3\x83\xbc\xe3\x82\xb8\xe3\x81\xab\xe3\x81\xaf\xe3\x80\x81\xe7\xa9\xba\xe6\xb8\xaf\xe3\x81\xae\xe9\xa7\x90\xe8\xbb\x8a\xe6\x96\x99\xe9\x87\x91\xe3\x81\x8c\xe5\x90\xab\xe3\x81\xbe\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x8a\xe3\x82\x8a\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82'
c.setFont('HeiseiMin-W3', 12)
jPara = Paragraph(gatwickText, jStyle)
jPara.wrap(300, 200)
jPara.drawOn(c, 100, 220)
jPara = Paragraph(gatwickText2, jStyle)
jPara.wrap(300, 200)
jPara.drawOn(c, 100, 320)
c.setFillColor(colors.purple)
tx = c.beginText(100, 200)
tx.setFont('Helvetica', 12)
tx.textLines("""This document shows sample output in Japanese
from the Reportlab PDF library. This page shows the two fonts
available and tests our ability to measure the width of glyphs
in both horizontal and vertical writing, with proportional and
fixed-width characters. The red boxes should be the same width
(or height) as the character strings they surround.
The next pages show more samples and information.
""")
c.drawText(tx)
c.setFont('Helvetica',10)
c.drawCentredString(297, 36, 'Page %d' % c.getPageNumber())
c.showPage()
c.setFont('Helvetica', 30)
c.drawString(100,700, 'Japanese TrueType Font Support')
msg = u'\u6771\u4EAC : Unicode font'.encode('utf8')
msg2 = u'utf8 input 0123456789 ABCDEF'.encode('utf8')
from reportlab.pdfbase.ttfonts import TTFont
try:
msmincho = TTFont('MS Mincho','msmincho.ttc',subfontIndex=0,asciiReadable=0)
fn = ' file=msmincho.ttc subfont 0'
except:
try:
msmincho = TTFont('MS Mincho','msmincho.ttf',asciiReadable=0)
fn = 'file=msmincho.ttf'
except:
#Ubuntu - works on Lucid Lynx if xpdf-japanese installed
try:
msmincho = TTFont('MS Mincho','ttf-japanese-mincho.ttf')
fn = 'file=msmincho.ttf'
except:
msmincho = None
if msmincho is None:
c.setFont('Helvetica', 12)
c.drawString(100,600, 'Cannot find msmincho.ttf or msmincho.ttc')
else:
#.........这里部分代码省略.........