[reportlab-users] Embedded font problems

569 views
Skip to first unread message

jsmith4332--- via reportlab-users

unread,
Mar 13, 2017, 8:13:52 AM3/13/17
to reportl...@lists2.reportlab.com, jsmit...@yahoo.com
Hello,

Does anyone have troubleshooting tips for figuring out why an embedded TTF font does not work in reportlab (just shows rectangles)? I'm trying to use https://grandzebu.net/informatique/codbar/code128.ttf (yes, I would like to use a barcode font instead of the built in barcode generator due to quality issues when printing on a low DPI printer)

The relevant code is:

stylesheet = StyleSheet1()

stylesheet.add(
ParagraphStyle(
name='Normal',
fontName='Helvetica',
fontSize=12,
leading=16,
spaceBefore=8,
spaceAfter=8,
)
)

stylesheet.add(
ParagraphStyle(
name='Barcode',
fontName='128',
fontSize=12,
)
)

pdfmetrics.registerFont(TTFont('128', os.path.join(os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))), 'code128.ttf')))
# story definition omitted...
story.append(Paragraph('helloworld'), stylesheet['Barcode']))

Unfortunately, the resulting PDF has a bunch of squares instead of using the barcode font. When I switch to a different ttf, things seem to work. Furthermore, the code128 font referenced above seems to work fine in applications like Microsoft Word/Excel.

Any ideas?

Thanks!

Robin Becker

unread,
Mar 13, 2017, 10:39:31 AM3/13/17
to reportlab-users
On 13/03/2017 12:09, jsmith4332--- via reportlab-users wrote:
> Hello,
> Does anyone have troubleshooting tips for figuring out why an embedded TTF font does not work in reportlab (just shows rectangles)? I'm trying to use https://grandzebu.net/informatique/codbar/code128.ttf (yes, I would like to use a barcode font instead of the built in barcode generator due to quality issues when printing on a low DPI printer)
> The relevant code is:
> stylesheet = StyleSheet1()
>
> stylesheet.add(
> ParagraphStyle(
> name='Normal',
> fontName='Helvetica',
> fontSize=12,
> leading=16,
> spaceBefore=8,
> spaceAfter=8,
> )
> )
>
> stylesheet.add(
> ParagraphStyle(
> name='Barcode',
> fontName='128',
> fontSize=12,
> )
> )
> pdfmetrics.registerFont(TTFont('128', os.path.join(os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))), 'code128.ttf')))# story definition omitted...story.append(Paragraph('helloworld'), stylesheet['Barcode']))
> Unfortunately, the resulting PDF has a bunch of squares instead of using the barcode font. When I switch to a different ttf, things seem to work. Furthermore, the code128 font referenced above seems to work fine in applications like Microsoft Word/Excel.
>
> Any ideas?
> Thanks!


I've never used a codabar font, so would have difficutly in knowing much about
them. I think the assumption that we make for ttf fonts is that we use the glyph
mapping that's present in the font and then we need to pass either unicode
strings or utf8 into the drawing functions.

If you see black squares coming out that is an indication that we didn't get the
bytes or unicode mapped into glyphs so the default missing glyph is shown
(normally a black square).

If you are able to send the trivial test + a sample font to me
(ro...@reportlab.com not the list) I will certainly be able to have a look and
see if I can figure out what the problem is.

My suspicion is that these fonts need a special encoding.
--
Robin Becker
_______________________________________________
reportlab-users mailing list
reportl...@lists2.reportlab.com
https://pairlist2.pair.net/mailman/listinfo/reportlab-users

Adrian Klaver

unread,
Mar 13, 2017, 11:39:14 AM3/13/17
to jsmit...@yahoo.com, reportlab-users
Look at this page:

http://grandzebu.net/informatique/codbar-en/code128.htm

There seems to be a translation process between the original string and
the string the font needs to see. The example being:

'3745 KV 75' --> 'ÒEMÍ KV 756Ó'

There are some programs at the bottom that do that. I tried the Python
one, but it does not work. I did manage to get the Postgres plpgsql
function to work.

>
>
> Thanks!
>
>
>
> _______________________________________________
> reportlab-users mailing list
> reportl...@lists2.reportlab.com
> https://pairlist2.pair.net/mailman/listinfo/reportlab-users
>


--
Adrian Klaver
adrian...@aklaver.com

