【问题标题】:Convert html to pdf with material icons in python using pdfkit使用 pdfkit 在 python 中使用材料图标将 html 转换为 pdf
【发布时间】:2018-08-19 00:08:10
【问题描述】:

我需要将一个页面呈现为包含 Material 图标的 PDF 文件,但它似乎不起作用。

templates/pdf.html

<html>
<head>
  <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
  <link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css">
  <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">

  <style>
    * {
      font-family: Roboto, Helvetica, sans-serif;
    }
</style>
</head>
<body>
  Just test 
  <i class="material-icons">remove_red_eye</i>
</body>
</html>

gen.py

import pdfkit
from django.template.loader import render_to_string

html = render_to_string('pdf.html')
pdfkit.from_string(html, 'out.pdf')
# I tried this version but it doesn't work either
# pdfkit.from_string(html, 'out.pdf', css=['https://fonts.googleapis.com/icon?family=Material+Icons'])

out.pdf 仅包含文本 Just test。有机会解决吗?

我刚刚还在 pdfkit 存储库中创建了这个问题 - issue

【问题讨论】:

  • html 看起来像什么?我的意思是像 print(html)
  • 如果您使用的是 django.. 尝试django-easy-pdf.. 看看这里(第二个答案)... 如果它不起作用,请告诉我,我会看看但我认为它会在这种情况下帮助你stackoverflow.com/questions/45233906/…
  • 也遇到了同样的问题。我只是想办法让它工作。将在下面添加答案

标签: python pdfkit


【解决方案1】:

有 2 个问题需要解决,我以可接受的方式完成了。

  1. CSS。

    • 只有inline styles 工作。外部样式不起作用。

    • wkhtmltopdf支持-webkit-前缀,所以一些css属性需要添加前缀才能正常工作。

    • 如果需要,请下载外部 css 文件。在 django 模板中使用 &lt;style&gt;{% include 'style.css' %}&lt;/style&gt; 来分离出 css 文件。

  2. 图标字体和自定义字体不起作用。

    • 下载所需的 svg 图标,而不是使用图标字体。

    一个。使用{% include 'icon.svg' %} 包含字体图标

    b.仅支持 svg xml 中的 pathfill 元素

    c。您可以在 svg xml 内容中更改 svg 的颜色和高度。

    d。在Material Icon下载svgs

    e。您可以使用 png 代替,但更改图标颜色和大小并不容易。

    • 使用系统字体或在服务实例中手动安装字体。

【讨论】:

    猜你喜欢
    • 2018-04-29
    • 1970-01-01
    • 2011-11-26
    • 2022-08-17
    • 2015-03-25
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    相关资源
    最近更新 更多