You must Sign In to post a response.
  • Category: ASP.NET

    How export google chart into pdf

    Hi,
    I have developed a form to display google chart and now i want export to PDF
    How to this. I am getting error as like 'Object reference not set to an instance of an object.'
    with follwing line.
    byte[] bytes = Convert.FromBase64String(base64.Split(',')[1]); because of the
    string base64 = Request.Form["chart_data"]; is getting null value..


    asp:Content ID="Content1" ContentPlaceHolderID="cHead" runat="Server">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
    google.load("visualization", "1", { packages: ["corechart"] });
    google.setOnLoadCallback(drawChart);
    function drawChart() {
    var options = {
    title: 'USA City Distribution'
    };
    var json = '[["ShipCity", "TotalOrders"], ["Albuquerque", 18], ["Anchorage", 10], ';
    json += '["Boise", 31], ["Butte", 3], ["Elgin", 5], ["Eugene", 11], ["Kirkland", 3], ';
    json += '["Lander", 9], ["Portland", 12], ["San Francisco", 4], ["Seattle", 14], ["Walla Walla", 2]]';
    var data = google.visualization.arrayToDataTable($.parseJSON(json));
    var chart = new google.visualization.PieChart($("#chart")[0]);
    chart.draw(data, options);
    var image = '<img src="' + chart.getImageURI() + '">';
    $("[id*=btnExport]").click(function () {
    $("input[name=chart_data]").val(chart.getImageURI());
    });
    }
    </script>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="mBody" runat="Server">
    <div id="chart" style="width: 500px; height: 300px;">
    </div>
    <br />
    <asp:Button ID="btnExport" Text="Export" runat="server" OnClick="Export_Click" />
    </asp:Content>


    protected void Export_Click(object sender, EventArgs e)
    {
    string base64 = Request.Form["chart_data"];
    byte[] bytes = Convert.FromBase64String(base64.Split(',')[1]);
    string fileName = "Chart.pdf";
    iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(bytes);
    using (MemoryStream memoryStream = new MemoryStream())
    {
    Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
    PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
    document.Open();
    document.Add(image);
    document.Close();
    bytes = memoryStream.ToArray();
    memoryStream.Close();
    }
    Response.Clear();
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/pdf";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
    }
  • #768109
    To Export Google chart to PDF you can use JSON to make its image and then with the help of Response object you can easily do it
    By reference of your your code it looks,
    your ImageURL must be empty which is not set to 'chart_data' , try to put Alert box in JQuery and try to find if you are getting right 'ImageURL' or not ?
    mean while you can refer below URL
    http://www.c-sharpcorner.com/uploadfile/raj1979/export-chart-to-pdf-using-itextsharp/

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #768111
    Hi,

    On this kind of PDF export from the Google Chart, we use on the of the tool.
    In fact it is just a JavaScript Plugin. We need to add the plugin and we can use it.

    The tool name is:

    jsPDF plugin


    It is a freeware only. So you can gave a try and let us know.

    Thanks,
    Mani


  • Sign In to post your comments