1

我的观点设置如下:

视图.py

class PDFTemplateView(TemplateView):
    Model = TemplateInfo
    template_name = 'hello.html'


    def get(self, *args, **kwargs):
        obj = self.Model.objects.get(id = kwargs['pk'])

        html  = get_template(self.template_name).render(Context({'object' : obj}))

        result = StringIO.StringIO()
        rendering = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)

        if not rendering.err:
            return HttpResponse(result.getvalue(), mimetype='application/pdf')
        return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))

我在这个提要中找到了一个定义fetch_resource()函数的解决方案,但这对我没有帮助。我阅读了文档,没有这个功能我会更好。

这是我的模板“hello.html”

<style type="text/css">

@page {
    background-image: url('/media/image/mainbg.jpg'); #this wouldnot give image too
    size: letter portrait;

    @frame header_frame {           /* Static Frame */
        -pdf-frame-content: header_content;
        left: 50pt; width: 512pt; top: 50pt; height: 40pt;
        -pdf-frame-border: 1;    /* for debugging the layout */
    }
    @frame content_frame {          /* Content Frame */
        left: 50pt; width: 512pt; top: 90pt; height: 632pt;
        -pdf-frame-border: 1;    /* for debugging the layout */
    }
    @frame footer_frame {           /* Another static Frame */
        -pdf-frame-content: footer_content;
        left: 50pt; width: 512pt; top: 772pt; height: 20pt;
        -pdf-frame-border: 1;    /* for debugging the layout */
    }

 </style>
 </head>
<div id="header_content">Lyrics-R-Us</div>

<div id="footer_content">(c) - page <pdf:pagenumber>
    of <pdf:pagecount>
</div>

<ul>
    <li>{{ object.emp_name }}</li>
    <li>{{ object.designation }}</li>
    <li>{{ object.image.url }}</li>
</ul>

到目前为止,一切似乎都很好。但我无法在 pdf 中获取图像。这{{ object.image.url}}给了我一个 pdf 中的路径字符串,但不是图像。我是不是错过了什么。请帮帮我。我已经被困了几个小时了。

4

5 回答 5

3

问题是 xhtml2pdf 无法找到这些图像,除非您定义一个 link_callback 函数来定义静态和媒体文件的路径。

这个函数在 xhtml2pdf 文档中:

def link_callback(uri, rel):
    # use short variable names
    sUrl = settings.STATIC_URL      # Typically /static/
    sRoot = settings.STATIC_ROOT    # Typically /home/userX/project_static/
    mUrl = settings.MEDIA_URL       # Typically /static/media/
    mRoot = settings.MEDIA_ROOT     # Typically /home/userX/project_static/media/

    # convert URIs to absolute system paths
    if uri.startswith(mUrl):
        path = os.path.join(mRoot, uri.replace(mUrl, ""))
    elif uri.startswith(sUrl):
        path = os.path.join(sRoot, uri.replace(sUrl, ""))

    # make sure that file exists
    if not os.path.isfile(path):
        raise Exception('media URI must start with %s or %s' % (sUrl, mUrl))
    return path

确保在 link_callback 函数(STATIC_URL...等)中定义所有路径。然后在渲染文档时,像这样包含 link_callback:

rendering = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result, link_callback=link_callback)
于 2015-01-07T20:58:40.027 回答
1

我遇到了同样的问题。请在设置中检查您的 STATIC_ROOT,因为它应该指向您存储静态文件的位置。

django-easy-pdf 无法找到您的资产(图像、css 等),直到您将它们指向 STATIC_ROOT。

https://github.com/nigma/django-easy-pdf/blob/master/docs/usage.rst

于 2015-07-17T06:02:26.120 回答
0

Xhtmltopdf 不使用“真正的”css 规则。您的背景图片必须为 pdf 格式。尝试将 '/media/image/mainbg.jpg' 转换为 pdf 并使用background-image: url('/media/image/mainbg.pdf').

于 2015-01-07T09:20:21.263 回答
0

在我删除之前,我一直在为类似的问题苦苦挣扎:

<pdf:pagecount>

试试看

于 2015-08-28T10:30:12.057 回答
-1

迟到的答案,但以上都没有为我解决。

这是为我解决问题的方法:

在 settings.py 中定义 STATIC_ROOT 为:

os.path.abspath('collected-static/')

另一个问题是 link_callback 函数的编写方式,您必须定义 MEDIA_URL,因为 MEDIA_URL 的默认值为 "" ,它与每个 URI 的开头匹配。

定义 MEDIA_URL 或重新编写 link_callback 函数以首先检查静态链接:

def link_callback(uri, rel):
    # use short variable names
    sUrl = settings.STATIC_URL      # Typically /static/
    sRoot = settings.STATIC_ROOT    # Typically /home/userX/project_static/
    mUrl = settings.MEDIA_URL       # Typically "" if not defined in settings.py
    mRoot = settings.MEDIA_ROOT     # Typically /home/userX/project_static/media/

    # convert URIs to absolute system paths
    if uri.startswith(sUrl):
        # Replaces 'static/image.png' with 'c:\\my-project\\collected-static/image.png'
        path = os.path.join(sRoot, uri.replace(sUrl, ""))
    elif uri.startswith(mUrl):
        # MEDIA_URL default value is "" so everything matches this
        path = os.path.join(mRoot, uri.replace(mUrl, ""))

    # make sure that file exists
    if not os.path.isfile(path):
        raise Exception('media URI must start with %s or %s' % (sUrl, mUrl))
    return path
于 2019-10-24T10:39:18.747 回答