Django - 使用pdf视图时不加载图像

时间:2017-04-13 23:06:05

标签: python django

我正在使用django-easy-pdf,我有以下观点:

class PrescriptionView(PDFTemplateView):
    template_name = 'meds/prescription.html'
    context_object_name = 'meds'
    model = Medicament

    def get_queryset(self):
        ids = map(int, self.kwargs['selected'].split(","))
        return Medicament.objects.filter(id__in=ids)

    def get_context_data(self, **kwargs):
        context = super(PrescriptionView, self).get_context_data(pagesize="A5", title="Ordonnance", **kwargs)
        return context

我有以下html页面:

{% load staticfiles %}
<html>
    <head>
        <meta charset="UTF-8">
        <style>
            @page { size: A5;
                @frame headerLogo {
                    top: 23mm;
                    left: 20mm;
                    width: 50mm;
                    height: 24mm;
                    -pdf-frame-content: headerLogo;
                }
            }
        </style>
    </head>
    <body>
    <div id="headerLogo">
        <img src="{% static 'img/banner.png' %}">
    </div>
</body>

但是banner.png图片没有加载,我不断收到以下错误:

missing explicit frame definition for content or just static frames

getFile '/static/img/banner.png' None '/home/nids/cabin'
'<div id="headerLogo"> <img src="/static/img/banner.png"/> </div>'
Traceback (most recent call last):
  File "/home/nids/cabin/dentist/lib/python3.5/site-packages/xhtml2pdf/context.py", line 828, in _getFileDeprecated
    nv = self.pathCallback(name, relative)
  File "/home/nids/cabin/dentist/lib/python3.5/site-packages/easy_pdf/rendering.py", line 35, in fetch_resources
    path = os.path.join(settings.STATIC_ROOT, uri.replace(settings.STATIC_URL, ""))
  File "/home/nids/cabin/dentist/lib/python3.5/posixpath.py", line 89, in join
    genericpath._check_arg_types('join', a, *p)
  File "/home/nids/cabin/dentist/lib/python3.5/genericpath.py", line 143, in _check_arg_types
    (funcname, s.__class__.__name__)) from None
TypeError: join() argument must be str or bytes, not 'NoneType'
Need a valid file name!
'<img src="/static/img/banner.png"/>'
line 0, msg: missing explicit frame definition for content or just static frames, fragment: 
line 0, msg: getFile %r %r %r, fragment: '<div id="headerLogo"> <img src="/static/img/banner.png"/> </div>'
line 0, msg: Need a valid file name!, fragment: '<img src="/static/img/banner.png"/>'

我的静态文件在其他视图中没有问题,除了这个pdf one!

1 个答案:

答案 0 :(得分:-1)

差不多10个小时试图解决这个问题,并且OH MA GOD,它已经解决了。 问题非常简单 - 重新安装此软件包

1.pip install --pre xhtml2pdf。

2.pip install --pre reportlab。

3.pip install django-easy-pdf。

请记住之前卸载此命令。 还有一件事,尝试在纯HTML上设置高度和宽度为img标签。