0

I'm trying to print highcharts in pdf file using wicked_pdf, wkhtmltopdf-binary gems in rails. In Gemfile I have added those two gems. below is my index.pdf.erb

<%= javascript_include_tag 'application' %>
<%= javascript_include_tag "https://code.highcharts.com/highcharts.js" %>
<%= javascript_include_tag "https://code.highcharts.com/modules/exporting.js" %>
<%= javascript_include_tag "https://code.highcharts.com/modules/export-data.js" %>


    <% @products.each do |product| %>
       <%= product.name %>
    <% end %>
<div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>

<script>
  Highcharts.chart('container', {
    chart: {
        plotBackgroundColor: null,
        plotBorderWidth: null,
        plotShadow: false,
        type: 'pie'
    },
    title: {
        text: 'Browser market shares in January, 2018'
    },
    tooltip: {
        pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
    },
    plotOptions: {
        animation: false,
        pie: {
                  animation: false,
    enableMouseTracking: false,
    shadow: false,
            allowPointSelect: true,
            cursor: 'pointer',
            dataLabels: {
                enabled: true,
                format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                style: {
                    color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                }
            }
        }
    },
    series: [{
        #{request.format == 'pdf' ? 'enableMouseTracking: false, shadow: false, animation: false' : nil}
        name: 'Brands',
        colorByPoint: true,
        data: [{
            name: 'Chrome',
            y: 61.41,
            sliced: true,
            selected: true
        }, {
            name: 'Internet Explorer',
            y: 11.84
        }]
    }]
});
</script>

but highcharts are not rendering in pdf file. Can anyone help me out this.

Unixmonkey
  • 18,485
  • 7
  • 55
  • 78
Rebel Rider
  • 165
  • 16
  • 1
    Is this line `#{request.format == 'pdf' ? 'enableMouseTracking: false, shadow: false, animation: false' : nil}` meant to be a ruby ternary operator? If so it should be enclosed with `<%= ... %>` – akaimo Apr 17 '19 at 19:00
  • @akaimo yeah it is ternary operator and you said I enclosed with <%= %> but still not rendering the chart in pdf – Rebel Rider Apr 18 '19 at 07:29
  • Hi @Rebel Rider, Your chart options are correct: http://jsfiddle.net/BlackLabel/x8w3bjry/, so the problem is probably not caused by JS code. Please make sure your environment meets the Highcharts requirements: https://www.highcharts.com/docs/getting-started/system-requirements – ppotaczek Apr 18 '19 at 10:55
  • 1
    Charting libraries can be difficult to get working with `wicked_pdf`, mostly because `wkhtmltopdf` uses a very old version of the webkit rendering engine. Here's a post where someone had some success: https://stackoverflow.com/q/36112020/23915 – Unixmonkey Apr 18 '19 at 13:39
  • @Unixmonkey I have tried the same by downgrading gem but getting error, Failed to execute: .rvm/gems/ruby-2.3.3/gems/wkhtmltopdf-binary-0.1.11/bin/wkhtmltopdf:14: Invalid return (SyntaxError) – Rebel Rider Apr 19 '19 at 07:54
  • @RebelRider Is the SyntaxError in your code, or after the command executes? That version probably doesn't support the same options or could be crashing outright. – Unixmonkey Apr 19 '19 at 22:47
  • @Unixmonkey Failed to execute: ["/Users/rebel/.rvm/gems/ruby-2.3.3/gems/wkhtmltopdf-binary-0.9.9.3/bin/wkhtmltopdf", "file:////var/folders/fy/67gyg_k95z5cycrq_03z21c40000gn/T/wicked_pdf20190420-3599-t8u4pu.html", "/var/folders/fy/67gyg_k95z5cycrq_03z21c40000gn/T/wicked_pdf_generated_file20190420-3599-4880gw.pdf"] Error: PDF could not be generated! Command Error: – Rebel Rider Apr 20 '19 at 07:58

0 Answers0