3

I am using chart.js for generating charts in my Rails application and Prawn-pdf to generate pdfs. I want to export canvas generated with chart.js to pdf.

chart.js has a method toDataUrl() which givers base64 string. I cannot find any method in prawn-pdf which accepts base64. Please suggest.

Thanks.

function done()
{
    var url_base64 =document.getElementById("myChart").toDataURL("image/png");
    $.ajax({
        type:'post', 
        url: '/admin/create_graph_to_image', 
        dataType: 'script',
        data: { string_base64: url_base64 } ,
        success: function (e) {
            console.log(e);
        },
        error: function (e) {
            console.log(e);
        }
    });
}
ART
  • 63
  • 1
  • 5
  • I would use D3.js for charts to generate SVG based elements and would use Prawn gem for generating pages in PDF format. – Dusht Oct 05 '15 at 10:50
  • @Dusht Is there any specific tutorial which leads me for generating graph and export them to pdf with D3.js? Thanks in Advance – ART Oct 06 '15 at 05:34

1 Answers1

0

chart.js has a method toDataUrl()

toDataUrl is a canvas property (https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL).

It returns an image representation of the canvas (default is png) in base 64 encoded format. Since Prawn needs an image path, you need to send this base 64 string to the server and save it as a file (see How to save a base64 string as an image using ruby) and then have Prawn use the location of the saved file.

Community
  • 1
  • 1
potatopeelings
  • 40,709
  • 7
  • 95
  • 119
  • it is a correct way to get an image on server but what I am trying is to post base64 with jquery ajax. But it responses **Completed 500 Internal Server Error in 13ms RuntimeError - ** – ART Oct 06 '15 at 13:32
  • Above is the code I have edited for jquery ajax call – ART Oct 06 '15 at 13:38
  • Okay I have found solution for it based on your guidance. Thanks @potatopeelings Correct code for Creating image file is in comment along with jquery ajax above. – ART Oct 06 '15 at 14:27