1

私はhtmlからpdfを作成していますが、変換されますが画像はありません。私は絶対URLも試しましたが、それでも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

設定ファイル:

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/"

これで何が悪いのですか。pdf は正常に生成されましたが、画像がありません

html ファイル:

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

1 に答える 1

0

エラーを想像することさえできませんでした:私のcssで私は書いた:

.main{width:75%;}

このため、画像は表示されませんでした。ランダムに削除したところ、画像が表示され始めました

于 2016-06-08T10:10:05.470 に答える