I wanted to have certain space inbetween each string so I made a function like below and drew string.
from io import BytesIO
from reportlab.pdfgen import canvas
from reportlab.lib.units import mm
from reportlab.lib.pagesizes import A4, landscape
bytes = BytesIO()
c = canvas.Canvas(bytes, pagesize=landscape(A4))
def map_each(text,x,y,w):
for moji in text:
c.drawString((x+w*text.index(moji))*mm,y*mm,str(moji))
fontname="Times-Roman"
p2="299"
p4="1950"
cha="261"
classification="抹"
c.setFont(fontname,12)
# c.drawString(58*mm,160*mm,p2)
map_each(p2, 58, 160, 6)
map_each(p4, 94, 160, 6)
map_each(cha, 124, 160, 6)
if classification=="抹":
syubetsu="11"
elif classification=="車":
syubetsu=" 3"
map_each(syubetsu,43,190,7)
c.save()
with open("output.pdf", "wb") as f:
f.write(bytes.getbuffer())
And then I get this.
"1 1" is "1 " and "2 9 9" is "2 9 " while "1 9 5 0" or "2 6 1" are "1 9 5 0" and "2 6 1" just fine
If I just use drawString like the one I commented out or print each letter in a loop to make sure, "1 1" and "2 9 9" are there there.
Is it possible that reportlab sometimes cannot draw strings too close to the next? Only sometimes???
text.index
returns the first occurrence in the string, try this instead:
for index, moji in enumerate(text):
c.drawString((x+w*index)*mm, y*mm, moji)