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

问题描述 投票:0回答:3

换算:

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

html css django html2pdf xhtml2pdf
3个回答
7
投票

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

您需要提供一个内部样式表 - 这意味着将您的样式写入 HTML 模板头部的标签之间

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

但是请注意,许多 CSS 样式不受支持。请参阅文档了解支持哪些 CSS。

示例模板如下:

<!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>
    

0
投票
图像、背景和样式表从 HTML 文档加载。通常 xhtml2pdf 期望在本地驱动器上找到这些文件。它们也可以相对于原始文档进行引用。但程序员可能希望通过 HTTP 请求或从数据库或其他任何方式加载不同类型的源,例如 Internet。因此,您可以定义一个 link_callback 来处理这些请求。

根据文档,肯定有一种方法可以通过 link_callback 函数添加外部样式表,尽管我不知道如何做到这一点。有人可以帮忙解决这个问题吗?具体来说,在 django 中加载静态文件。


0
投票
自 2024 年起,您可以链接到外部样式表,如下所示:

输入文件:

<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title>Assurance Report</title> <link rel="stylesheet" type="text/css" href="Styles\assuranceReport.css" /> </head> <body> ...

Python(在 Windows 上)

from xhtml2pdf import pisa with open('Docs\\assuranceReport.html', 'r') as input_file: source_html = input_file.read() result_file = open("My.pdf", "w+b") pisa_status = pisa.CreatePDF( source_html, #default_css - no need for anything here dest=result_file) result_file.close() # return False on success and True on errors return pisa_status.err
需要注意的一点是,您的 CSS 文件必须保存为 UTF-8 而不是 UTF-8-BOM,否则 xhtml2pdf 将难以解析它:

© www.soinside.com 2019 - 2024. All rights reserved.