class ReferenceText(IndexingFlowable): """Fakery to illustrate how a reference would work if we could put it in a paragraph.""" def __init__(self, textPattern, targetKey): self.textPattern = textPattern self.target = targetKey self.paraStyle = ParagraphStyle('tmp') self._lastPageNum = None self._pageNum = -999 self._para = None def beforeBuild(self): self._lastPageNum = self._pageNum def notify(self, kind, stuff): if kind == 'Target': (key, pageNum) = stuff if key == self.target: self._pageNum = pageNum def wrap(self, availWidth, availHeight): text = self.textPattern % self._lastPageNum self._para = Paragraph(text, self.paraStyle) return self._para.wrap(availWidth, availHeight) def drawOn(self, canvas, x, y, _sW=0): self._para.drawOn(canvas, x, y, _sW)
def test3(self): aW = 307 styleSheet = getSampleStyleSheet() bt = styleSheet['BodyText'] btj = ParagraphStyle('bodyText1j', parent=bt, alignment=TA_JUSTIFY) p = Paragraph( """<a name='top'/>Subsequent pages test pageBreakBefore, frameBreakBefore and keepTogether attributes. Generated at 1111. The number in brackets at the end of each paragraph is its position in the story. llllllllllllllllllllllllll bbbbbbbbbbbbbbbbbbbbbb ccccccccccccccccccccccc ddddddddddddddddddddd eeeeyyy""", btj) w, h = p.wrap(aW, 1000) canv = Canvas('test_platypus_paragraph_just.pdf', pagesize=(aW, h)) i = len(canv._code) p.drawOn(canv, 0, 0) ParaCode = canv._code[i:] canv.saveState() canv.setLineWidth(0) canv.setStrokeColorRGB(1, 0, 0) canv.rect(0, 0, aW, h) canv.restoreState() canv.showPage() canv.save() x = paraFontSizeHeightOffset and '50' or '53.17' good = [ 'q', '1 0 0 1 0 0 cm', 'q', 'BT 1 0 0 1 0 ' + x + ' Tm 3.59 Tw 12 TL /F1 10 Tf 0 0 0 rg (Subsequent pages test pageBreakBefore, frameBreakBefore and) Tj T* 0 Tw .23 Tw (keepTogether attributes. Generated at 1111. The number in brackets) Tj T* 0 Tw .299167 Tw (at the end of each paragraph is its position in the story. llllllllllllllllllllllllll) Tj T* 0 Tw 66.9 Tw (bbbbbbbbbbbbbbbbbbbbbb ccccccccccccccccccccccc) Tj T* 0 Tw (ddddddddddddddddddddd eeeeyyy) Tj T* ET', 'Q', 'Q' ] ok = ParaCode == good assert ok, "\nParaCode=%r\nexpected=%r" % (ParaCode, good)
def beforeDrawPage(self,canvas,doc): canvas.setFont(serif_font,10) canvas.setLineWidth(0) #header canvas.line(header_margin_hor, page_height - header_margin_vert, page_width - header_margin_hor, page_height - header_margin_vert ) if pdfstyles.show_page_header: canvas.saveState() canvas.resetTransforms() canvas.translate(header_margin_hor, page_height - header_margin_vert - 0.1*cm) p = Paragraph(self.title, text_style()) p.canv = canvas p.wrap(page_width - header_margin_hor*2.5, page_height) # add an extra 0.5 margin to have enough space for page number p.drawPara() canvas.restoreState() canvas.drawRightString(page_width - header_margin_hor, page_height - header_margin_vert + 0.1 * cm, "%d" % doc.page) #Footer canvas.saveState() canvas.setFont(serif_font,8) canvas.line(footer_margin_hor, footer_margin_vert, page_width - footer_margin_hor, footer_margin_vert ) if pdfstyles.show_page_footer: p = Paragraph(formatter.cleanText(pagefooter, escape=False), text_style()) p.canv = canvas w,h = p.wrap(page_width - header_margin_hor*2.5, page_height) p.drawOn(canvas, footer_margin_hor, footer_margin_vert - 10 - h) canvas.restoreState()
def test3(self): from reportlab.pdfgen.canvas import Canvas aW=307 styleSheet = getSampleStyleSheet() bt = styleSheet['BodyText'] btj = ParagraphStyle('bodyText1j',parent=bt,alignment=TA_JUSTIFY) p=Paragraph("""<a name='top'/>Subsequent pages test pageBreakBefore, frameBreakBefore and keepTogether attributes. Generated at 1111. The number in brackets at the end of each paragraph is its position in the story. llllllllllllllllllllllllll bbbbbbbbbbbbbbbbbbbbbb ccccccccccccccccccccccc ddddddddddddddddddddd eeeeyyy""",btj) w,h=p.wrap(aW,1000) canv=Canvas('test_platypus_paragraph_just.pdf',pagesize=(aW,h)) i=len(canv._code) p.drawOn(canv,0,0) ParaCode=canv._code[i:] canv.saveState() canv.setLineWidth(0) canv.setStrokeColorRGB(1,0,0) canv.rect(0,0,aW,h) canv.restoreState() canv.showPage() canv.save() from reportlab import rl_config x = rl_config.paraFontSizeHeightOffset and '50' or '53.17' good = ['q', '1 0 0 1 0 0 cm', 'q', 'BT 1 0 0 1 0 '+x+' Tm 3.59 Tw 12 TL /F1 10 Tf 0 0 0 rg (Subsequent pages test pageBreakBefore, frameBreakBefore and) Tj T* 0 Tw .23 Tw (keepTogether attributes. Generated at 1111. The number in brackets) Tj T* 0 Tw .299167 Tw (at the end of each paragraph is its position in the story. llllllllllllllllllllllllll) Tj T* 0 Tw 66.9 Tw (bbbbbbbbbbbbbbbbbbbbbb ccccccccccccccccccccccc) Tj T* 0 Tw (ddddddddddddddddddddd eeeeyyy) Tj T* ET', 'Q', 'Q'] ok= ParaCode==good assert ok, "\nParaCode=%r\nexpected=%r" % (ParaCode,good)
def do1(x,y,text,sty): p = Paragraph(text,sty) w,h=p.wrap(aW,1000000) y -= h p.drawOn(canv,x,y) canv.saveState() canv.setLineWidth(0.5) canv.setStrokeColor((1,0,0)) canv.rect(x,y,aW,h,stroke=1,fill=0) canv.restoreState() return y
def __list_format(canvas, doc): canvas.saveState() building_style = ParagraphStyle(name='building_title', fontSize=__FONT_SIZE__) t = u'%s<br/>Data afișării: %s<br/>Luna: %s' % (doc.habitam_building.name, doc.habitam_display, doc.habitam_month) p = Paragraph(t, building_style) p.wrapOn(canvas, 5 * cm, 2 * cm) p.drawOn(canvas, .5 * cm, __HEIGHT__ - 1.7 * cm) habitam_brand(canvas, __WIDTH__, __HEIGHT__) canvas.restoreState()
def doPara(x, text, wc, ns, n, hrep=' ', crep=' ', hdw=0, cdw=0): if '{H}' in text: text = text.replace('{H}', hrep) wc += hdw if '{C}' in text: text = text.replace('{C}', crep) wc += cdw p = Paragraph(text, bt) w, h = p.wrap(aW, 1000) annotations[:] = [] if measuring: ends[:] = [] p.drawOn(canv, x, y - h) canv.saveState() canv.setLineWidth(0.1) canv.setStrokeColorRGB(1, 0, 0) canv.rect(x, y - h, wc, h) if n is not None: canv.setFillColorRGB(0, 1, 0) canv.drawRightString(x, y - h, '%3d: ' % n) if annotations: canv.setLineWidth(0.1) canv.setStrokeColorRGB(0, 1, 0) canv.setFillColorRGB(0, 0, 1) canv.setFont('Helvetica', 0.2) for info in annotations: cur_x = info['cur_x'] + x cur_y = info['cur_y'] + y - h canv.drawCentredString(cur_x, cur_y + 0.3, '%.2f' % (cur_x - x)) canv.line(cur_x, cur_y, cur_x, cur_y + 0.299) if measuring: if not ends: errors.append( 'Paragraph measurement failure no ends found for %s\n%r' % (ns, text)) elif len(ends) > 1: errors.append( 'Paragraph measurement failure no len(ends)==%d for %s\n%r' % (len(ends), ns, text)) else: cur_x = ends[0]['cur_x'] adiff = abs(wc - cur_x) length_errors.append(adiff) if adiff > 1e-8: errors.append( 'Paragraph measurement error wc=%.4f measured=%.4f for %s\n%r' % (wc, cur_x, ns, text)) canv.restoreState() return h
def test3(self): '''compare CJK splitting in some edge cases''' from reportlab.pdfgen.canvas import Canvas from reportlab.platypus.paragraph import Paragraph from reportlab.lib.styles import ParagraphStyle from reportlab.pdfbase import pdfmetrics from reportlab.lib.enums import TA_LEFT sty = ParagraphStyle('A') sty.fontSize = 15 sty.leading = sty.fontSize*1.2 sty.fontName = 'Courier' sty.alignment = TA_LEFT sty.wordWrap = 'CJK' p0=Paragraph('ABCDEFGHIJKL]N',sty) p1=Paragraph('AB<font color="red">C</font>DEFGHIJKL]N',sty) canv = Canvas('test_platypus_paragraph_cjk3.pdf') ix = len(canv._code) aW = pdfmetrics.stringWidth('ABCD','Courier',15) w,h=p0.wrap(aW,1000000) y = canv._pagesize[1]-72-h p0.drawOn(canv,72,y) w,h=p1.wrap(aW,1000000) y -= h+10 p1.drawOn(canv,72,y) w,h=p0.wrap(aW*0.25-2,1000000) y -= h+10 p0.drawOn(canv,72,y) w,h=p1.wrap(aW/4.-2,1000000) y -= h+10 p1.drawOn(canv,72,y) assert canv._code[ix:]==['q', '1 0 0 1 72 697.8898 cm', 'q', '0 0 0 rg', 'BT 1 0 0 1 0 57 Tm /F2 15 Tf 18 TL (ABCD) Tj T* (EFGH) Tj T* (IJKL]) Tj T* (N) Tj T* ET', 'Q', 'Q', 'q', '1 0 0 1 72 615.8898 cm', 'q', 'BT 1 0 0 1 0 57 Tm 18 TL /F2 15 Tf 0 0 0 rg (AB) Tj 1 0 0 rg (C) Tj 0 0 0 rg (D) Tj T* (EFGH) Tj T* (IJKL]) Tj T* (N) Tj T* ET', 'Q', 'Q', 'q', '1 0 0 1 72 353.8898 cm', 'q', '0 0 0 rg', 'BT 1 0 0 1 0 237 Tm /F2 15 Tf 18 TL (A) Tj T* (B) Tj T* (C) Tj T* (D) Tj T* (E) Tj T* (F) Tj T* (G) Tj T* (H) Tj T* (I) Tj T* (J) Tj T* (K) Tj T* (L) Tj T* (]) Tj T* (N) Tj T* ET', 'Q', 'Q', 'q', '1 0 0 1 72 91.88976 cm', 'q', 'BT 1 0 0 1 0 237 Tm 18 TL /F2 15 Tf 0 0 0 rg (A) Tj T* (B) Tj T* 1 0 0 rg (C) Tj T* 0 0 0 rg (D) Tj T* (E) Tj T* (F) Tj T* (G) Tj T* (H) Tj T* (I) Tj T* (J) Tj T* (K) Tj T* (L) Tj T* (]) Tj T* (N) Tj T* ET', 'Q', 'Q'] canv.showPage() canv.save()
def drawrelationshipcs(can, resultsbuffer): can.setFont("Helvetica", 24) sec_title = "System Heat Balance" can.drawString(25, 750, sec_title) desc_text = "The following figure shows the relationship between the chilled water" \ " system operating efficiency and the cooling load. As the figure shows, with " \ "the Cooling load ranges between 800 RT to 1100 RT, the efficiency hovers between " \ "0.75/RT to 0.91KW/RT." stylesheet = getSampleStyleSheet() paragraph = Paragraph(desc_text, stylesheet['Normal']) aW, aH = 500, 600 w, h = paragraph.wrap(aW, aH) if w <= aW and h <= aH: paragraph.drawOn(can, 25, 700) drawing = Drawing(600, 400) data = [((801, 0.7), (903, 0.8), (799, 0.84), (1002, 0.97), (1101, 0.89)), ((987, 0.98), (1007, 1.1), (1102, 0.98), (987, 0.95), (908, 0.89))] lp = LinePlot() lp.x = 50 lp.y = 50 lp.height = 250 lp.width = 400 lp.data = data lp.joinedLines = 0 lp.lines.symbol = makeMarker('FilledCircle') # lp.lines.symbol = makeMarker('Circle') # lp.lines[1].symbol = makeMarker('Circle') lp.lineLabelFormat = '%2.0f' lp.strokeColor = colors.black lp.xValueAxis.valueMin = 0 lp.xValueAxis.valueMax = 1200 lp.xValueAxis.valueSteps = [ 0.00, 200.00, 400.00, 600.00, 800.00, 1000.00, 1200.00 ] lp.xValueAxis.labelTextFormat = '%2.1f' lp.yValueAxis.valueMin = 0 lp.yValueAxis.valueMax = 1.2 lp.yValueAxis.valueSteps = [ 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2 ] drawing.add(lp) drawing.drawOn(can, 50, 350) can.showPage()
def beforeDrawPage(self, canvas, doc): canvas.setFont(serif_font, 10) canvas.setLineWidth(0) #header canvas.line(header_margin_hor, page_height - header_margin_vert, page_width - header_margin_hor, page_height - header_margin_vert) if pdfstyles.show_page_header: canvas.saveState() canvas.resetTransforms() if not self.rtl: h_offset = header_margin_hor else: h_offset = 1.5 * header_margin_hor canvas.translate(h_offset, page_height - header_margin_vert - 0.1 * cm) p = Paragraph(self.title, text_style()) p.canv = canvas p.wrap( page_width - header_margin_hor * 2.5, page_height ) # add an extra 0.5 margin to have enough space for page number p.drawPara() canvas.restoreState() if not self.rtl: h_pos = page_width - header_margin_hor d = canvas.drawRightString else: h_pos = header_margin_hor d = canvas.drawString #d(h_pos, page_height - header_margin_vert + 0.1 * cm, "%d" % doc.page) #Footer canvas.saveState() canvas.setFont(serif_font, 8) canvas.line(footer_margin_hor, footer_margin_vert, page_width - footer_margin_hor, footer_margin_vert) if pdfstyles.show_page_footer: p = Paragraph(formatter.cleanText(pagefooter, escape=False), text_style()) p.canv = canvas w, h = p.wrap(page_width - header_margin_hor * 2.5, page_height) p.drawOn(canvas, footer_margin_hor, footer_margin_vert - 10 - h) d(page_width / 2, footer_margin_vert - 15 - h, "%d" % doc.page) canvas.restoreState()
def doPara(x,text,wc,ns,n,hrep=' ',crep=' ',hdw=0,cdw=0): if '{H}' in text: text = text.replace('{H}',hrep) wc += hdw if '{C}' in text: text = text.replace('{C}',crep) wc += cdw p = Paragraph(text,bt) w,h = p.wrap(aW,1000) annotations[:] = [] if measuring: ends[:] = [] p.drawOn(canv,x,y-h) canv.saveState() canv.setLineWidth(0.1) canv.setStrokeColorRGB(1,0,0) canv.rect(x,y-h,wc,h) if n is not None: canv.setFillColorRGB(0,1,0) canv.drawRightString(x,y-h,'%3d: ' % n) if annotations: canv.setLineWidth(0.1) canv.setStrokeColorRGB(0,1,0) canv.setFillColorRGB(0,0,1) canv.setFont('Helvetica',0.2) for info in annotations: cur_x = info['cur_x']+x cur_y = info['cur_y']+y-h canv.drawCentredString(cur_x, cur_y+0.3,'%.2f' % (cur_x-x)) canv.line(cur_x,cur_y,cur_x,cur_y+0.299) if measuring: if not ends: errors.append('Paragraph measurement failure no ends found for %s\n%r' % (ns,text)) elif len(ends)>1: errors.append('Paragraph measurement failure no len(ends)==%d for %s\n%r' % (len(ends),ns,text)) else: cur_x = ends[0]['cur_x'] adiff = abs(wc-cur_x) length_errors.append(adiff) if adiff>1e-8: errors.append('Paragraph measurement error wc=%.4f measured=%.4f for %s\n%r' % (wc,cur_x,ns,text)) canv.restoreState() return h
def drawsystempeakefficieny(can, resultsbuffer): can.setFont("Helvetica", 24) sec_title = "System Peak Efficiency Breakdown" can.drawString(25, 750, sec_title) desc_text = "The system operating peak efficiency measures the peak efficiency value between " \ "the selected dates for different parts of the system." stylesheet = getSampleStyleSheet() paragraph = Paragraph(desc_text, stylesheet['Normal']) aW, aH = 500, 600 w, h = paragraph.wrap(aW, aH) if w <= aW and h <= aH: paragraph.drawOn(can, 25, 700) peak = (ce_peak, chwp_peak, cwp_peak, ct_peak) = (0.656, 0.075, 0.024, 0.027) overall_peak = sum(peak) chiller_peak_str = Paragraph("Chiller Efficiency", stylesheet['Normal']) chwp_peak_str = Paragraph("Chilled Water Pump Efficiency", stylesheet['Normal']) cwp_peak_str = Paragraph("Condenser Water Pump Efficiency", stylesheet['Normal']) ct_peak_str = Paragraph("Cooling Tower Efficiency", stylesheet['Normal']) overall_peak_str = Paragraph("Overall Efficiency", stylesheet['Normal']) data = [[' ', 'Peak', 'Unit'], [chiller_peak_str, ce_peak, 'KW/RT'], [chwp_peak_str, chwp_peak, 'KW/RT'], [cwp_peak_str, cwp_peak, 'KW/RT'], [ct_peak_str, ct_peak, 'KW/RT'], [overall_peak_str, overall_peak, 'KW/RT']] t = Table(data, [2.5 * inch, inch, inch], 6 * [0.5 * inch]) t.setStyle( TableStyle([ ('TEXTCOLOR', (1, 1), (-2, -1), colors.red), ('TEXTCOLOR', (0, 0), (0, -1), colors.blue), ('TEXTCOLOR', (0, -1), (0, -1), colors.green), ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black), ('BOX', (0, 0), (-1, -1), 0.25, colors.black), ])) t.wrapOn(can, 500, 500) t.drawOn(can, 100, 450) can.showPage()
def drawsystemefficiency(can, resultsbuffer): can.setFont("Helvetica", 24) sec_title = "System Efficiency Breakdown" can.drawString(25, 750, sec_title) desc_text = "The system operating efficiency consists of the metrics of different parts" \ "in the system: chillers, chilled water pumps, condenser water pumps and cooling" \ "towers. Here we demonstrate the average efficiency measured for each day from () " \ "to ()." stylesheet = getSampleStyleSheet() paragraph = Paragraph(desc_text, stylesheet['Normal']) aW, aH = 500, 600 w, h = paragraph.wrap(aW, aH) if w <= aW and h <= aH: paragraph.drawOn(can, 25, 700) chiller_efficiency = Paragraph("Chiller Efficiency", stylesheet['Normal']) chwp_efficiency = Paragraph("Chilled Water Pump Efficiency", stylesheet['Normal']) cwp_efficiency = Paragraph("Condenser Water Pump Efficiency", stylesheet['Normal']) ct_efficiency = Paragraph("Cooling Tower Efficiency", stylesheet['Normal']) overall_efficiency = Paragraph("Overall Efficiency", stylesheet['Normal']) data = [[' ', '1st Day', '2nd Day', '3rd Day', 'unit'], [chiller_efficiency, '0.712', '0.734', '0.723', 'KW/RT'], [chwp_efficiency, '0.043', '0.076', '0.034', 'KW/RT'], [cwp_efficiency, '0.040', '0.041', '0.042', 'KW/RT'], [ct_efficiency, '0.029', '0.021', '0.023', 'KW/RT'], [overall_efficiency, '0.784', '0.831', '0.780', 'KW/RT']] t = Table(data, 5 * [1 * inch], 6 * [1 * inch]) t.setStyle( TableStyle([ ('TEXTCOLOR', (1, 1), (-2, -1), colors.red), ('TEXTCOLOR', (0, 0), (0, -1), colors.blue), ('TEXTCOLOR', (0, -1), (0, -1), colors.green), ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black), ('BOX', (0, 0), (-1, -1), 0.25, colors.black), ])) t.wrapOn(can, 500, 500) t.drawOn(can, 100, 250) can.showPage()
def afterPage(self): """Called after each page has been processed""" # saveState keeps a snapshot of the canvas state, so you don't # mess up any rendering that platypus will do later. self.canv.saveState() # Reset the origin to (0, 0), remember, we can restore the # state of the canvas later, so platypus should be unaffected. self.canv._x = 0 self.canv._y = 0 style = getSampleStyleSheet() p = Paragraph("This is drawn after the page!", style["Normal"]) # Wraps and draws the paragraph onto the canvas # You can change the last 2 parameters (canv, x, y) p.wrapOn(self.canv, 2*inch, 2*inch) p.drawOn(self.canv, 1*inch, 3*inch) # Now we restore the canvas back to the way it was. self.canv.restoreState()
def HeaderFooter(canvas, doc): canvas.saveState() styleN = PS('nomal', fontName='Times-Roman', leading=9, fontSize=9) P = Paragraph("This is a multi-line footer or header", styleN) w, h = P.wrap(doc.width, doc.bottomMargin) #print doc.width, doc.bottomMargin #print w, h #print dir(doc) #print dir(canvas) global pageNum if doc.page < pageNum: global ChapterName ChapterName = 'Table of contents' #print doc.page #print ChapterName pageNum = doc.page if doc.page == 1: footerMsg = [] footerMsg.append(Paragraph("Lei Yang", styleN)) footerMsg.append(Paragraph("Wei Gao", styleN)) footerMsg.append(Paragraph("XiangYu Dong", styleN)) footerMsg.append(Paragraph("Liang Chi", styleN)) footerMsg.append(Paragraph("Beijing ChaoYang, China", styleN)) canvas.line(2.5*cm, h+50, w+2.5*cm, h+50) f = Frame(70, 2, 16*cm, 2.1*cm, showBoundary=0) f.addFromList(footerMsg,canvas) else: P = Paragraph("User Guide", styleN) w, h = P.wrap(doc.width, doc.bottomMargin) P.drawOn(canvas, doc.leftMargin, h+789) P = Paragraph(ChapterName, PS('nomal', fontName='Times-Roman', fontSize=9, alignment = TA_RIGHT, leading=9)) w, h = P.wrap(doc.width, doc.bottomMargin) P.drawOn(canvas, doc.rightMargin, h+789) P = Paragraph("Page %d" % doc.page, PS('nomal', fontName='Times-Roman', fontSize=9, alignment = 1)) w, h = P.wrap(doc.width, doc.bottomMargin) P.drawOn(canvas, doc.leftMargin, h) canvas.line(2.5*cm, h+780, w+2.5*cm, h+780) canvas.restoreState()
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: pdfmetrics.registerFont(msmincho) c.setFont('MS Mincho', 30) c.drawString(100, 600, msg) c.drawString(100, 570, msg2) c.drawString(100, 540, fn) if fn.endswith('0'): try: msmincho1 = TTFont('MS Mincho 1', 'msmincho.ttc', subfontIndex=1, asciiPreload=0) pdfmetrics.registerFont(msmincho1) fn = ' file=msmincho.ttc subfont 1' c.setFont('MS Mincho 1', 30) c.drawString(100, 500, msg + fn) except: c.setFont('Helvetica', 30) c.drawString(100, 500, msg) c.drawString(100, 470, msg2) c.drawString(100, 440, fn) #test a paragraph with CJK and <br/> tags u = u'''<font color=red>\u30ac\u30c8\u30a6\u30a3\u30c3</font><br/><font color=blue>\u30af\u7a7a\u6e2f\u3068\u9023\u7d61\u901a</font><br/>\u8def\u3067\u76f4\u7d50\u3055\u308c\u3066\u3044\u308b\u552f<br/>\u4e00\u306e\u30db\u30c6\u30eb\u3067\u3042\u308b\u5f53\u30db\u30c6\u30eb\u306f\u3001\u8857\u306e\u4e2d\u5fc3\u90e8\u304b\u308930\u5206\u306e\u5834\u6240\u306b\u3054\u3056\u3044\u307e\u3059\u3002\u5168\u5ba2\u5ba4\u306b\u9ad8\u901f\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8<br/>\u74b0\u5883\u3092\u5b8c\u5099\u3057\u3066\u304a\u308a\u307e\u3059\u3002\u30d5\u30a1\u30df\u30ea\u30fc\u30eb\u30fc\u30e0\u306f5\u540d\u69d8\u307e\u3067\u304a\u6cca\u308a\u3044\u305f\u3060\u3051\u307e\u3059\u3002\u307e\u305f\u3001\u30a8\u30b0\u30bc\u30af\u30c6\u30a3\u30d6\u30eb\u30fc\u30e0\u306e\u304a\u5ba2\u69d8\u306f\u3001\u30a8\u30b0\u30bc\u30af\u30c6\u30a3\u30d6\u30e9\u30a6\u30f3\u30b8\u3092\u3054\u5229\u7528\u3044\u305f\u3060\u3051\u307e\u3059\u3002\u4e8b\u524d\u306b\u3054\u4e88\u7d04\u3044\u305f\u3060\u3051\u308b\u30bf\u30a4\u30e0\u30c8\u30a5\u30d5\u30e9\u30a4\u30fb\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u306f\u3001\u7a7a\u6e2f\u306e\u99d0\u8eca\u6599\u91d1\u304c\u542b\u307e\u308c\u3066\u304a\u308a\u307e\u3059\u3002''' jPara = Paragraph(u, jStyle) jPara.wrap(300, 500) jPara.drawOn(c, 100, 300) 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 ##\x83h\x83L\x83\x85\x83\x81\x83\x93\x83g\x82\xcdAdobe® Acrobat® 5.0\x82\xf0\x8eg\x82\xc1\x82\xc4Adobe PDF\x81iPortable Document ##Format\x81j\x83t\x83@\x83C\x83\x8b\x82\xc9\x95\xcf\x8a\xb7\x82\xb5\x82\xdc\x82\xb5\x82\xe5\x82\xa4\x81B\x96\xb3\x8f\x9e\x94z\x95z\x82\xcc ##Adobe Acrobat Reader\x82\xf0\x8eg\x82\xa6\x82\xce\x81A\x83n\x81[\x83h\x83E\x83F\x83A\x81A\x83\\\x83t\x83g\x83E\x83F\x83A\x82\xc9\x82\xa9 ##\x82\xa9\x82\xed\x82\xe7\x82\xb8\x81A\x92N\x82\xc5\x82\xe0\x82\xa0\x82\xc8\x82\xbd\x82\xcc\x83h\x83L\x83\x85\x83\x81\x83\x93\x83g\x82\xf0 ##\x83I\x83\x8a\x83W\x83i\x83\x8b\x82\xcc\x91\xcc\x8d\xd9\x82\xc5\x8aJ\x82\xad\x82\xb1\x82\xc6\x82\xaa\x82\xc5\x82\xab\x82\xdc\x82\xb7\x81B ##\x82\xa0\x82\xc8\x82\xbd\x82\xcc\x88\xd3\x90}\x82\xb5\x82\xbd\x82\xc6\x82\xa8\x82\xe8\x82\xc9\x8f\xee\x95\xf1\x82\xf0\x93`\x82\xa6\x82\xe9 ##\x82\xb1\x82\xc6\x82\xaa\x82\xc5\x82\xab\x82\xdc\x82\xb7\x81B ##\x82\xb3\x82\xe7\x82\xc9\x81AAdobe Acrobat 5.0\x82\xc5\x82\xcd\x81AWeb\x83u\x83\x89\x83E\x83U\x82\xa9\x82\xe7\x83R\x83\x81\x83\x93\x83g\x82\xe2 ##\x83}\x81[\x83N\x83A\x83b\x83v\x82\xf0\x8f\x91\x82\xab\x8d\x9e\x82\xf1\x82\xbe\x82\xe8\x81A\x93d\x8eq\x8f\x90\x96\xbc\x82\xf0\x8f\x91\x82\xab ##\x8d\x9e\x82\xdd\x81A\x8c\xb4\x96{\x82\xc6\x82\xb5\x82\xc4\x83\x8d\x81[\x83J\x83\x8b\x82\xc9\x95\xdb\x91\xb6\x82\xb7\x82\xe9\x82\xb1\x82\xc6\x82\xe0\x89\xc2\x94\\\x82\xc5\x82\xb7\x81B ##\x8a\xe9\x8b\xc6\x93\xe0\x82\xa0\x82\xe9\x82\xa2\x82\xcd\x8a\xe9\x8b\xc6\x82\xcc\x98g\x82\xf0\x92\xb4\x82\xa6\x82\xc4\x83`\x81[\x83\x80\x82\xc5 ##\x82\xcc\x83h\x83L\x83\x85\x83\x81\x83\x93\x83g\x83\x8f\x81[\x83N\x82\xcc\x90\xb6\x8eY\x90\xab\x82\xf0\x8c\xfc\x8f\xe3\x82\xb3\x82\xb9\x82\xe9\x82\xb1\x82\xc6\x82\xaa\x82\xc5\x82\xab\x82\xdc\x82\xb7\x81B ## ##Adobe Acrobat 5.0\x82\xc5\x8d\xec\x90\xac\x82\xb5\x82\xbdAdobe PDF\x82\xcd\x81A(Acrobat 5.0\x82\xc5\x82\xcc\x82\xdd\x83T\x83|\x81[\x83g ##\x82\xb5\x82\xc4\x82\xa2\x82\xe9\x88\xc3\x8d\x86\x89\xbb\x90\xdd\x92\xe8\x82\xf0\x8f\x9c\x82\xa2\x82\xc4\x82\xcd)\x8f]\x97\x88\x82\xdc ##\x82\xc5\x82\xcc\x83o\x81[\x83W\x83\x87\x83\x93(3\x82\xa8\x82\xe6\x82\xd1\x82S)\x82\xccAcrobat Reader\x82\xc5\x82\xe0\x8aJ\x82\xad ##\x82\xb1\x82\xc6\x82\xaa\x82\xc5\x82\xab\x82\xdc\x82\xb7\x81B\x8f\xee\x95\xf1\x8b\xa4\x97L\x82\xcc\x83c\x81[\x83\x8b\x82\xc6\x82\xb5 ##\x82\xc4\x81A\x82\xb3\x82\xe7\x82\xc9\x90i\x95\xe0\x82\xb5\x82\xbdAdobe Acrobat 5.0\x82\xf0\x81A\x8f]\x97\x88\x82\xcc\x8a\xc2\x8b\xab ##\x82\xc5\x82\xe0\x88\xc0\x90S\x82\xb5\x82\xc4\x82\xb2\x97\x98\x97p\x82\xa2\x82\xbd\x82\xbe\x82\xaf\x82\xdc\x82\xb7\x81B ## ##\x96{\x90\xbb\x95i\x82\xf0\x83l\x83b\x83g\x83\x8f\x81[\x83N\x82\xc8\x82\xc7\x82\xf0\x89\xee\x82\xb5\x82\xc4\x92\xbc\x90\xda\x82\xa0\x82\xe9 ##\x82\xa2\x82\xcd\x8a\xd4\x90\xda\x82\xc9\x95\xa1\x90\x94\x82\xcc\x92[\x96\x96\x82\xa9\x82\xe7\x8eg\x97p\x82\xb7\x82\xe9\x8f\xea\x8d\x87\x81A ##\x82\xbb\x82\xcc\x92[\x96\x96\x82\xc6\x93\xaf\x90\x94\x82\xcc\x83\x89\x83C\x83Z\x83\x93\x83X\x82\xf0\x82\xb2\x8dw\x93\xfc\x82\xad\x82\xbe ##\x82\xb3\x82\xa2\x81B\x96{\x90\xbb\x95i\x82\xcd\x83N\x83\x89\x83C\x83A\x83\x93\x83g\x97p\x83\\\x83t\x83g\x83E\x83F\x83A\x82\xc5\x82\xa0\x82\xe8 ##\x81A\x83T\x81[\x83o\x97p\x83\\\x83t\x83g\x83E\x83F\x83A\x82\xc6\x82\xb5\x82\xc4\x82\xa8\x8eg\x82\xa2\x82\xa2\x82\xbd\x82\xbe\x82\xad\x82\xb1\x82\xc6 ##\x82\xcd\x81A\x8f\xe3\x8bL\x95\xfb\x96@\x82\xc9\x82\xe6\x82\xe9\x88\xc8\x8aO\x81A\x8b\x96\x91\xf8\x82\xb3\x82\xea\x82\xc4\x82\xa2\x82\xdc\x82\xb9 ##\x82\xf1\x81B\x95\xa1\x90\x94\x82\xcc\x83\x89\x83C\x83Z\x83\x93\x83X\x82\xf0\x82\xb2\x8dw\x93\xfc\x82\xb3\x82\xea\x82\xe9\x8f\xea\x8d\x87\x82\xc9 ##\x82\xcd\x83\x89\x83C\x83Z\x83\x93\x83X\x83v\x83\x8d\x83O\x83\x89\x83\x80\x82\xf0\x82\xb2\x97\x98\x97p\x82\xc9\x82\xc8\x82\xe9\x82\xc6\x82\xa8\x93\xbe\x82\xc5\x82\xb7\x81B ## ## ##\x81y\x82\xa8\x92m\x82\xe7\x82\xb9\x81zMicrosoft Office XP\x82\xa9\x82\xe7PDF\x82\xf0\x8d\xec\x90\xac\x82\xb7\x82\xe9\x82\xc9\x82\xcd ##""" ## c.setFont('Helvetica', 24) ## c.drawString(100,750, "Sample text from Adobe's web site") ## tx = c.beginText(100,700) ## tx.setFont('Helvetica', 10) ## tx.textLine('Note: line wrapping has not been preserved and some lines may be wrapped in mid-word.') ## tx.textLine('We are just testing that we see Japanese and not random characters!') ## tx.setFont('HeiseiMin-W3-90ms-RKSJ-H',6) ## tx.textLines(sample) ## tx.setFont('Helvetica', 8) ## tx.textLine() ## tx.textLine() ## tx.textLines(""" ## This test document shows Japanese output from the Reportlab PDF Library. ## You may use two fonts, HeiseiMin-W3 and HeiseiKakuGo-W5, and a number of ## different encodings. ## ## The available encoding names (with comments from the PDF specification) are: ## encodings_jpn = [ ## # official encoding names, comments taken verbatim from PDF Spec ## '83pv-RKSJ-H', #Macintosh, JIS X 0208 character set with KanjiTalk6 ## #extensions, Shift-JIS encoding, Script Manager code 1 ## '90ms-RKSJ-H', #Microsoft Code Page 932 (lfCharSet 0x80), JIS X 0208 ## #character set with NEC and IBM extensions ## '90ms-RKSJ-V', #Vertical version of 90ms-RKSJ-H ## '90msp-RKSJ-H', #Same as 90ms-RKSJ-H, but replaces half-width Latin ## #characters with proportional forms ## '90msp-RKSJ-V', #Vertical version of 90msp-RKSJ-H ## '90pv-RKSJ-H', #Macintosh, JIS X 0208 character set with KanjiTalk7 ## #extensions, Shift-JIS encoding, Script Manager code 1 ## 'Add-RKSJ-H', #JIS X 0208 character set with Fujitsu FMR extensions, ## #Shift-JIS encoding ## 'Add-RKSJ-V', #Vertical version of Add-RKSJ-H ## 'EUC-H', #JIS X 0208 character set, EUC-JP encoding ## 'EUC-V', #Vertical version of EUC-H ## 'Ext-RKSJ-H', #JIS C 6226 (JIS78) character set with NEC extensions, ## #Shift-JIS encoding ## 'Ext-RKSJ-V', #Vertical version of Ext-RKSJ-H ## 'H', #JIS X 0208 character set, ISO-2022-JP encoding, ## 'V', #Vertical version of H ## 'UniJIS-UCS2-H', #Unicode (UCS-2) encoding for the Adobe-Japan1 character ## #collection ## 'UniJIS-UCS2-V', #Vertical version of UniJIS-UCS2-H ## 'UniJIS-UCS2-HW-H', #Same as UniJIS-UCS2-H, but replaces proportional Latin ## #characters with half-width forms ## 'UniJIS-UCS2-HW-V' #Vertical version of UniJIS-UCS2-HW-H ## ] ## ## The next few pages show the complete character set available in the encoding ## "90ms-RKSJ-H" - Shift-JIS with the standard Microsoft extensions. ## """) ## c.drawText(tx) ## ## c.setFont('Helvetica',10) ## c.drawCentredString(297, 36, 'Page %d' % c.getPageNumber()) ## ## ## ## c.showPage() from reportlab.lib import textsplit c.setFont('HeiseiMin-W3', 14) y = 700 c.drawString(70, y, 'cannot end line') y -= 20 for group in textsplit.CANNOT_START_LINE: c.drawString(70, y, group) y -= 20 c.setFont('Helvetica', 10) c.drawString(70, y, ' '.join(map(lambda x: repr(x)[4:-1], group))) c.setFont('HeiseiMin-W3', 14) y -= 20 y -= 20 c.drawString(70, y, 'cannot end line') y -= 20 for group in textsplit.CANNOT_END_LINE: c.drawString(70, y, group) y -= 20 c.setFont('Helvetica', 10) c.drawString(70, y, ' '.join(map(lambda x: repr(x)[2:], group))) c.setFont('HeiseiMin-W3', 14) y -= 20 c.showPage() #utf8 encoded paragraph sample2_uni = u'''\u30ac\u30c8\u30a6\u30a3\u30c3\u30af\u7a7a\u6e2f\u3068\u9023\u7d61\u901a \u8def\u3067\u76f4\u7d50\u3055\u308c\u3066\u3044\u308b\u552f\u4e00\u306e\u30db\u30c6\u30eb \u3067\u3042\u308b\u5f53\u30db\u30c6\u30eb\u306f\u3001\u8857\u306e\u4e2d\u5fc3\u90e8\u304b \u308930\u5206\u306e\u5834\u6240\u306b\u3054\u3056\u3044\u307e\u3059\u3002\u5168\u5ba2\u5ba4 \u306b\u9ad8\u901f\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8\u74b0\u5883\u3092\u5b8c\u5099 \u3057\u3066\u304a\u308a\u307e\u3059\u3002\u30d5\u30a1\u30df\u30ea\u30fc\u30eb\u30fc\u30e0 \u306f5\u540d\u69d8\u307e\u3067\u304a\u6cca\u308a\u3044\u305f\u3060\u3051\u307e\u3059\u3002 \u307e\u305f\u3001\u30a8\u30b0\u30bc\u30af\u30c6\u30a3\u30d6\u30eb\u30fc\u30e0\u306e\u304a \u5ba2\u69d8\u306f\u3001\u30a8\u30b0\u30bc\u30af\u30c6\u30a3\u30d6\u30e9\u30a6\u30f3\u30b8 \u3092\u3054\u5229\u7528\u3044\u305f\u3060\u3051\u307e\u3059\u3002\u4e8b\u524d\u306b\u3054 \u4e88\u7d04\u3044\u305f\u3060\u3051\u308b\u30bf\u30a4\u30e0\u30c8\u30a5\u30d5\u30e9\u30a4 \u30fb\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u306f\u3001\u7a7a\u6e2f\u306e\u99d0\u8eca\u6599 \u91d1\u304c\u542b\u307e\u308c\u3066\u304a\u308a\u307e\u3059\u3002''' oneline_uni = u''.join(sample2_uni.split()) sample2_utf8 = oneline_uni.encode('utf8') from reportlab.platypus import Paragraph from reportlab.lib.styles import ParagraphStyle jsty = ParagraphStyle('japanese', fontName='HeiseiMin-W3', wordWrap='CJK') jpara = Paragraph(oneline_uni, style=jsty) c.drawString( 100, 710, 'Try to wrap a paragraph using a style with wordWrap="CJK"') w, h = jpara.wrap(400, 400) jpara.drawOn(c, 100, 700 - h) #now try to split it... c.drawString(100, 510, 'Now try to split a paragraph as if over a page break') topPara, bottomPara = jpara.split(400, 30) w1, h1 = topPara.wrap(400, 30) topPara.drawOn(c, 100, 450) w2, h2 = bottomPara.wrap(400, 30) bottomPara.drawOn(c, 100, 400) #print 'split into heights %0.2f, %0.2f' % (topPara.height, bottomPara.height) ## c.showPage() ## ## ## # full kuten chart in EUC ## c.setFont('Helvetica', 24) ## c.drawString(72,750, 'Characters available in JIS 0208-1997') ## y = 600 ## for row in range(1, 95): ## KutenRowCodeChart(row, 'HeiseiMin-W3','EUC-H').drawOn(c, 72, y) ## y = y - 125 ## if y < 50: ## c.setFont('Helvetica',10) ## c.drawCentredString(297, 36, 'Page %d' % c.getPageNumber()) ## c.showPage() ## y = 700 ## ## c.showPage() #try with Unicode truetype - Mincho for starters ## import time ## started = time.clock() ## c.showPage() ## c.setFont('Helvetica',16) ## c.drawString(100,750, 'About to say Tokyo in MS Gothic...') ## ## from reportlab.pdfbase.ttfonts import TTFont, TTFontFile ## f = TTFontFile("msgothic.ttf") ## print f.name ## ## pdfmetrics.registerFont(TTFont(f.name, f)) ## ## utfText = u'Andr\202'.encode('utf8') ## c.setFont(f.name,16) ## c.drawString(100,700, utfText) ## ## ## #tokyoUCS2 = '\x67\x71\x4E\xAC' ## finished = time.clock() c.save() if VERBOSE: print 'saved test_multibyte_jpn.pdf'
def testUtf8Canvas(self): """Verify canvas declared as utf8 autoconverts. This assumes utf8 input. It converts to the encoding of the underlying font, so both text lines APPEAR the same.""" c = Canvas(outputfile('test_pdfbase_encodings_utf8.pdf')) c.drawString(100,700, testUTF8) # Set a font with UTF8 encoding c.setFont('Vera', 12) # This should pass the UTF8 through unchanged c.drawString(100,600, testUTF8) # and this should convert from Unicode to UTF8 c.drawString(100,500, testUni) # now add a paragraph in Latin-1 in the latin-1 style p = Paragraph(testUTF8, style=self.styNormal, encoding="utf-8") w, h = p.wrap(150, 100) p.drawOn(c, 100, 400) #3 c.rect(100,300,w,h) # now add a paragraph in UTF-8 in the UTF-8 style p2 = Paragraph(testUTF8, style=self.styTrueType, encoding="utf-8") w, h = p2.wrap(150, 100) p2.drawOn(c, 300, 400) #4 c.rect(100,300,w,h) # now add a paragraph in Unicode in the latin-1 style p3 = Paragraph(testUni, style=self.styNormal) w, h = p3.wrap(150, 100) p3.drawOn(c, 100, 300) c.rect(100,300,w,h) # now add a paragraph in Unicode in the UTF-8 style p4 = Paragraph(testUni, style=self.styTrueType) p4.wrap(150, 100) p4.drawOn(c, 300, 300) c.rect(300,300,w,h) # now a graphic d1 = Drawing(400,50) d1.add(Ellipse(200,25,200,12.5, fillColor=None)) d1.add(String(200,25,testUTF8, textAnchor='middle', encoding='utf-8')) d1.drawOn(c, 100, 150) # now a graphic in utf8 d2 = Drawing(400,50) d2.add(Ellipse(200,25,200,12.5, fillColor=None)) d2.add(String(200,25,testUTF8, fontName='Vera', textAnchor='middle', encoding='utf-8')) d2.drawOn(c, 100, 100) # now a graphic in Unicode with T1 font d3 = Drawing(400,50) d3.add(Ellipse(200,25,200,12.5, fillColor=None)) d3.add(String(200,25,testUni, textAnchor='middle')) d3.drawOn(c, 100, 50) # now a graphic in Unicode with TT font d4 = Drawing(400,50) d4.add(Ellipse(200,25,200,12.5, fillColor=None)) d4.add(String(200,25,testUni, fontName='Vera', textAnchor='middle')) d4.drawOn(c, 100, 0) extracted = extractText(c.getCurrentPageContent()) self.assertEquals(extracted[0], expectedCp1252) self.assertEquals(extracted[1], extracted[2]) #self.assertEquals(subsetToUnicode(self.vera, extracted[1]), testUni) c.save()
def makePDF(names, fonts, pageSize, path): width, height = pageSize margin = 36 resultsLeft = 100 resultsRight = width - margin startTop = height - 75 blankLineHeight = 20 entryHeight = 20 glyphEntryHeight = 48 minorLine = .3 majorLine = 1 glyphSpacing = 10 settings = { "date" : time.asctime(), "width" : width, "height" : height, "margin" : margin, "labelRight" : 90, "resultsLeft" : resultsLeft, "resultsRight" : resultsRight, "resultsWidth" : resultsRight - resultsLeft, "labelWidth" : width - margin - resultsLeft, "contentWidth" : width - (margin * 2), "startTop" : startTop, "regularFont" : "Helvetica", "boldFont" : "Helvetica-Bold", "textPointSize" : 10, "textLeading" : 15, "entryHeight" : entryHeight, "glyphPointSize" : 36, "glyphEntryHeight" : glyphEntryHeight, "minorLine" : minorLine, "majorLine" : majorLine, "glyphSpacing" : glyphSpacing, } basicStyle = ParagraphStyle("BasicReport") basicStyle.fontName = settings["regularFont"] basicStyle.fontSize = settings["textPointSize"] basicStyle.leading = settings["textLeading"] pdf = canvas.Canvas(path, pagesize=pageSize) _drawTemplate(pdf, settings) flatResults = [] currentTop = startTop for name in names: flatResults.append(("line", majorLine)) flatResults.append(("note", name)) flatResults.append(("line", majorLine)) flatResults.append(("blank line", None)) flatResults.append(("glyph", name)) flatResults.append(("blank line", None)) lines = [] for tag, content in flatResults: if tag == "line": lines.append((currentTop, content)) elif tag == "blank line": currentTop -= blankLineHeight elif tag == "note": entities = [ ("&", "&"), ("<", "<"), (">", ">"), ('"', """), ("'", "'"), ] for b, a in entities: content = content.replace(b, a) p = Paragraph(content, basicStyle) w, h = p.wrap(settings["resultsWidth"], 5000) rH = h + ((h / settings["textLeading"]) * 5) if rH > 20: rH -= ((rH / 20) - 1) * 5 if currentTop - rH < margin: lines = _finishPage(pdf, lines, settings) _drawTemplate(pdf, settings) currentTop = startTop _drawHighlightBox(pdf, currentTop, rH, settings) p.drawOn(pdf, settings["resultsLeft"], currentTop - h - 3) _drawLabel(pdf, currentTop, "Glyph", settings) currentTop -= rH else: # make sure that we have room to start all this if currentTop - glyphEntryHeight < margin: lines = _finishPage(pdf, lines, settings) _drawTemplate(pdf, settings) currentTop = startTop _drawGlyphs(pdf, content, currentTop, fonts) currentTop -== glyphEntryHeight for top, weight in lines: _drawLine(pdf, top, weight, settings) pdf.save()
def testUtf8Canvas(self): """Verify canvas declared as utf8 autoconverts. This assumes utf8 input. It converts to the encoding of the underlying font, so both text lines APPEAR the same.""" c = Canvas(outputfile('test_pdfbase_encodings_utf8.pdf')) c.drawString(100, 700, testUTF8) # Set a font with UTF8 encoding c.setFont('Vera', 12) # This should pass the UTF8 through unchanged c.drawString(100, 600, testUTF8) # and this should convert from Unicode to UTF8 c.drawString(100, 500, testUni) # now add a paragraph in Latin-1 in the latin-1 style p = Paragraph(testUTF8, style=self.styNormal, encoding="utf-8") w, h = p.wrap(150, 100) p.drawOn(c, 100, 400) #3 c.rect(100, 300, w, h) # now add a paragraph in UTF-8 in the UTF-8 style p2 = Paragraph(testUTF8, style=self.styTrueType, encoding="utf-8") w, h = p2.wrap(150, 100) p2.drawOn(c, 300, 400) #4 c.rect(100, 300, w, h) # now add a paragraph in Unicode in the latin-1 style p3 = Paragraph(testUni, style=self.styNormal) w, h = p3.wrap(150, 100) p3.drawOn(c, 100, 300) c.rect(100, 300, w, h) # now add a paragraph in Unicode in the UTF-8 style p4 = Paragraph(testUni, style=self.styTrueType) p4.wrap(150, 100) p4.drawOn(c, 300, 300) c.rect(300, 300, w, h) # now a graphic d1 = Drawing(400, 50) d1.add(Ellipse(200, 25, 200, 12.5, fillColor=None)) d1.add(String(200, 25, testUTF8, textAnchor='middle', encoding='utf-8')) d1.drawOn(c, 100, 150) # now a graphic in utf8 d2 = Drawing(400, 50) d2.add(Ellipse(200, 25, 200, 12.5, fillColor=None)) d2.add( String(200, 25, testUTF8, fontName='Vera', textAnchor='middle', encoding='utf-8')) d2.drawOn(c, 100, 100) # now a graphic in Unicode with T1 font d3 = Drawing(400, 50) d3.add(Ellipse(200, 25, 200, 12.5, fillColor=None)) d3.add(String(200, 25, testUni, textAnchor='middle')) d3.drawOn(c, 100, 50) # now a graphic in Unicode with TT font d4 = Drawing(400, 50) d4.add(Ellipse(200, 25, 200, 12.5, fillColor=None)) d4.add(String(200, 25, testUni, fontName='Vera', textAnchor='middle')) d4.drawOn(c, 100, 0) extracted = extractText(c.getCurrentPageContent()) self.assertEquals(extracted[0], expectedCp1252) self.assertEquals(extracted[1], extracted[2]) #self.assertEquals(subsetToUnicode(self.vera, extracted[1]), testUni) c.save()
def drawsystemdailyefficiency(can, resultsbuffer): can.setFont("Helvetica", 24) sec_title = "Chiller Plant System Daily Efficiency" can.drawString(25, 750, sec_title) desc_text = "The chiller plant system daily efficiency demonstrates the total efficiency including every" \ " parts of the system across a series of days. The range can be by default (say, 2 days) or" \ " specified by administrators." stylesheet = getSampleStyleSheet() paragraph = Paragraph(desc_text, stylesheet['Normal']) aW, aH = 500, 600 w, h = paragraph.wrap(aW, aH) if w <= aW and h <= aH: paragraph.drawOn(can, 25, 700) # Draw the chart drawing = Drawing(600, 400) # font fontName = 'Helvetica' fontSize = 7 # chart lp = LinePlot() lp.y = 16 lp.x = 32 lp.width = 400 lp.height = 200 # line styles lp.lines.strokeWidth = 0 lp.lines.symbol = makeMarker('FilledSquare') # x axis lp.xValueAxis = NormalDateXValueAxis() lp.xValueAxis.labels.fontName = fontName lp.xValueAxis.labels.fontSize = fontSize - 1 lp.xValueAxis.forceEndDate = 1 lp.xValueAxis.forceFirstDate = 1 lp.xValueAxis.labels.boxAnchor = 'autox' lp.xValueAxis.xLabelFormat = '{d}-{MMM}' lp.xValueAxis.maximumTicks = 5 lp.xValueAxis.minimumTickSpacing = 0.5 lp.xValueAxis.niceMonth = 0 lp.xValueAxis.strokeWidth = 1 lp.xValueAxis.loLLen = 5 lp.xValueAxis.hiLLen = 5 lp.xValueAxis.gridEnd = drawing.width lp.xValueAxis.gridStart = lp.x - 10 # y axis # self.chart.yValueAxis = AdjYValueAxis() lp.yValueAxis.visibleGrid = 1 lp.yValueAxis.visibleAxis = 0 lp.yValueAxis.labels.fontName = fontName lp.yValueAxis.labels.fontSize = fontSize - 1 lp.yValueAxis.labelTextFormat = '%0.2f%%' lp.yValueAxis.strokeWidth = 0.25 lp.yValueAxis.visible = 1 lp.yValueAxis.labels.rightPadding = 5 # self.chart.yValueAxis.maximumTicks = 6 lp.yValueAxis.rangeRound = 'both' lp.yValueAxis.tickLeft = 7.5 lp.yValueAxis.minimumTickSpacing = 0.5 lp.yValueAxis.maximumTicks = 8 lp.yValueAxis.forceZero = 0 lp.yValueAxis.avoidBoundFrac = 0.1 # legend ll = LineLegend() ll.fontName = fontName ll.fontSize = fontSize ll.alignment = 'right' ll.dx = 5 # sample data lp.data = [[(19010706, 3.3900000000000001), (19010806, 3.29), (19010906, 3.2999999999999998), (19011006, 3.29), (19011106, 3.3399999999999999), (19011206, 3.4100000000000001), (19020107, 3.3700000000000001), (19020207, 3.3700000000000001), (19020307, 3.3700000000000001), (19020407, 3.5), (19020507, 3.6200000000000001), (19020607, 3.46), (19020707, 3.3900000000000001)], [(19010706, 3.2000000000000002), (19010806, 3.1200000000000001), (19010906, 3.1400000000000001), (19011006, 3.1400000000000001), (19011106, 3.1699999999999999), (19011206, 3.23), (19020107, 3.1899999999999999), (19020207, 3.2000000000000002), (19020307, 3.1899999999999999), (19020407, 3.3100000000000001), (19020507, 3.4300000000000002), (19020607, 3.29), (19020707, 3.2200000000000002)]] lp.lines[0].strokeColor = PCMYKColor(0, 100, 100, 40, alpha=100) lp.lines[1].strokeColor = PCMYKColor(100, 0, 90, 50, alpha=100) lp.xValueAxis.strokeColor = PCMYKColor(100, 60, 0, 50, alpha=100) ll.colorNamePairs = [(PCMYKColor(0, 100, 100, 40, alpha=100), '01-Mar-2017'), (PCMYKColor(100, 0, 90, 50, alpha=100), '02-Mar-2017')] lp.lines.symbol.x = 0 lp.lines.symbol.strokeWidth = 0 lp.lines.symbol.arrowBarbDx = 5 lp.lines.symbol.strokeColor = PCMYKColor(0, 0, 0, 0, alpha=100) lp.lines.symbol.fillColor = None lp.lines.symbol.arrowHeight = 5 ll.dxTextSpace = 7 ll.boxAnchor = 'nw' ll.subCols.dx = 0 ll.subCols.dy = -2 ll.subCols.rpad = 0 ll.columnMaximum = 1 ll.deltax = 1 ll.deltay = 0 ll.dy = 5 ll.y = 240 ll.x = 300 lp.lines.symbol.kind = 'FilledCross' lp.lines.symbol.size = 5 lp.lines.symbol.angle = 45 drawing.add(lp) drawing.add(ll) # drawing.title.text = "Jurong Point System Efficiency" # drawing.title.fondSize = 16 drawing.drawOn(can, 100, 450) can.showPage()
def makePDF(names, fonts, pageSize, path): width, height = pageSize margin = 36 resultsLeft = 100 resultsRight = width - margin startTop = height - 75 blankLineHeight = 20 entryHeight = 20 glyphEntryHeight = 48 minorLine = .3 majorLine = 1 glyphSpacing = 120 settings = { "date" : time.asctime(), "width" : width, "height" : height, "margin" : margin, "labelRight" : 90, "resultsLeft" : resultsLeft, "resultsRight" : resultsRight, "resultsWidth" : resultsRight - resultsLeft, "labelWidth" : width - margin - resultsLeft, "contentWidth" : width - (margin * 2), "startTop" : startTop, "regularFont" : "Helvetica", "boldFont" : "Helvetica-Bold", "textPointSize" : 10, "textLeading" : 15, "entryHeight" : entryHeight, "glyphPointSize" : 48, "glyphEntryHeight" : glyphEntryHeight, "minorLine" : minorLine, "majorLine" : majorLine, "glyphSpacing" : glyphSpacing, } basicStyle = ParagraphStyle("BasicReport") basicStyle.fontName = settings["regularFont"] basicStyle.fontSize = settings["textPointSize"] basicStyle.leading = settings["textLeading"] pdf = canvas.Canvas(path, pagesize=pageSize) _drawTemplate(pdf, settings) flatResults = [] currentTop = startTop head = [u"Fonts tested:", ] for font in fonts: head.append(u"%s: %s" % (font.info.postscriptFullName, font.path)) flatResults.append(("head", head)) flatResults.append(("blank line", None)) for name in names: flatResults.append(("line", majorLine)) flatResults.append(("note", name)) flatResults.append(("line", majorLine)) flatResults.append(("blank line", None)) flatResults.append(("blank line", None)) flatResults.append(("glyph", name)) lines = [] for tag, content in flatResults: if tag == "line": lines.append((currentTop, content)) elif tag == "blank line": currentTop -= blankLineHeight elif tag == "head": entities = [ ("&", "&"), ("<", "<"), (">", ">"), ('"', """), ("'", "'"), ] pdf.setFillColorRGB(0, 0, 0) textObject = pdf.beginText(margin, currentTop - 13) textObject.setFont(settings["boldFont"], settings["textPointSize"], leading=12) start = textObject.getY() for line in content: if line != content[0]: textObject.setFont(settings["regularFont"], settings["textPointSize"], leading=12) for b, a in entities: content = line.replace(b, a) textObject.textLine(line) end = textObject.getY() pdf.drawText(textObject) currentTop -= (start - end) elif tag == "note": entities = [ ("&", "&"), ("<", "<"), (">", ">"), ('"', """), ("'", "'"), ] for b, a in entities: content = content.replace(b, a) p = Paragraph(content, basicStyle) w, h = p.wrap(settings["resultsWidth"], 5000) rH = h + ((h / settings["textLeading"]) * 5) if rH > 20: rH -= ((rH / 20) - 1) * 5 if currentTop - (rH + glyphEntryHeight + blankLineHeight * 2) < margin: lines = _finishPage(pdf, lines, settings) _drawTemplate(pdf, settings) currentTop = startTop _drawHighlightBox(pdf, currentTop, rH, settings) p.drawOn(pdf, settings["resultsLeft"], currentTop - h - 3) _drawLabel(pdf, currentTop, "glyph:", settings) currentTop -= rH else: # make sure that we have room to start all this if currentTop - glyphEntryHeight < margin: lines = _finishPage(pdf, lines, settings) _drawTemplate(pdf, settings) currentTop = startTop _drawGlyphs(pdf, content, currentTop, fonts, settings) currentTop -= glyphEntryHeight for top, weight in lines: _drawLine(pdf, top, weight, settings) pdf.save()
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: pdfmetrics.registerFont(msmincho) c.setFont('MS Mincho', 30) c.drawString(100,600, msg) c.drawString(100,570, msg2) c.drawString(100,540, fn) if fn.endswith('0'): try: msmincho1 = TTFont('MS Mincho 1','msmincho.ttc',subfontIndex=1,asciiPreload=0) pdfmetrics.registerFont(msmincho1) fn = ' file=msmincho.ttc subfont 1' c.setFont('MS Mincho 1',30) c.drawString(100,500,msg+fn) except: c.setFont('Helvetica',30) c.drawString(100,500,msg) c.drawString(100,470, msg2) c.drawString(100,440, fn) #test a paragraph with CJK and <br/> tags u = u'''<font color=red>\u30ac\u30c8\u30a6\u30a3\u30c3</font><br/><font color=blue>\u30af\u7a7a\u6e2f\u3068\u9023\u7d61\u901a</font><br/>\u8def\u3067\u76f4\u7d50\u3055\u308c\u3066\u3044\u308b\u552f<br/>\u4e00\u306e\u30db\u30c6\u30eb\u3067\u3042\u308b\u5f53\u30db\u30c6\u30eb\u306f\u3001\u8857\u306e\u4e2d\u5fc3\u90e8\u304b\u308930\u5206\u306e\u5834\u6240\u306b\u3054\u3056\u3044\u307e\u3059\u3002\u5168\u5ba2\u5ba4\u306b\u9ad8\u901f\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8<br/>\u74b0\u5883\u3092\u5b8c\u5099\u3057\u3066\u304a\u308a\u307e\u3059\u3002\u30d5\u30a1\u30df\u30ea\u30fc\u30eb\u30fc\u30e0\u306f5\u540d\u69d8\u307e\u3067\u304a\u6cca\u308a\u3044\u305f\u3060\u3051\u307e\u3059\u3002\u307e\u305f\u3001\u30a8\u30b0\u30bc\u30af\u30c6\u30a3\u30d6\u30eb\u30fc\u30e0\u306e\u304a\u5ba2\u69d8\u306f\u3001\u30a8\u30b0\u30bc\u30af\u30c6\u30a3\u30d6\u30e9\u30a6\u30f3\u30b8\u3092\u3054\u5229\u7528\u3044\u305f\u3060\u3051\u307e\u3059\u3002\u4e8b\u524d\u306b\u3054\u4e88\u7d04\u3044\u305f\u3060\u3051\u308b\u30bf\u30a4\u30e0\u30c8\u30a5\u30d5\u30e9\u30a4\u30fb\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u306f\u3001\u7a7a\u6e2f\u306e\u99d0\u8eca\u6599\u91d1\u304c\u542b\u307e\u308c\u3066\u304a\u308a\u307e\u3059\u3002''' jPara = Paragraph(u, jStyle) jPara.wrap(300, 500) jPara.drawOn(c, 100, 300) 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 ##\x83h\x83L\x83\x85\x83\x81\x83\x93\x83g\x82\xcdAdobe® Acrobat® 5.0\x82\xf0\x8eg\x82\xc1\x82\xc4Adobe PDF\x81iPortable Document ##Format\x81j\x83t\x83@\x83C\x83\x8b\x82\xc9\x95\xcf\x8a\xb7\x82\xb5\x82\xdc\x82\xb5\x82\xe5\x82\xa4\x81B\x96\xb3\x8f\x9e\x94z\x95z\x82\xcc ##Adobe Acrobat Reader\x82\xf0\x8eg\x82\xa6\x82\xce\x81A\x83n\x81[\x83h\x83E\x83F\x83A\x81A\x83\\\x83t\x83g\x83E\x83F\x83A\x82\xc9\x82\xa9 ##\x82\xa9\x82\xed\x82\xe7\x82\xb8\x81A\x92N\x82\xc5\x82\xe0\x82\xa0\x82\xc8\x82\xbd\x82\xcc\x83h\x83L\x83\x85\x83\x81\x83\x93\x83g\x82\xf0 ##\x83I\x83\x8a\x83W\x83i\x83\x8b\x82\xcc\x91\xcc\x8d\xd9\x82\xc5\x8aJ\x82\xad\x82\xb1\x82\xc6\x82\xaa\x82\xc5\x82\xab\x82\xdc\x82\xb7\x81B ##\x82\xa0\x82\xc8\x82\xbd\x82\xcc\x88\xd3\x90}\x82\xb5\x82\xbd\x82\xc6\x82\xa8\x82\xe8\x82\xc9\x8f\xee\x95\xf1\x82\xf0\x93`\x82\xa6\x82\xe9 ##\x82\xb1\x82\xc6\x82\xaa\x82\xc5\x82\xab\x82\xdc\x82\xb7\x81B ##\x82\xb3\x82\xe7\x82\xc9\x81AAdobe Acrobat 5.0\x82\xc5\x82\xcd\x81AWeb\x83u\x83\x89\x83E\x83U\x82\xa9\x82\xe7\x83R\x83\x81\x83\x93\x83g\x82\xe2 ##\x83}\x81[\x83N\x83A\x83b\x83v\x82\xf0\x8f\x91\x82\xab\x8d\x9e\x82\xf1\x82\xbe\x82\xe8\x81A\x93d\x8eq\x8f\x90\x96\xbc\x82\xf0\x8f\x91\x82\xab ##\x8d\x9e\x82\xdd\x81A\x8c\xb4\x96{\x82\xc6\x82\xb5\x82\xc4\x83\x8d\x81[\x83J\x83\x8b\x82\xc9\x95\xdb\x91\xb6\x82\xb7\x82\xe9\x82\xb1\x82\xc6\x82\xe0\x89\xc2\x94\\\x82\xc5\x82\xb7\x81B ##\x8a\xe9\x8b\xc6\x93\xe0\x82\xa0\x82\xe9\x82\xa2\x82\xcd\x8a\xe9\x8b\xc6\x82\xcc\x98g\x82\xf0\x92\xb4\x82\xa6\x82\xc4\x83`\x81[\x83\x80\x82\xc5 ##\x82\xcc\x83h\x83L\x83\x85\x83\x81\x83\x93\x83g\x83\x8f\x81[\x83N\x82\xcc\x90\xb6\x8eY\x90\xab\x82\xf0\x8c\xfc\x8f\xe3\x82\xb3\x82\xb9\x82\xe9\x82\xb1\x82\xc6\x82\xaa\x82\xc5\x82\xab\x82\xdc\x82\xb7\x81B ## ##Adobe Acrobat 5.0\x82\xc5\x8d\xec\x90\xac\x82\xb5\x82\xbdAdobe PDF\x82\xcd\x81A(Acrobat 5.0\x82\xc5\x82\xcc\x82\xdd\x83T\x83|\x81[\x83g ##\x82\xb5\x82\xc4\x82\xa2\x82\xe9\x88\xc3\x8d\x86\x89\xbb\x90\xdd\x92\xe8\x82\xf0\x8f\x9c\x82\xa2\x82\xc4\x82\xcd)\x8f]\x97\x88\x82\xdc ##\x82\xc5\x82\xcc\x83o\x81[\x83W\x83\x87\x83\x93(3\x82\xa8\x82\xe6\x82\xd1\x82S)\x82\xccAcrobat Reader\x82\xc5\x82\xe0\x8aJ\x82\xad ##\x82\xb1\x82\xc6\x82\xaa\x82\xc5\x82\xab\x82\xdc\x82\xb7\x81B\x8f\xee\x95\xf1\x8b\xa4\x97L\x82\xcc\x83c\x81[\x83\x8b\x82\xc6\x82\xb5 ##\x82\xc4\x81A\x82\xb3\x82\xe7\x82\xc9\x90i\x95\xe0\x82\xb5\x82\xbdAdobe Acrobat 5.0\x82\xf0\x81A\x8f]\x97\x88\x82\xcc\x8a\xc2\x8b\xab ##\x82\xc5\x82\xe0\x88\xc0\x90S\x82\xb5\x82\xc4\x82\xb2\x97\x98\x97p\x82\xa2\x82\xbd\x82\xbe\x82\xaf\x82\xdc\x82\xb7\x81B ## ##\x96{\x90\xbb\x95i\x82\xf0\x83l\x83b\x83g\x83\x8f\x81[\x83N\x82\xc8\x82\xc7\x82\xf0\x89\xee\x82\xb5\x82\xc4\x92\xbc\x90\xda\x82\xa0\x82\xe9 ##\x82\xa2\x82\xcd\x8a\xd4\x90\xda\x82\xc9\x95\xa1\x90\x94\x82\xcc\x92[\x96\x96\x82\xa9\x82\xe7\x8eg\x97p\x82\xb7\x82\xe9\x8f\xea\x8d\x87\x81A ##\x82\xbb\x82\xcc\x92[\x96\x96\x82\xc6\x93\xaf\x90\x94\x82\xcc\x83\x89\x83C\x83Z\x83\x93\x83X\x82\xf0\x82\xb2\x8dw\x93\xfc\x82\xad\x82\xbe ##\x82\xb3\x82\xa2\x81B\x96{\x90\xbb\x95i\x82\xcd\x83N\x83\x89\x83C\x83A\x83\x93\x83g\x97p\x83\\\x83t\x83g\x83E\x83F\x83A\x82\xc5\x82\xa0\x82\xe8 ##\x81A\x83T\x81[\x83o\x97p\x83\\\x83t\x83g\x83E\x83F\x83A\x82\xc6\x82\xb5\x82\xc4\x82\xa8\x8eg\x82\xa2\x82\xa2\x82\xbd\x82\xbe\x82\xad\x82\xb1\x82\xc6 ##\x82\xcd\x81A\x8f\xe3\x8bL\x95\xfb\x96@\x82\xc9\x82\xe6\x82\xe9\x88\xc8\x8aO\x81A\x8b\x96\x91\xf8\x82\xb3\x82\xea\x82\xc4\x82\xa2\x82\xdc\x82\xb9 ##\x82\xf1\x81B\x95\xa1\x90\x94\x82\xcc\x83\x89\x83C\x83Z\x83\x93\x83X\x82\xf0\x82\xb2\x8dw\x93\xfc\x82\xb3\x82\xea\x82\xe9\x8f\xea\x8d\x87\x82\xc9 ##\x82\xcd\x83\x89\x83C\x83Z\x83\x93\x83X\x83v\x83\x8d\x83O\x83\x89\x83\x80\x82\xf0\x82\xb2\x97\x98\x97p\x82\xc9\x82\xc8\x82\xe9\x82\xc6\x82\xa8\x93\xbe\x82\xc5\x82\xb7\x81B ## ## ##\x81y\x82\xa8\x92m\x82\xe7\x82\xb9\x81zMicrosoft Office XP\x82\xa9\x82\xe7PDF\x82\xf0\x8d\xec\x90\xac\x82\xb7\x82\xe9\x82\xc9\x82\xcd ##""" ## c.setFont('Helvetica', 24) ## c.drawString(100,750, "Sample text from Adobe's web site") ## tx = c.beginText(100,700) ## tx.setFont('Helvetica', 10) ## tx.textLine('Note: line wrapping has not been preserved and some lines may be wrapped in mid-word.') ## tx.textLine('We are just testing that we see Japanese and not random characters!') ## tx.setFont('HeiseiMin-W3-90ms-RKSJ-H',6) ## tx.textLines(sample) ## tx.setFont('Helvetica', 8) ## tx.textLine() ## tx.textLine() ## tx.textLines(""" ## This test document shows Japanese output from the Reportlab PDF Library. ## You may use two fonts, HeiseiMin-W3 and HeiseiKakuGo-W5, and a number of ## different encodings. ## ## The available encoding names (with comments from the PDF specification) are: ## encodings_jpn = [ ## # official encoding names, comments taken verbatim from PDF Spec ## '83pv-RKSJ-H', #Macintosh, JIS X 0208 character set with KanjiTalk6 ## #extensions, Shift-JIS encoding, Script Manager code 1 ## '90ms-RKSJ-H', #Microsoft Code Page 932 (lfCharSet 0x80), JIS X 0208 ## #character set with NEC and IBM extensions ## '90ms-RKSJ-V', #Vertical version of 90ms-RKSJ-H ## '90msp-RKSJ-H', #Same as 90ms-RKSJ-H, but replaces half-width Latin ## #characters with proportional forms ## '90msp-RKSJ-V', #Vertical version of 90msp-RKSJ-H ## '90pv-RKSJ-H', #Macintosh, JIS X 0208 character set with KanjiTalk7 ## #extensions, Shift-JIS encoding, Script Manager code 1 ## 'Add-RKSJ-H', #JIS X 0208 character set with Fujitsu FMR extensions, ## #Shift-JIS encoding ## 'Add-RKSJ-V', #Vertical version of Add-RKSJ-H ## 'EUC-H', #JIS X 0208 character set, EUC-JP encoding ## 'EUC-V', #Vertical version of EUC-H ## 'Ext-RKSJ-H', #JIS C 6226 (JIS78) character set with NEC extensions, ## #Shift-JIS encoding ## 'Ext-RKSJ-V', #Vertical version of Ext-RKSJ-H ## 'H', #JIS X 0208 character set, ISO-2022-JP encoding, ## 'V', #Vertical version of H ## 'UniJIS-UCS2-H', #Unicode (UCS-2) encoding for the Adobe-Japan1 character ## #collection ## 'UniJIS-UCS2-V', #Vertical version of UniJIS-UCS2-H ## 'UniJIS-UCS2-HW-H', #Same as UniJIS-UCS2-H, but replaces proportional Latin ## #characters with half-width forms ## 'UniJIS-UCS2-HW-V' #Vertical version of UniJIS-UCS2-HW-H ## ] ## ## The next few pages show the complete character set available in the encoding ## "90ms-RKSJ-H" - Shift-JIS with the standard Microsoft extensions. ## """) ## c.drawText(tx) ## ## c.setFont('Helvetica',10) ## c.drawCentredString(297, 36, 'Page %d' % c.getPageNumber()) ## ## ## ## c.showPage() from reportlab.lib import textsplit c.setFont('HeiseiMin-W3', 14) y = 700 c.drawString(70, y, 'cannot end line') y -= 20 for group in textsplit.CANNOT_START_LINE: c.drawString(70, y, group) y -= 20 c.setFont('Helvetica',10) c.drawString(70, y, ' '.join([ascii(x)[4:-1] for x in group])) c.setFont('HeiseiMin-W3', 14) y -= 20 y -= 20 c.drawString(70, y, 'cannot end line') y -= 20 for group in textsplit.CANNOT_END_LINE: c.drawString(70, y, group) y -= 20 c.setFont('Helvetica',10) c.drawString(70, y, ' '.join([ascii(x)[2:] for x in group])) c.setFont('HeiseiMin-W3', 14) y -= 20 c.showPage() #utf8 encoded paragraph sample2_uni = u'''\u30ac\u30c8\u30a6\u30a3\u30c3\u30af\u7a7a\u6e2f\u3068\u9023\u7d61\u901a \u8def\u3067\u76f4\u7d50\u3055\u308c\u3066\u3044\u308b\u552f\u4e00\u306e\u30db\u30c6\u30eb \u3067\u3042\u308b\u5f53\u30db\u30c6\u30eb\u306f\u3001\u8857\u306e\u4e2d\u5fc3\u90e8\u304b \u308930\u5206\u306e\u5834\u6240\u306b\u3054\u3056\u3044\u307e\u3059\u3002\u5168\u5ba2\u5ba4 \u306b\u9ad8\u901f\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8\u74b0\u5883\u3092\u5b8c\u5099 \u3057\u3066\u304a\u308a\u307e\u3059\u3002\u30d5\u30a1\u30df\u30ea\u30fc\u30eb\u30fc\u30e0 \u306f5\u540d\u69d8\u307e\u3067\u304a\u6cca\u308a\u3044\u305f\u3060\u3051\u307e\u3059\u3002 \u307e\u305f\u3001\u30a8\u30b0\u30bc\u30af\u30c6\u30a3\u30d6\u30eb\u30fc\u30e0\u306e\u304a \u5ba2\u69d8\u306f\u3001\u30a8\u30b0\u30bc\u30af\u30c6\u30a3\u30d6\u30e9\u30a6\u30f3\u30b8 \u3092\u3054\u5229\u7528\u3044\u305f\u3060\u3051\u307e\u3059\u3002\u4e8b\u524d\u306b\u3054 \u4e88\u7d04\u3044\u305f\u3060\u3051\u308b\u30bf\u30a4\u30e0\u30c8\u30a5\u30d5\u30e9\u30a4 \u30fb\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u306f\u3001\u7a7a\u6e2f\u306e\u99d0\u8eca\u6599 \u91d1\u304c\u542b\u307e\u308c\u3066\u304a\u308a\u307e\u3059\u3002''' oneline_uni = u''.join(sample2_uni.split()) sample2_utf8 = oneline_uni.encode('utf8') from reportlab.platypus import Paragraph from reportlab.lib.styles import ParagraphStyle jsty = ParagraphStyle('japanese',fontName='HeiseiMin-W3', wordWrap='CJK') jpara = Paragraph(oneline_uni, style=jsty) c.drawString(100, 710, 'Try to wrap a paragraph using a style with wordWrap="CJK"') w, h = jpara.wrap(400,400) jpara.drawOn(c, 100, 700 - h) #now try to split it... c.drawString(100, 510, 'Now try to split a paragraph as if over a page break') topPara, bottomPara = jpara.split(400, 30) w1, h1 = topPara.wrap(400, 30) topPara.drawOn(c, 100, 450) w2, h2 = bottomPara.wrap(400, 30) bottomPara.drawOn(c, 100, 400) #print 'split into heights %0.2f, %0.2f' % (topPara.height, bottomPara.height) ## c.showPage() ## ## ## # full kuten chart in EUC ## c.setFont('Helvetica', 24) ## c.drawString(72,750, 'Characters available in JIS 0208-1997') ## y = 600 ## for row in range(1, 95): ## KutenRowCodeChart(row, 'HeiseiMin-W3','EUC-H').drawOn(c, 72, y) ## y = y - 125 ## if y < 50: ## c.setFont('Helvetica',10) ## c.drawCentredString(297, 36, 'Page %d' % c.getPageNumber()) ## c.showPage() ## y = 700 ## ## c.showPage() #try with Unicode truetype - Mincho for starters ## import time ## started = time.clock() ## c.showPage() ## c.setFont('Helvetica',16) ## c.drawString(100,750, 'About to say Tokyo in MS Gothic...') ## ## from reportlab.pdfbase.ttfonts import TTFont, TTFontFile ## f = TTFontFile("msgothic.ttf") ## print f.name ## ## pdfmetrics.registerFont(TTFont(f.name, f)) ## ## utfText = u'Andr\202'.encode('utf8') ## c.setFont(f.name,16) ## c.drawString(100,700, utfText) ## ## ## #tokyoUCS2 = '\x67\x71\x4E\xAC' ## finished = time.clock() c.save() if VERBOSE: print('saved test_multibyte_jpn.pdf')