【问题标题】:Django Reportlab getKeepWithNextDjango Reportlab getKeepWithNext
【发布时间】:2021-12-02 05:35:28
【问题描述】:

我正在尝试将图像添加到 Reportlab 中生成的 PDF。我正在尝试从 Django 字段访问图像,并指定文件的完整路径。

当我运行以下代码时,我得到:“异常值:getKeepWithNext”。

任何关于我做错了什么的帮助将不胜感激。

def holding_pdf(self, course_slug, holding_slug):
        buffer = io.BytesIO()
        holding = HoldingDetail.objects.get(identifier=holding_slug)
        doc = SimpleDocTemplate(buffer,
                                rightMargin=72,
                                leftMargin=72,
                                topMargin=72,
                                bottomMargin=72,
                                pagesize=A4,
                                title=f"Why the {holding.name} is in the portfolio.pdf")
        elements = []
        styles = getSampleStyleSheet()
        elements.append(Paragraph(str(holding.logo.path), styles['Normal']))
        elements.append(Image(holding.logo.path))
        print(holding.logo.path)
        doc.build(elements)
        buffer.seek(0)
        return FileResponse(buffer, as_attachment=False, filename=f"Why the {holding.name} is in the portfolio.pdf")

【问题讨论】:

  • 你能发布更多关于异常的信息吗?
  • AttributeError at /leads/investsense-hub24/why/ETL0484AU/pdf/getKeepWithNext 请求方法:GET 请求 URL:localhost:8000/leads/investsense-hub24/why/ETL0484AU/pdf Django 版本:3.2.9 异常类型:AttributeError 异常值:getKeepWithNext 异常位置: C:\Users\danie\Python\Django\PortfolioSense\env\lib\site-packages\PIL\Image.py,第 546 行,在 getattr Python 可执行文件:C:\Users\danie\ Python\Django\PortfolioSense\env\Scripts\python.exe Python 版本:3.8.5

标签: django reportlab


【解决方案1】:

我认为您使用的 Image 类不正确。验证文件中的 python 导入,并确保使用 reportlab 提供的 Image flowable 类。

from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Paragraph, SimpleDocTemplate, Image

【讨论】:

  • 解决了,非常感谢!我意识到我已经导入了“从 reportlab.platypus 导入图像”,但是后来导入了“从 PIL 导入图像”,它覆盖了鸭嘴兽的导入。我现在已经删除了“从 PIL 导入图像”,它工作得很好。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-28
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
  • 1970-01-01
  • 2013-09-10
相关资源
最近更新 更多