bcbnz / pylabels

Python library to create PDFs for printing labels.
GNU General Public License v3.0
100 stars 40 forks source link

Basic exampole / changing font-size? #23

Closed Rapid1898-code closed 2 years ago

Rapid1898-code commented 2 years ago

I tried the basic exampole and want to change the font-size to 10 with the following code -

But this seems not to work -

import labels
from reportlab.graphics import shapes

specs = labels.Specification(210, 297, 2, 8, 76.2, 25.4, corner_radius=2;
  left_padding=5, top_padding=5, bottom_padding=5, right_padding=5, padding_radius=4)

def draw_label(label, width, height, obj):    
  label.add(shapes.String(0, 0, str(obj), fontName="Arial", fontSize=10))

sheet = labels.Sheet(specs, draw_label, border=True)
sheet.add_label("Hello")
sheet.add_label("World")
sheet.add_labels(range(3, 22))
sheet.add_label("Oversized label here")

sheet.save('basic.pdf')
print(f"{sheet.label_count} label(s) output on {sheet.page_count}")

It seems that the font-sitze is still set to 40 with very big letters. What am i doing wrong?

command-tab commented 2 years ago

When I run your example code (after fixing the ; on line 4), I get a PDF containing little over a page of labels, all with size 10 font. Here's the second page:

image

(The first page is similar.) I did have to swap in Helvetica for Arial, but I'd be surprised if that made a difference. Also, the border radius may be cutting off the corners.

Rapid1898-code commented 2 years ago

Yes you are right - i tried this today on another computer and now it seems to work - will test this in the evening on my previous pc.

A side question by that way - is it possible to make a line-break when assigning a text with add_label Tried it with

sheet.add_label("Hello\nsecondline\nthirdline")

But with that i only get this output: [

output

](url)

command-tab commented 2 years ago

For multi-line labels, I believe you have to draw each line as a separate string, incrementing a vertical offset as you draw each line. See https://github.com/bcbnz/pylabels/issues/20#issuecomment-720211465 for an example. (I would be happy to learn if there is a simpler way to do this!)

Rapid1898-code commented 2 years ago

Thanks this works perfect! Maybe a last simple questions - is it also possible to set a font to bold somehow?

command-tab commented 2 years ago

It seems like appending -Bold to the font name does the trick, e.g. fontName="Helvetica-Bold".

Rapid1898-code commented 2 years ago

Yes - also that worked perfectly! - Thanks a lot for your help!