【问题标题】:Getting the Base URI for Weasyprint in Python在 Python 中获取 Weasyprint 的基本 URI
【发布时间】:2015-10-13 02:19:02
【问题描述】:

我正在使用 Python 的 Weasyprint 库来尝试将 html 文件打印为 pdf。我正在尝试将图像嵌入到页面的背景中。这是代码:

HTML(string='''
    <h1>The title</h1>
    <p>Content goes here
''', base_url=os.path.dirname(os.path.realpath(__file__))).write_pdf("hello.pdf",  stylesheets=[CSS(string='body{background-image: url("example_image.png")}')])

我得到此代码的输出如下:

Ignored `background-image: url("example_image.png")` at 1:6, Relative URI reference without a base URI: 'example_image.png'.
blah@blah:~/Dropbox/Terraverde/annual_reports$ python3 test_excel.py

我已尝试在 Stackoverflow 上搜索此问题的解决方案,并已阅读文档,但我能找到的最接近答案的是以下关于 Django 的相同问题的帖子:Django WeasyPrint CSS integration warning: Relative URI reference without a base URI: <link href="/static/css/bootstrap.min.css"> at line None

我也尝试在我的代码中使用 document.baseURI:

base_url=os.path.dirname(os.path.realpath(__file__))).write_pdf("hello.pdf",  stylesheets=[CSS(string='body{background-image: url(document.baseURI + "example_image.png")}')])

但这仍然产生了错误:

Parse error at 1:24, unexpected BAD_URI token in property value

关于如何处理问题的任何建议,或者对于常规 Python 或 Flask 可能类似于 Django 的 request.build_absolute_uri() 的命令?

【问题讨论】:

    标签: python css flask weasyprint


    【解决方案1】:

    在 OSX 上,我在使用 pystache 渲染的模板中遇到了同样的错误:

    <img src="/Users/my_username/my_project/my_image.png" />
    

    所以我尝试了这个,它成功了:

    <img src="file:///Users/my_username/my_project/my_image.png" />
    

    只需在 /Users/... 路径之前添加 file://。 (注意是 3 个斜线)。

    【讨论】:

      【解决方案2】:

      我发现base_url 必须作为参数提供给weasyprint.CSS 函数,而不是weasyprint.HTML 之一:

      from weasyprint import HTML, CSS
      
      html_content = '''<h1>The title</h1><p>Content goes here'''
      
      base_url = os.path.dirname(os.path.realpath(__file__))
      css = CSS(string='body{background-image: url("example_image.png")}', base_url=base_url)
      
      HTML(string=html_content).write_pdf("hello.pdf", stylesheets=[css])
      

      作为奖励,加载位于该脚本旁边的 fonts 文件夹中的本地字体也是如此:

      # for debugging
      import logging
      logger = logging.getLogger('weasyprint')
      logger.addHandler(logging.StreamHandler())
      
      import os
      
      from weasyprint import HTML, CSS
      from weasyprint.fonts import FontConfiguration
      
      
      html_content = '''<h1>The title</h1><p>Content goes here</p>'''
      
      font_config = FontConfiguration()
      
      THIS_FILE_DIR = os.path.dirname(os.path.abspath(__file__)) + os.sep
      base_url = 'file://' + THIS_FILE_DIR
      
      # fonts downloaded from
      # https://fonts.google.com/specimen/Poppins?preview.text_type=custom&sidebar.open=true&selection.family=Poppins:wght@400;500;600;700
      css = CSS(string='''
              @font-face {
                font-family: 'Poppins';
                src: url('./fonts/Poppins-Regular.ttf') format('truetype');
                font-weight: 400;
                font-style: normal;
              }
              
              @font-face {
                font-family: 'Poppins';
                src: url('./fonts/Poppins-Medium.ttf') format('truetype');
                font-weight: 500;
                font-style: normal;
              }
              
              @font-face {
                font-family: 'Poppins';
                src: url('./fonts/Poppins-SemiBold.ttf') format('truetype');
                font-weight: 600;
                font-style: normal;
              }
              
              @font-face {
                font-family: 'Poppins';
                src: url('../base/fonts/Poppins-Bold.ttf') format('truetype');
                font-weight: 700;
                font-style: normal;
              }
              ''', font_config=font_config, base_url=base_url)
      
      HTML(string=html_content).write_pdf("hello.pdf", stylesheets=[css])
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-21
        • 1970-01-01
        • 2017-08-21
        • 2021-07-10
        • 2010-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多