PDF generated with ReportLab won't open in Adobe Acrobat; "Expected a dict object."

282 Views Asked by At

I'm generating a pretty simple report using ReportLab. It has some text, about fifteen small images, and some custom metadata. It generates fine and opens on Chrome or Edge, but won't open in Acrobat or Reader. When opened in one of the latter two, it says it "Expected a dict object" and then just diplays an empty white square for each page.

I'm not using Platypus at all, just reportlab.pdfgen.canvas. I'll put an anonymized version of the code here. Also, I'm pretty new to programming in general so please don't judge my rookie mistakes (like literally setting a variable i for incrementing; even I know that one's lazy (and also please ignore that the tables are images; that's because of...reasons)).

from datetime import datetime
from reportlab.pdfgen import canvas
import pdf_report_utils as pu
import plotly.graph_objects as go

def create_pdf_canvas(year_int, month_int):
    # Create PDF and give PDF a document title
    start_as_date = datetime. strptime(start, '%Y-%m-%d %H:%M:%S')
    month_name = start_as_date.strftime('%B')
    year_name  = start_as_date.strftime('%Y')
    pdf = canvas.Canvas('pdfs/OSP Monthly Summary Report DRAFT - ' +
                        year_name + ' ' + month_name + '.pdf',
                        pagesize='letter'
                        )
    pdf.setTitle('Company Inc Monthly Summary Report')

    # Setting metadata
    pdf.setAuthor("Someone via ReportLab in Python")
    pdf.setTitle("Company Inc Monthly Summary Report DRAFT - " +
                    year_name + " " + month_name)
    pdf.setSubject("An auto-generated report of various data.")
    
    # Making title backdrop
    pdf.setFillColorRGB(237/256,238/256,239/256)
    pdf.rect(0, 660, 750, 300, stroke=0, fill=1)

    # Making title
    pdf.setFillColorRGB(118/256,168/256,53/256)
    pdf.setFontSize(size=32)
    pdf.drawString(50, 700, 'Company Inc')
    pdf.setFillColorRGB(0,0,0)
    pdf.drawString(125, 700, 'Monthly Summary Report')

    # Making subtitle 
    pdf.setFontSize(size=14)
    pdf.drawString(50, 680, f'For {month_name}, {year_name}')

    # Making section header
    pdf.setFontSize(size=18)
    pdf.drawString(50, 630, 'Summary Tables')

    #Drawing all of the tables
    pdf.setFontSize(size=10)
    px = 610
    i = 0
    for title in section_titles:
        # If statement checks to see if it should loop to next page
        if i+1 <= len(section_titles): # This keeps it within the list dims
            if px - section_title_height - table_height[i]/2 < 50:
                pdf.showPage()
                pdf.setFontSize(size=14)
                pdf.drawString(50, 700, 'Summary Tables (cont.)')
                pdf.setFontSize(size=10)
                px = 680 # resetting px to top of page
        px = px - section_title_height
        pdf.drawString(50, px, title)
        px = px - table_height[i]/2 - 5
        pdf.drawImage('images/table'+ str(i+1) +'.jpg', 
                        50, px, 510, table_height[i]/2)
        i = i+1
    
    #=============Charts Page 1============
    pdf.showPage()  #New page, charts added in next lines
    pdf.drawImage('images/chart1.jpg', 50, 400, 510, 300)
    pdf.drawImage('images/chart2.jpg', 50,  50, 510, 300)
    pdf.setFontSize(size=18)
    pdf.drawString(50, 700, 'Summary Charts')
    
    #=============PAGE 3============
    pdf.showPage()  #New page, charts added in next lines
    pdf.drawImage('images/chart3.jpg', 50, 400, 510, 300)
    pdf.drawImage('images/chart4.jpg', 50,  50, 510, 300)

    # #=============PAGE 4============
    # pdf.showPage()  #New page, charts added in next lines
    # pdf.drawImage('images/chart5.jpg', 50, 400, 510, 300)
    # pdf.drawImage('images/chart6.jpg', 50,  50, 510, 300)


    # Saving PDF
    pdf.save()
0

There are 0 best solutions below