Tim Roberts

unread,
Mar 13, 2017, 12:14:52 PM3/13/17
to reportlab-users
jsmith4332--- via reportlab-users wrote:

Does anyone have troubleshooting tips for figuring out why an embedded TTF font does not work in reportlab (just shows rectangles)? I'm trying to use https://grandzebu.net/informatique/codbar/code128.ttf (yes, I would like to use a barcode font instead of the built in barcode generator due to quality issues when printing on a low DPI printer)

Odd.  I just tried this, and it worked perfectly:
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import LETTER
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont

pdfmetrics.registerFont( TTFont( 'code128', '/tmp/code128.ttf'))

canv = canvas.Canvas( 'code.pdf', pagesize=LETTER )
canv.setPageCompression( 0 )
canv.setFont( 'code128', 14 )
canv.drawString( 100, 100, "0123456789" )
canv.showPage()
canv.save()
Does it work for you?
-- 
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Robin Becker

unread,
Mar 13, 2017, 12:18:59 PM3/13/17
to reportlab-users
I found the set of unicodes that the font uses and wrote the test program below.

Inspection of the output reveals that the fonts is using a special 'private'
encoding.

There are three ranges

u'\uf020' ----> u'\uf07e'
space -----> tilde Ascii printable chars + f000

u'\uf0c8' -----> u'\uf0d4' ????

u'\uffff' the unknown glyph

If you want to do anything sensible with the font you will need to figure out
what the drawn bits of barcode actually mean and apply an encoding.

Good luck or get the barcode class to do what you need.


########################################################
from reportlab.pdfbase.pdfmetrics import registerFont
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import A4
registerFont(TTFont('128', 'code128.ttf'))

canv = Canvas('tcode128.pdf',A4)
y = ytop = A4[1] - 72
for u in '''61472 3
61473 4
61474 5
61475 6
61476 7
61477 8
61478 9
61479 10
61480 11
61481 12
61482 13
61483 14
61484 15
61485 16
61486 17
61487 18
61488 19
61489 20
61490 21
61491 22
61492 23
61493 24
61494 25
61495 26
61496 27
61497 28
61498 29
61499 30
61500 31
61501 32
61502 33
61503 34
61504 35
61505 36
61506 37
61507 38
61508 39
61509 40
61510 41
61511 42
61512 43
61513 44
61514 45
61515 46
61516 47
61517 48
61518 49
61519 50
61520 51
61521 52
61522 53
61523 54
61524 55
61525 56
61526 57
61527 58
61528 59
61529 60
61530 61
61531 62
61532 63
61533 64
61534 65
61535 66
61536 67
61537 68
61538 69
61539 70
61540 71
61541 72
61542 73
61543 74
61544 75
61545 76
61546 77
61547 78
61548 79
61549 80
61550 81
61551 82
61552 83
61553 84
61554 85
61555 86
61556 87
61557 88
61558 89
61559 90
61560 91
61561 92
61562 93
61563 94
61564 95
61565 96
61566 97
61640 98
61641 99
61642 100
61643 101
61644 102
61645 103
61646 104
61647 105
61648 106
61649 107
61650 108
61651 109
61652 3
65535 0'''.split('\n'):
u, g = map(int,u.split())
u = unichr(u)
if y<72:
y = ytop
canv.showPage()
canv.saveState()
canv.setFont('Helvetica',16)
label = 'u=%r g=%3d ' % (u,g)
canv.drawString(72,y,label)
x = 72+canv.stringWidth(label)
canv.setFillColorRGB(1,0,0)
canv.setFont('128',16)
canv.drawString(x,y,u)
canv.restoreState()
y -= 20
canv.showPage()
canv.save()
########################################################




