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

    How export google chart into pdf

    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=""></script>
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
    google.load("visualization", "1", { packages: ["corechart"] });
    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 () {
    <asp:Content ID="Content2" ContentPlaceHolderID="mBody" runat="Server">
    <div id="chart" style="width: 500px; height: 300px;">
    <br />
    <asp:Button ID="btnExport" Text="Export" runat="server" OnClick="Export_Click" />

    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);
    bytes = memoryStream.ToArray();
    Response.Buffer = true;
    Response.Charset = "";
    Response.ContentType = "application/pdf";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
  • #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

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

  • #768111

    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.


Sign In to post your comments