【问题标题】:Is it possible to convert a html file containing a JavaScript chart to PDF in python?是否可以在 python 中将包含 JavaScript 图表的 html 文件转换为 PDF?
【发布时间】:2018-02-27 13:05:52
【问题描述】:

我正在尝试在 python 中将 html 文件转换为 pdf。 html 文件有一个javascript 图表。
一开始我使用了weasyprint 和pdfkit 模块,但我发现这些模块不支持javascript。
所以现在我正在使用 wkhtmltopdf 模块。它将大部分 html 代码转换为 pdf,除了 javascript。是否可以在 python 中将包含 JavaScript 图表的 html 文件转换为 PDF?
或者我应该使用另一个模块?
以下是pdf文件中没有出现的JavaScript代码。

<script type="text/javascript">

FusionCharts.ready(function(){

  var fusioncharts = new FusionCharts({

  type: 'hlineargauge',

  renderAt: 'chart_container',

  width: '350px',

  height: '170px',

  dataFormat: 'json',

  dataSource: {

      "chart": {

          "theme": "fint",

          "caption": "Chart A",

          "lowerLimit": "0",

          "upperLimit": "20",

          "chartBottomMargin": "40",

          "valueFontSize": "11",

          "valueFontBold": "z0"

      },

      "colorRange": {

          "color": [{

              "minValue": "0",

              "maxValue": "11.5",

              "label": "Low",

              "code" : "#FDB881",

          }, {

              "minValue": "11.5",

              "maxValue": "12.5",

              "label": "Typical",

              "code" : "#F18B36",

          }, {

              "minValue": "12.5",

              "maxValue": "20",

              "label": "High",

              "code" : "#D2660D",

          }]

      },

      "pointers": {

          "pointer": [{

              "value": "8",

              'borderColor':'#333333',

              'borderThickness':'3',

              'borderAlpha':'100',

              'bgColor':'#FF0000'
          }]
      },
  }
}
);
  fusioncharts.render();
});
</script>

wkhtmltopdf版本为0.12.4,命令为

$ wkhtmltopdf --javascript-delay 5000 test.html test.pdf

【问题讨论】:

    标签: javascript python html pdf


    【解决方案1】:

    这是上一个 StackOverflow 问题的示例。 How to convert webpage into PDF by using Python

    本例使用库pfdkit

    import pdfkit
    pdfkit.from_url('http://google.com', 'out.pdf')
    

    如果它不呈现图表,您可以尝试使用带有 pdfkit 的 iFrame 来获得所需的结果!

    这是一个使用WeasyPrint 的示例 首先,安装 weasyprint。

    pip install weasyprint
    

    然后运行示例

    python
    >>> pdf = weasyprint.HTML('http://www.google.com').write_pdf()
    >>> len(pdf)
    92059
    >>> file('google.pdf', 'w').write(pdf)
    

    这是第三个例子,因为我很有趣。 :)

    import sys 
    from PyQt4.QtCore import *
    from PyQt4.QtGui import * 
    from PyQt4.QtWebKit import * 
    
    app = QApplication(sys.argv)
    web = QWebView()
    web.load(QUrl("http://www.yahoo.com"))
    printer = QPrinter()
    printer.setPageSize(QPrinter.A4)
    printer.setOutputFormat(QPrinter.PdfFormat)
    printer.setOutputFileName("fileOK.pdf")
    
    def convertIt():
        web.print_(printer)
        print "Pdf generated"
        QApplication.exit()
    
    QObject.connect(web, SIGNAL("loadFinished(bool)"), convertIt)
    sys.exit(app.exec_())
    

    【讨论】:

    • 谢谢史蒂文。实际上网站渲染得很好,但我不知道为什么我的 html 文件中的 javascript 图表没有渲染。
    • 也许可以尝试使用 $(function() { });所以它是在页面加载时呈现的?也许它在加载所有脚本之前正在运行脚本?
    • 即使将其包装在 $(function() { }); 中也无法正常工作任何建议@BingbongKim
    • WeasyPrint 不执行 JavaScript。 See the issue
    猜你喜欢
    • 2019-05-28
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 2019-10-25
    • 2015-08-17
    • 1970-01-01
    相关资源
    最近更新 更多