On 13/03/2017 12:09, jsmith4332--- via reportlab-users wrote:
> Hello,
> Does anyone have troubleshooting tips for figuring out why an embedded TTF font does not work in reportlab (just shows rectangles)? I'm trying to use https://grandzebu.net/informatique/codbar/code128.ttf (yes, I would like to use a barcode font instead of the built in barcode generator due to quality issues when printing on a low DPI printer)
> The relevant code is:
> stylesheet = StyleSheet1()
>
> stylesheet.add(
> ParagraphStyle(
> name='Normal',
> fontName='Helvetica',
> fontSize=12,
> leading=16,
> spaceBefore=8,
> spaceAfter=8,
> )
> )
>
> stylesheet.add(
> ParagraphStyle(
> name='Barcode',
> fontName='128',
> fontSize=12,
> )
> )
> pdfmetrics.registerFont(TTFont('128', os.path.join(os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))), 'code128.ttf')))# story definition omitted...story.append(Paragraph('helloworld'), stylesheet['Barcode']))
> Unfortunately, the resulting PDF has a bunch of squares instead of using the barcode font. When I switch to a different ttf, things seem to work. Furthermore, the code128 font referenced above seems to work fine in applications like Microsoft Word/Excel.
>
> Any ideas?
> Thanks!
>
>
>
> _______________________________________________
> reportlab-users mailing list
> reportl...@lists2.reportlab.com
> https://pairlist2.pair.net/mailman/listinfo/reportlab-users
>


--
Robin Becker

jsmith4332--- via reportlab-users

unread,
Mar 13, 2017, 12:29:10 PM3/13/17
to reportlab-users, jsmit...@yahoo.com
It does not. (Running this in a docker container if that matters)


Adrian Klaver

unread,
Mar 13, 2017, 12:35:24 PM3/13/17
to jsmit...@yahoo.com, reportlab-users
On 03/13/2017 09:25 AM, jsmith4332--- via reportlab-users wrote:
> It does not. (Running this in a docker container if that matters)

It does not work for me either:

Reportlab 3.4.0
Python 2.7 or 3.4
openSUSE 42.2

>
>
> On Monday, March 13, 2017 12:14 PM, Tim Roberts <ti...@probo.com> wrote:
>
>



--
Adrian Klaver
adrian...@aklaver.com

Tim Roberts

unread,
Mar 13, 2017, 12:46:43 PM3/13/17
to reportlab-users
Adrian Klaver wrote:
> On 03/13/2017 09:25 AM, jsmith4332--- via reportlab-users wrote:
>> It does not. (Running this in a docker container if that matters)
> It does not work for me either:
>
> Reportlab 3.4.0
> Python 2.7 or 3.4
> openSUSE 42.2

I should have mentioned my test was Reportlab 3.1.44 in Python 3.4 on
Windows 10. Perhaps this is a Windows vs Linux thing.

--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Robin Becker

unread,
Mar 13, 2017, 12:59:24 PM3/13/17
to reportlab-users
On 13/03/2017 16:46, Tim Roberts wrote:
> Adrian Klaver wrote:
>> On 03/13/2017 09:25 AM, jsmith4332--- via reportlab-users wrote:
>>> It does not. (Running this in a docker container if that matters)
>> It does not work for me either:
>>
>> Reportlab 3.4.0
>> Python 2.7 or 3.4
>> openSUSE 42.2
>
> I should have mentioned my test was Reportlab 3.1.44 in Python 3.4 on
> Windows 10. Perhaps this is a Windows vs Linux thing.
>
I'm running on windows 7 and this fails for me. I looked inside the ttf file and
the unicode --> glyph mappings are strictly in a private area uF020 - uFFFF.

Nothing we normally do would undo that. A special decoder encoder pair would be
required.

It's possible that the older version of reportlab was doing something integer
bases that failed, but since you are using python 3.4 I'm not sure that can be true.

Did you install the font?

--
Robin Becker

Tim Roberts

unread,
Mar 13, 2017, 4:19:42 PM3/13/17
to reportlab-users
Robin Becker wrote:
> On 13/03/2017 16:46, Tim Roberts wrote:
>>
>> I should have mentioned my test was Reportlab 3.1.44 in Python 3.4 on
>> Windows 10. Perhaps this is a Windows vs Linux thing.
> I'm running on windows 7 and this fails for me. I looked inside the ttf file and
> the unicode --> glyph mappings are strictly in a private area uF020 - uFFFF.
>
> Nothing we normally do would undo that. A special decoder encoder pair would be
> required.
>
> It's possible that the older version of reportlab was doing something integer
> bases that failed, but since you are using python 3.4 I'm not sure that can be true.
>
> Did you install the font?

OK, I can explain this now. Instead of following the link from the
original mail, I Googled code128.ttf and found this:

http://www.dafont.com/code-128.font

THAT code128.ttf file does not use the private range. It's in the
normal ASCII region.

--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Reply all
Reply to author
Forward
0 new messages