I have code to convert a .txt
file to a .pdf
. I'm 99% sure that it converts the file to .pdf
, but it won't output the PDF
file.
Below is my code. I got it from an online website, btw.
from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=15)
f = open("text-file-name.txt", "r")
for x in f:
pdf.cell(200, 10, txt=x, ln=1, align='C')
pdf.output("completed.pdf")
If I run this code as it is, it comes up with this error:
Traceback (most recent call last):
File "main.py", line 27, in <module>
pdf.output("blh.pdf")
File "/home/runner/RoyalblueTimelyProperties/venv/lib/python3.8/site-packages/fpdf/fpdf.py", line 1065, in output
self.close()
File "/home/runner/RoyalblueTimelyProperties/venv/lib/python3.8/site-packages/fpdf/fpdf.py", line 246, in close
self._enddoc()
File "/home/runner/RoyalblueTimelyProperties/venv/lib/python3.8/site-packages/fpdf/fpdf.py", line 1636, in _enddoc
self._putpages()
File "/home/runner/RoyalblueTimelyProperties/venv/lib/python3.8/site-packages/fpdf/fpdf.py", line 1170, in _putpages
p = self.pages[n].encode("latin1") if PY3K else self.pages[n]
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 228-233: ordinal not in range(256)
I have uploaded the file to replit
already, does anyone know what to do?