[reportlab-users] Using an OEM font
Luc Saffre
luc.saffre at gmx.net
Sat Oct 21 02:56:07 EDT 2006
Thanks, Andy and Tim, your answers helped me to find a solution.
It may be true that the system TrueType fonts Arial and Courier don't
support Unicode, but they do know at least the OEM charset, including
box characters. That's enough for my purpose. I prefer Courier to Arial
because for my utility I need a monospaced font. I just have to register
the Windows version of the Courier font. Since this is a system font,
the following code should produce the desired result on any Windows machine:
import os
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfgen import canvas
from reportlab.lib.units import mm
from reportlab.pdfbase.ttfonts import TTFont
filename="tmp.pdf"
s=u'\u2500'*20
print s
pdfmetrics.registerFont(TTFont("Courier", "cour.ttf"))
canvas = canvas.Canvas(filename)
canvas.setFont("Courier", 10)
t=canvas.beginText()
t.setTextOrigin(20*mm,200*mm)
t.textOut(s)
canvas.drawText(t)
canvas.save()
os.startfile(filename)
Now I can continue:
http://lino.saffre-rumma.ee/news/446.html
Luc
On 21.10.2006 01:12, Tim Roberts wrote:
> Luc Saffre wrote:
>
>> Hello,
>>
>> I'd like to create PDF files with texts encoded in cp437 or cp850. These
>> texts also contain box characters which are not part of the latin
>> character set. But most Windows machines can display and print such text
>> if you use a font that provides the OEM charset (and if you activate the
>> appropriate codepage).
>>
>> For example the following script should print a horizontal line that is
>> 20 characters wide. First on the consoloe ("print s"), then in a PDF
>> file. The console displays it correctly, but the PDF file doesn't.
>>
>> import os
>>from reportlab.pdfgen import canvas
>>from reportlab.lib.units import mm
>> filename="tmp.pdf"
>> s=u'\u2500'*20
>> print s
>> canvas = canvas.Canvas(filename)
>> t=canvas.beginText()
>> t.setTextOrigin(20*mm,200*mm)
>> t.textOut(s)
>> canvas.drawText(t)
>> canvas.showPage()
>> canvas.save()
>> os.startfile(filename)
>>
>> I guess that I must specify the right font to use --- but how?
>>
>>
>
> The default font is Helvetica, which is a Type 1 font that doesn't
> support Unicode encoding.
>
> import os
> from reportlab.pdfgen import canvas
> from reportlab.lib.units import mm
> from reportlab.pdfbase import pdfmetrics
> from reportlab.pdfbase.ttfonts import TTFont
>
> pdfmetrics.registerFont( TTFont( 'Arial', 'arial.ttf'))
> filename="tmp.pdf"
> s=u'\u2500'*20
> print s
> canvas = canvas.Canvas(filename)
> canvas.setFont( 'Arial', 12 )
> t=canvas.beginText()
> t.setTextOrigin(20*mm,200*mm)
> t.textOut(s)
> canvas.drawText(t)
> canvas.showPage()
> canvas.save()
> os.startfile(filename)
>
More information about the reportlab-users
mailing list