简体   繁体   English

xhtml2pdf没有选择图像

[英]xhtml2pdf not picking image

i am creating pdf from html and it gets converted but without image. 我正在从html创建pdf并且它被转换但没有图像。 i have tried the absolute urls also but it still doesn't work pdf function: 我也试过绝对的网址,但它仍然无法正常工作:

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

settings file : 设置文件:

PROJECT_ROOT = "/var/www/html/newclone/userapi/" 
MEDIA_ROOT = path.join(PROJECT_ROOT,'media')
MEDIA_URL = '/media/'
STATIC_ROOT = path.join(PROJECT_ROOT,'static-root')
STATIC_URL =  "/static/"

What is wrong in this. 这有什么不对。 pdf is generated successfully but the images are missing pdf生成成功但图像丢失

html file : html文件:

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

Could not even imagine the error : in my css i have written : 甚至无法想象错误:在我的CSS中我写过:

.main{width:75%;}

because of this images were not visible. 因为这个图像不可见。 I just removed it randomly and images started displaying 我随意删除它,图像开始显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM