gpt4 book ai didi

html - xhtml2pdf 在创建 pdf 时不应用 css(html 到 pdf)

转载 作者:太空宇宙 更新时间:2023-11-04 01:15:11 24 4
gpt4 key购买 nike

转换:

html = template.render(context)
resultFile = open(filepath, "w+b")
pdf = pisa.CreatePDF(html.encode('utf-8'), dest=resultFile,encoding='utf-8', link_callback=link_callback)

链接回调

def link_callback(uri, rel):
sUrl = settings.STATIC_URL # Typically /static/
sRoot = settings.STATIC_ROOT
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, ""))
else:
return uri # handle absolute uri (ie: http://some.tld/foo.png)

# 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

我使用 xhtml2pdf 创建了 pdf。该文件已创建,但根本未对其应用 css。我已经检查了从 link_callback 返回的路径,它是正确的,但在 pdf 中根本没有应用任何 css。

CSS:

#page_1 {position:relative; overflow: hidden;margin: 64px 0px 65px 0px;padding: 0px;border: none;}

#page_1 #p1dimg1 {position:absolute;top:0px;left:20%;z-index:-1;width:600px;height:870px;}
#page_1 #p1dimg1 #p1img1 {width:600px;height:870px;}

#page_2 {position:relative; overflow: hidden;padding-left: 100px;border: none;height: 854px;}

#page_2 #p2dimg1 {position:absolute;left:27%;z-index:-1;width:490px;height:669px;}
#page_2 #p2dimg1 #p2img1 {width:490px;height:669px;}

我得到的 css 路径:/Users/mypc/project/project/static/css/lender_borrower.css

最佳答案

xhtml2pdf 不允许链接到外部样式表。

您需要提供一个内部样式表——这意味着在 HTML 模板的头部,标签之间编写您的样式

<style type="text/css">HERE</style>

但是请注意,许多 CSS 样式不受支持。请参阅 what CSS is supported 的文档.

示例模板如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Whatever</title>
<style type="text/css">

# PLACE ALL YOUR CSS STYLING HERE

</style>
</head>
<body>

# content goes here

</body>
</html>

关于html - xhtml2pdf 在创建 pdf 时不应用 css(html 到 pdf),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50308254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com