Some time ago, I translated a PHP script to python, but I am getting a few problems more recently. (it's a script to generate voting ballots)
The code:
The full code is here: https://pastebin.com/ULAF1Nh5
The part that prints the voting options (the candidates' names and parties) is around line 403:
#Now we have the text, we calculate how tall it is
current_option_height = getOptionHeight(current_option_text,left_padding,right_padding)
#Write the option out
pdf.set_left_margin(left_margin+left_padding+(current_column*column_width)+oval_column_width)
pdf.set_xy((left_margin+(current_column*column_width)+left_padding)+oval_column_width,current_column_y+OVAL_HEIGHT*2)
WriteHTML(current_option_text)
print(current_option_text)
The getOptionHeight function just returns the height of a given text. The WriteHTML function just converts html text to python text and writes it on the pdf :
def WriteHTML(html):
#HTML parser
html = html.replace('\n',' ')
a = html.replace('<BR>','\n')
a = a.replace('</B>','<B>')
a = a.split('<B>')
for i in a:
if a.index(i)%2 == 0:
#Normal
pdf.set_font('Helvetica','',11.5)
pdf.write(OPTION_LINE_HEIGHT,i)
else:
#Bold
pdf.set_font('Helvetica','B',11.5)
pdf.write(OPTION_LINE_HEIGHT,i)
The problem: At first, the script works. The first texts fed into it are:
President and Vice-President of the United States
Vote for 1 pair
<B>Joseph R. Biden<BR>and<BR>Kamala D. Harris</B><BR>Democratic
<B>Donald J. Trump<BR>and<BR>Michael R. Pence</B><BR>Republican
<B>Jo Jorgensen<BR>and<BR>Jeremy F. "Spike" Cohen</B><BR>Libertarian
(each option is written separately)
Which yields this (as expected):
However, as soon as it draws another part of the ballot, it starts writing text vertically:
Why? And how can I fix it?