0

我正在使用 ReportLab 生成一个非常简单的报告。它有一些文本、大约 15 张小图像和一些自定义元数据。它可以正常生成并在 Chrome 或 Edge 上打开,但不会在 Acrobat 或 Reader 中打开。当在后两者之一中打开时,它会说“期望一个 dict 对象”,然后只为每一页显示一个空白的白色方块。

我根本没有使用鸭嘴兽,只是 reportlab.pdfgen.canvas。我将在此处放置代码的匿名版本。另外,我对一般编程很陌生,所以请不要评判我的菜鸟错误(就像字面上设置一个变量i来增加;即使我知道一个人很懒(也请忽略表格是图像;那是因为...原因))。

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()
4

0 回答 0