Path: csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'output': 0.05; 'subject:text': 0.05; '"""': 0.07; 'context': 0.07; 'puts': 0.07; 'python3': 0.07; '"""create': 0.09; '"__main__":': 0.09; '__name__': 0.09; 'instance.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'recommends': 0.09; 'width': 0.09; 'wrapper': 0.09; 'python': 0.11; 'def': 0.12; '"""convert': 0.16; '"""draw': 0.16; '"default"': 0.16; 'attribute,': 0.16; 'filename,': 0.16; 'height,': 0.16; 'left,': 0.16; 'name)': 0.16; 'name):': 0.16; 'pod.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'size):': 0.16; 'subject:Print': 0.16; 'width,': 0.16; 'size,': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'looked': 0.18; 'app': 0.19; 'drawing': 0.19; 'reports,': 0.19; 'subject:page': 0.19; 'seems': 0.21; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'exists': 0.24; 'simpler': 0.24; 'math': 0.24; 'post': 0.26; 'header:X-Complaints- To:1': 0.27; 'wonder': 0.29; "doesn't": 0.30; 'skip:@ 10': 0.30; 'location,': 0.31; 'object.': 0.31; 'prints': 0.31; 'class': 0.32; 'text': 0.33; 'skip:# 10': 0.33; 'skip:_ 10': 0.34; 'subject:the': 0.34; 'skip:s 30': 0.35; 'something': 0.35; 'but': 0.35; 'format.': 0.36; 'height': 0.36; 'yield': 0.36; 'url:org': 0.36; 'too': 0.37; 'list': 0.37; 'thank': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'temporarily': 0.60; 'new': 0.61; 'course': 0.61; 'first': 0.61; 'email addr:gmail.com': 0.63; 'provide': 0.64; 'bottom': 0.67; 'delegate': 0.68; 'landscape': 0.68; 'delegated': 0.84; 'lying': 0.84; 'powerfull': 0.84; 'resulted': 0.84; 'subject:location': 0.84; 'surface': 0.84; 'certificates': 0.91; 'courses.': 0.91; 'dirty': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Print a text at a specific location on the page Date: Wed, 04 Dec 2013 11:57:08 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084b2f9.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 111 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386154641 news.xs4all.nl 2854 [2001:888:2000:d::a6]:40593 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61010 loic.espern@gmail.com wrote: > Hi all > > This is my first post on this list :-) > > I have a web-application (developped using a python framework). In this > web-app, I would like to print certificates for some courses. > > The principle : > The course teacher has a "default" certificates, with placeholders for the > name, and the certification name. ("Congratulations _______ you > successfully passed the ______ course") He puts the certificate in his > printer, and my app prints the name, and the certification on a specific > location, to fill the placeholders. > > I looked at ReportLab, and Pod. They seems powerfull to build complex > reports, but I wonder if it exists a simpler solution just to print a text > at a specific place on the page... > > Thank you I have the following quick-and-dirty wrapper for cairographics lying around -- just added the demo. #/usr/bin/env python3 import cairo import math from contextlib import contextmanager A4 = 595, 842 _mm = 72/25.4 def mm(x): """Convert mm to cairo-native (point)""" return x * _mm class Context(object): """Wrapper for a cairo context object. Methods it doesn't provide are delegated to the cairo.Context instance. """ def __init__(self, width, height, context): self.width = width self.height = height self.context = context def __getattr__(self, name): """Unknown attribute, delegate to cairo context """ return getattr(self.context, name) @contextmanager def fontsize(self, size): """Set fontsize temporarily to something other than 12pt""" self.context.set_font_size(size) yield self.context.set_font_size(12) def line(self, start_x, start_y, end_x, end_y): """Draw a line from (start_x, start_y) to (end_x, end_y)""" self.context.move_to(start_x, start_y) self.context.line_to(end_x, end_y) def text_at(self, left, top, text): """Draw `text` at position (left, top)""" self.context.move_to(left, top) self.context.text_path(text) def setup_context( filename, size, landscape, font="Times New Roman", slant=cairo.FONT_SLANT_NORMAL): """Create a cairo drawing context suitable for printing in landscape format. """ width, height = size # http://cairographics.org/documentation/using_the_postscript_surface/ # recommends the following for printing in landscape # # the "natural" approach (swapping width and height directly without # dsc_comment, translate, rotate, looked OK in Okular resulted in misplaced # (large left/top offset, bottom right corner clipped) output in print surface = cairo.PSSurface(filename, width, height) if landscape: surface.dsc_comment("%%PageOrientation: Landscape") context = cairo.Context(surface) context.translate(0, height) context.rotate(-math.pi/2) width, height = height, width else: context = cairo.Context(surface) context.select_font_face( font, slant, cairo.FONT_WEIGHT_NORMAL) context.set_font_size(12) context.set_line_width(.5) return Context(width, height, context) if __name__ == "__main__": def demo(): context = setup_context("tmp.ps", A4, landscape=False, font="Times") with context.fontsize(18): context.text_at(mm(20), mm(100), "Max Mustermann") context.text_at(mm(120), mm(100), "Brewing") context.fill() demo() If that's too dirty for you consider using cairographics directly...