Search code examples
pythondjangoencodingutf-8reportlab

UTF-8 cannot decode ReportLab pdf


I'm having an issue regarding the decoding of pdf generated by reportlab library.

Here is the code calling my function:

print('Rendering Report and Sending Mail')
r = renderTestPDF()
sendTestPDF(r)

This is the code creating the PDF:

def renderTestPDF():

 canvas = Canvas('hello.pdf')
 canvas.drawString(72, 72, "Hello, World")
 content = canvas.getpdfdata()
 return content

This is the code sending the email and where I am getting the error:

def sendtestpdf(report):
# Variables For The Server
 emailhost = ''
 emailport = ''

# Variables For The Email
 systemEmail = ''
 password = ''
 recieveremail = ''

# Configure The Email Message
 message = MIMEMultipart()
 message["Subject"] = 'This is a test for pdf creation and attachment'
 message["From"] = systemEmail
 message["To"] = recieveremail

html = """
    <h1>Your report is here!</h1>
"""

# Turn The Message Into A MIME Object
part1 = MIMEText(html, "html")

**---- I THINK THIS IS WHERE THE ERROR IS LYING -----**
binary_pdf = open(report, "rb")
payload = MIMEBase('application', 'octate-stream', Name="Test.pdf")
payload.set_payload((binary_pdf).read())
encoders.encode_base64(payload)
payload.add_header('Content-Decomposition', 'attachment', filename='Test.pdf')

# Add HTML/plain-text parts to MIMEMultipart message
message.attach(part1)
message.attach(payload)

# Create Secure Connection with Server and Send Email
 context = ssl.create_default_context()
 with smtplib.SMTP_SSL(emailhost, emailport, context=context) as server:
     server.login(systemEmail, password)
     server.sendmail(systemEmail, recieveremail, message.as_string())

and lastly, this is the error I'm getting:

UnicodeDecodeError at /instruction/testreportgen/
'utf-8' codec can't decode byte 0x93 in position 10: invalid start byte



Unicode error hint
The string that could not be encoded/decoded was: 1.3 %���� R

The Value of the variable

(b'%PDF-1.3\n%\x93\x8c\x8b\x9e ReportLab Generated PDF document http://www.r'
 b'eportlab.com\n1 0 obj\n<<\n/F1 2 0 R\n>>\nendobj\n2 0 obj\n<<\n/BaseFont /He'
 b'lvetica /Encoding /WinAnsiEncoding /Name /F1 /Subtype /Type1 /Type /Font'
 b'\n>>\nendobj\n3 0 obj\n<<\n/Contents 7 0 R /MediaBox [ 0 0 595.2756 841.8'
 b'898 ] /Parent 6 0 R /Resources <<\n/Font 1 0 R /ProcSet [ /PDF /Text /Ima'
 b'geB /ImageC /ImageI ]\n>> /Rotate 0 /Trans <<\n\n>> \n  /Type /Page\n>>\ne'
 b'ndobj\n4 0 obj\n<<\n/PageMode /UseNone /Pages 6 0 R /Type /Catalog\n>>\ne'
 b"ndobj\n5 0 obj\n<<\n/Author (anonymous) /CreationDate (D:20220902151051-02'"
 b"00') /Creator (ReportLab PDF Library - www.reportlab.com) /Keywords () /ModD"
 b"ate (D:20220902151051-02'00') /Producer (ReportLab PDF Library - www.reportl"
 b'ab.com) \n  /Subject (unspecified) /Title (untitled) /Trapped /False\n>>\ne'
 b'ndobj\n6 0 obj\n<<\n/Count 1 /Kids [ 3 0 R ] /Type /Pages\n>>\nendobj\n7 0'
 b' obj\n<<\n/Filter [ /ASCII85Decode /FlateDecode ] /Length 100\n>>\nstrea'
 b'm\nGapQh0E=F,0U\\H3T\\pNYT^QKk?tc>IP,;W#U1^23ihPEM_?CW4KISi<!hk]\\"V$OBS=FsI'
 b"L%,#7S.n\\OO=Z(s/Wa->/cZ9h'*A~>endstream\nendobj\nxref\n0 8\n0000000000 6"
 b'5535 f \n0000000073 00000 n \n0000000104 00000 n \n0000000211 00000 n \n0000'
 b'000414 00000 n \n0000000482 00000 n \n0000000778 00000 n \n0000000837 00000'
 b' n \ntrailer\n<<\n/ID \n[<7494983efb930c60acac18528d411a2d><7494983efb930c60'
 b'acac18528d411a2d>]\n% ReportLab generated PDF document -- digest (http://'
 b'www.reportlab.com)\n\n/Info 5 0 R\n/Root 4 0 R\n/Size 8\n>>\nstartxref\n102'
 b'7\n%%EOF\n')

*EDITED

I'm a self taught 'programmer' so really don't have any knowledge about encoding and this kind of stuff


Solution

  • UPDATE: (MARKING AS ANSWER)

    It seems that the canvas created with ReportLab NEEDS to be saved as a .pdf before attempting to read it...

    For the reasons of my application, I do not want/need the pdf to be saved anywhere.

    A workaround for this is temporarily saving the pdf to the root dir, then opening it from the root dir, attaching it to the email, sending the email and then deleting it from the root dir.

    I can't confirm how effective or safe this is, but will look more into it now that I have a working solution.