【问题标题】:Image file can't covert to pdf when using python-pdfkit使用 python-pdfkit 时图像文件无法转换为 pdf
【发布时间】:2022-08-17 01:04:27
【问题描述】:

这是我的代码

options = {
      \"enable-local-file-access\": True,
      ...
}
pdfkit.from_string(html_file, pdf_file, options=options, ...)

因为我使用 Django 模板,所以这是我的代码来引用它

 <img src=\"{{ static_root }}{% static \'../../target.svg\' %}\" alt=\"\">

我在 html 中使用本地图像文件,它只是在 pdf 输出文件中显示一个空白框

我还尝试使用 \"base64\" 来解决我的问题,基于此链接pdfkit not converting image to pdf

它对我真的不起作用。

  • django 为浏览器生成带有 URL 的 HTML,它可以向服务器请求图像。但是pdfkit 像普通程序一样工作,不能向服务器发送图像请求,我宁愿使用普通路径/full/path/to/file
  • It doesn\'t really work to me. 是无用的信息。你运行的时候有没有报错?你如何使用base64?更好地显示您的代码。
  • 嘿嘿,谢谢大家回答我的问题。我意识到在这种情况下我不能使用 .svg 格式的图像,而且我不知道究竟是为什么,但是 .png 对我来说非常有效,这解决了我的问题。
  • 当你在 django 中使用 PNG 时,你有这个问题吗?
  • 我记得PDF 是在SVG 发明之前发明的,也许PDF 这种格式有问题。或 pdfkit 对此格式有疑问。

标签: python django wkhtmltopdf pdfkit python-pdfkit


【解决方案1】:

意识到pdfkit不喜欢.svg,我改用.png格式

【讨论】:

    【解决方案2】:

    wkhtmltopdf 0.12.6 (Ubuntu 22.04.1 LTS)

    选项= {'启用本地文件访问':无}

    pdfkit.from_file("file.html", file_path, options=options) # 没有图片:(

    pdfkit.from_string(html_str, file_path, options=options) # 没问题!

    【讨论】:

      猜你喜欢
      • 2016-11-14
      • 1970-01-01
      • 2018-04-29
      • 2021-12-12
      • 2020-06-27
      • 2018-08-19
      • 1970-01-01
      • 1970-01-01
      • 2014-06-13
      相关资源
      最近更新 更多