Is it possible to convert a html file containing a JavaScript chart to PDF in python? - Stack Overflow

I am trying to convert a html file to pdf in python.The html file has a javascript chart. At first, I

I am trying to convert a html file to pdf in python. The html file has a javascript chart.
At first, I used weasyprint and pdfkit modules but I found that the modules does not support javascript.
So now I am using wkhtmltopdf module. It convert most of the html code to pdf, except javascript. Is it possible to convert an html file containing a JavaScript chart to PDF in python?
or should I use another module?
Below is the JavaScript code that does not appear in the pdf file.

<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 version is 0.12.4 and the mand is

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

I am trying to convert a html file to pdf in python. The html file has a javascript chart.
At first, I used weasyprint and pdfkit modules but I found that the modules does not support javascript.
So now I am using wkhtmltopdf module. It convert most of the html code to pdf, except javascript. Is it possible to convert an html file containing a JavaScript chart to PDF in python?
or should I use another module?
Below is the JavaScript code that does not appear in the pdf file.

<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 version is 0.12.4 and the mand is

$ wkhtmltopdf --javascript-delay 5000 test.html test.pdf
Share Improve this question edited Sep 18, 2017 at 22:10 BingbongKim asked Sep 18, 2017 at 21:49 BingbongKimBingbongKim 5791 gold badge6 silver badges20 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 5

Here is an example from a previous StackOverflow question. How to convert webpage into PDF by using Python

This example uses the library pfdkit

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

If it doesn't render the chart you could try using an iFrame with pdfkit to get desired results!

Here is an example using WeasyPrint First, install weasyprint.

pip install weasyprint

Then run example

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

Here is a third example, because i'm fun. :)

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."))
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_())

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745449877a4628219.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信