我的问题是:我正在使用 xhtml2pdf 库从 html 创建 pdf 文件。创建 pdf 文件后,我使用 sendgrid API 通过电子邮件将该文件发送给用户。但是,我无法在 pdf 文件中保留嵌入的图像,因为应用程序返回给我“需要有效的文件名!” 信息。我在几个地方进行了研究,但找不到解决方案。使用的代码如下。
HTML 代码:
<img src="/static/media/logo.jpg" alt="Image">
Run Code Online (Sandbox Code Playgroud)
python代码(将html转换为pdf):
def link_callback(uri, rel):
"""
Convert HTML URIs to absolute system paths so xhtml2pdf can access those
resources
"""
# use short variable names
sUrl = settings.STATIC_URL
mUrl = settings.MEDIA_URL
mRoot = settings.MEDIA_ROOT
# convert URIs to absolute system paths
if uri.startswith(mUrl):
path = os.path.join(mRoot, uri.replace(mUrl, ""))
else:
return uri # handle absolute uri (ie: http://some.tld/foo.png)
# make sure that …Run Code Online (Sandbox Code Playgroud)