xhtml2pdf未拾取图像

2024-05-15 17:11:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我从html创建pdf,它被转换,但没有图像。我也尝试过绝对网址,但仍然不起作用 pdf函数:

def test_pdf(request):

    template = get_template('../templates/index.html')
    html = template.render(Context(data))
    filename = 'pdfs/'+str(random.random())+'.pdf'

    file = open(filename, "w+b")
    pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file, encoding='utf-8',link_callback=link_callback)
    # Return PDF document through a Django HTTP response
    file.seek(0)
    pdf = file.read()
    file.close()            # Don't forget to close the file handle
    return HttpResponse({"success":"success"})

def link_callback(uri, rel):

    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/


    if uri.startswith(mUrl):
        path = os.path.join(mRoot, uri.replace(mUrl, ""))

    elif uri.startswith(sUrl):
        path = os.path.join(sRoot, uri.replace(sUrl, ""))

    else:
        return uri  # handle absolute uri (ie: http://some.tld/foo.png)

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

设置文件:

^{pr2}$

这有什么不对。已成功生成pdf,但缺少图像

html文件:

<div class="h1"><img src="/media/xyz"></div>

Tags: pathreturnsettingspdfhtmlcallbacklinkstatic
1条回答
网友
1楼 · 发布于 2024-05-15 17:11:35

甚至无法想象错误: 在我的css中我写了:

.main{width:75%;}

因为这个图像是不可见的。我只是随意地把它取下来,图像开始显示出来

相关问题 更多 >