universal unicode font for reportlab

Duncan Booth duncan.booth at invalid.invalid
Thu Sep 11 04:00:27 EDT 2008


Tim Roberts <timr at probo.com> wrote:

> Duncan Booth <duncan.booth at invalid.invalid> wrote:
>>
>>Laszlo Nagy <gandalf at shopzeus.com> wrote:
>>
>>> I need to use HTML anyway. I realized that universal unicode fonts
>>> are above 5MB in size. The report would be a 10KB PDF, but I need to
>>> embed the font before I can send it to anyone. Since some reports
>>> needs to be sent in emails, I need to use something else. I cannot
>>> be sending 10MB emails for  "one page" reports.
>>> 
>>I thought that usually when you embed a font in a PDF only the glyphs
>>which are actually used in the document get embedded. Unfortunately a
>>quick test with reportlab seems to show that it doesn't do that
>>optimisation: it looks as though it just embeds the entire font.
> 
> No, it does subsetting.  There was a debate a year or two ago on the
> reportlab list about how the font subset should be named in the
> resulting PDF file.
> 
> Is it possible you have an older release?

It was 2.1 downloaded about 30 minutes before my post.

The not too scientific test I did was to copy the font embedding example 
from the Reportlab documentation, modify it enough to make it actually 
run, and then change the output to have only one glyph. The resulting 
PDF is virtually identical. I'm not a reportlab expert though so I may 
have made some blindingly obvious beginners mistake (or maybe it only 
subsets fonts over a certain size or glyphs outside the ascii range?).


---------- rlab.py ------------
import os, sys
import reportlab
folder = os.path.dirname(reportlab.__file__) + os.sep + 'fonts'
afmFile = os.path.join(folder, 'LeERC___.AFM')
pfbFile = os.path.join(folder, 'LeERC___.PFB')
from reportlab.pdfbase import pdfmetrics
justFace = pdfmetrics.EmbeddedType1Face(afmFile, pfbFile)
faceName = 'LettErrorRobot-Chrome' # pulled from AFM file
pdfmetrics.registerTypeFace(justFace)
justFont = pdfmetrics.Font('LettErrorRobot-Chrome',faceName,'WinAnsiEncoding')
pdfmetrics.registerFont(justFont)
from reportlab.pdfgen.canvas import Canvas
canvas = Canvas('temp.pdf')
canvas.setFont('LettErrorRobot-Chrome', 32)
if sys.argv:
    canvas.drawString(10, 150, 'TTTT TTTTTT TT TT')
    canvas.drawString(10, 100, 'TTTTTTTTTTTTTTTTTTTTT')
else:
    canvas.drawString(10, 150, 'This should be in')
    canvas.drawString(10, 100, 'LettErrorRobot-Chrome')
canvas.save()
-------------------------------

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list