Exporting RadHtmlChart to PNG and PDF

Thread is closed for posting
87 posts, 0 answers
  1. 7794844E-3AF2-48F0-867F-47FFCE778386
    7794844E-3AF2-48F0-867F-47FFCE778386 avatar
    260 posts
    Member since:
    Oct 2007

    Posted 29 Oct 2014 in reply to A1CE16C4-4C2E-464E-BF18-532525D276CA Link to this post

    Thanks for the tip on PhantomJS. I was mistaken, it's not a listener service to be placed on the web server, it's just a stand-alone executable to run from any machine. You can write a .JS script and pass in command line parameters for URL and filename, and have it go out and save that URL to a file. like so:

    //call via: phantomjs savePage.js http://www.google.com zzz.png
    var system = require('system');
     
    var url = system.args[1]; //'http://www.google.com';
    var filename = system.args[2]; //'zzz.png';
     
    var page = require('webpage').create();
     
    page.open(url, function() {
      page.render(filename);
      phantom.exit();
    });

    and call it like so:

    phantomjs savePage.js http://myServer/ChartEngine/chart.aspx?type=3 chartType3.png

    ...with some tweaking one may be able to define the captured-portion to only the desired chart element, then call this from our consuming apps that require the saved chart image. Still not ideal, owing to it being an third-party, an .EXE on our app servers, and non-supported...but may be better than using InkScape or commercial screenscapers.
  2. DF3A6C91-6E45-4E8C-9692-D454B632D9F6
    DF3A6C91-6E45-4E8C-9692-D454B632D9F6 avatar
    3 posts
    Member since:
    Jun 2014

    Posted 30 Oct 2014 Link to this post

    Hi Guys,

    I've found a solution to get the radhtmlchart images to the server but I'm finding that the quality of some of the rendered images is not what I would expect.(seems to be a problem with images using Line Series when lines are close together) My solution is to call the ImageDataURL jscript method on the kendo chart as mentioned earlier in this post and use JSON to send this data to a webmethod async inside the page which then reconstructs the image and saves it to the server.It works pretty well but I'm just curious why the images with line series are getting messed up as when I do a file save as on the image in the browser they are fine.The images are in the attached zip file.

    i.e in the Page markup/client side

    function Print(sender, args) {
    debugger;
    var $j_object = $(".RadHtmlChart");

    $j_object.each( function(i)
    {
    var radHtmlChartObject = $find($j_object[i].id);
    var kendoChart = radHtmlChartObject.get_kendoWidget();
    var imageDataURL = kendoChart.imageDataURL();
    //var blob = toBlob(imageDataURL, "image/png");
    PostSvgData($j_object[i].id, imageDataURL);
    } );
    }

    function PostSvgData(id, fileData)
    {
    $.ajax({
    type: 'POST',
    url: "PerformanceReporting.aspx/PostSvgFile",
    data: '{fileData: "' + fileData + '",filename:"' + id + '"}',
    contentType: "application/json; charset=utf-8",
    async: true,
    dataType: "json",
    success: OnSuccess,
    failure: function(response) {
    alert(response.d);
    }
    });
    }

    function OnSuccess(response) {
    }

    //On server side
    [System.Web.Services.WebMethod]
    public static void PostSvgFile(string fileData, string filename)
    {
    int data_index = fileData.IndexOf("base64") + 7;
    string fileContents = fileData.Substring(data_index);

    byte[] binaryData;

    binaryData = System.Convert.FromBase64String(fileContents);

    // save to disk
    System.IO.FileStream outFile;
    try
    {
    string file = Path.Combine(@"c:\temp\png", filename.Replace("-", ""));
    file = string.Concat(file, ".png");

    outFile = new System.IO.FileStream(file,
    System.IO.FileMode.Create,
    System.IO.FileAccess.Write);
    outFile.Write(binaryData, 0, binaryData.Length);
    outFile.Close();
    }
    catch (System.Exception exp)
    {
    // Error creating stream or writing to it.
    System.Console.WriteLine("{0}", exp.Message);
    }
    }
  3. 9915F819-5226-40CF-90E0-E07E884605ED
    9915F819-5226-40CF-90E0-E07E884605ED avatar
    1502 posts
    Member since:
    Jan 2017

    Posted 03 Nov 2014 Link to this post

    Hi Christian,

    I have tried to reproduce the mentioned issue but to no avail - the charts output looks like the one in the browser. Could you please try to reproduce the issue with the attached VS example and then tell us what changes you have made, so that I can proceed further with the investigation?

    Regards,
    Danail Vasilev
    Telerik
     
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
     
  4. F728CFBD-89FD-4F76-9660-3CCA39078501
    F728CFBD-89FD-4F76-9660-3CCA39078501 avatar
    23 posts
    Member since:
    Mar 2009

    Posted 18 Mar 2015 in reply to 9915F819-5226-40CF-90E0-E07E884605ED Link to this post

    Hello Danail,

    We are using Inkscape to export the svg file to pdf/png from the RadHTML chart. We are facing the same issue of text missing while exporting to image/pdf. This issue occurs when it is hosted in the server and not in the development machine(http://localhost:2001). We have also added MIME type in the IIS also.

    FYR, I have attached the screenshot of the exported pdf file with missing text, from the application hosted in the server.
    Could you help us to resolve the issue
  5. 9915F819-5226-40CF-90E0-E07E884605ED
    9915F819-5226-40CF-90E0-E07E884605ED avatar
    1502 posts
    Member since:
    Jan 2017

    Posted 18 Mar 2015 Link to this post

    Hi Karthick,

    I can suggest that you try to export the chart on the client-side through the RadClientExportManager control. You can find an example in the HtmlChart - Export to PDF demo.

    Regards,
    Danail Vasilev
    Telerik
     
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
     
  6. 72E5D830-CE6A-47CE-A227-FD829AB716AD
    72E5D830-CE6A-47CE-A227-FD829AB716AD avatar
    2 posts
    Member since:
    Mar 2015

    Posted 20 Mar 2015 Link to this post

    Hi,

    I'm trying to export a RadHtmlChart but I get the next error:

    Error en tiempo de ejecución de Microsoft JScript: Sys.WebForms.PageRequestManagerParserErrorException: No se pudo analizar el mensaje recibido del servidor. Este error suele producirse cuando la respuesta resulta modificada por llamadas a Response.Write() o cuando los filtros de respuesta, los HttpModules o el seguimiento de servidor están habilitados.
    Detalles: Error de análisis cerca de '￿PNG

    The code creates the temp.svg and exportedchart.png files in the app_data folder, but the files are deleted and then I get the error.

    Thanks for your help.
    Carlos García Tovar
  7. 9915F819-5226-40CF-90E0-E07E884605ED
    9915F819-5226-40CF-90E0-E07E884605ED avatar
    1502 posts
    Member since:
    Jan 2017

    Posted 24 Mar 2015 Link to this post

    Hi Carlos,

    Since you are modifying the request by returning the image from the server you should disable the AJAX.

    Regards,
    Danail Vasilev
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  8. DDA8DE6D-EE75-4815-BC82-12B272BF4114
    DDA8DE6D-EE75-4815-BC82-12B272BF4114 avatar
    104 posts
    Member since:
    Aug 2014

    Posted 01 May 2015 Link to this post

    it is not clear on how to implement this export.
    I downloaded your test project
    inserted the telerik dll into the bin folder and ran it.
    it created the file app_code/temp.svg and it has the correct svg chart in it.

    but my page errors on

    ExportRadHtmlChart_VB\App_Code\HtmlChartExporter.vb    Line: 21    
    inkscape.Start()
    error = The system cannot find the file specified

    do I need to put an inkscape dll into my bin?
            

     

  9. DDA8DE6D-EE75-4815-BC82-12B272BF4114
    DDA8DE6D-EE75-4815-BC82-12B272BF4114 avatar
    104 posts
    Member since:
    Aug 2014

    Posted 01 May 2015 in reply to DDA8DE6D-EE75-4815-BC82-12B272BF4114 Link to this post

    before you ask - yes I had already installed inkscape
  10. DDA8DE6D-EE75-4815-BC82-12B272BF4114
    DDA8DE6D-EE75-4815-BC82-12B272BF4114 avatar
    104 posts
    Member since:
    Aug 2014

    Posted 01 May 2015 in reply to DDA8DE6D-EE75-4815-BC82-12B272BF4114 Link to this post

    ok a few things - first I see it is hardcoded to the path of inkscape so I fixed that.
    I am having a different issue of the chart not showing in the image but I didn't see there were 3 pages of help text on this post so I just need to review those before I post again.
    disregard my posts (admin delete if you can)

    Doug
  11. DDA8DE6D-EE75-4815-BC82-12B272BF4114
    DDA8DE6D-EE75-4815-BC82-12B272BF4114 avatar
    104 posts
    Member since:
    Aug 2014

    Posted 01 May 2015 in reply to DDA8DE6D-EE75-4815-BC82-12B272BF4114 Link to this post

    I am having 2 issues
    1) my exports show only a small top left corner of the chart (I think)
    in debug mode I see the width and height are correct.

    the svg exported temp file shows the chart when viewed. (see attached files)
    I even tried putting into the export a larger/smaller amount then the actual chart width / height to no avail.

    2) I have a HtmlChartHolder that holds my charts and I want to export that whole thing. can it be done with this code or method?
    $find("<%=RadHtmlChart1.ClientID %>").getSVGString(); <-- does not work for a chart holder, maybe there is a way to get it?

  12. DDA8DE6D-EE75-4815-BC82-12B272BF4114
    DDA8DE6D-EE75-4815-BC82-12B272BF4114 avatar
    104 posts
    Member since:
    Aug 2014

    Posted 01 May 2015 Link to this post

    here are my attachments
  13. DDA8DE6D-EE75-4815-BC82-12B272BF4114
    DDA8DE6D-EE75-4815-BC82-12B272BF4114 avatar
    104 posts
    Member since:
    Aug 2014

    Posted 04 May 2015 Link to this post

    I have my .svg file saved so I tried opening it directly with inkscape and it gives me the image shown in my attachment.


    now when I opened the svg in inkscape and use the export GUI on the "page" tab it has the same bounding box as shown in image (0-100, 0-100)
    but on the "drawing" tab it has the correct values and exports correctly.
    it seems somehow my inkscape is exporting in "page" mode and I need it in "drawing" mode.

    ok now looking at their help page I added this to my arguments and it does now export the full chart
     --export-area-drawing

    but the labels are not showing on export.
    I tried adding --export-text-to-path (to no effect)

    perhaps there are default settings on inkscape that I need to set when I install it?
  14. 80FA0A9D-FAFC-4475-9D17-D7C5FA582B82
    80FA0A9D-FAFC-4475-9D17-D7C5FA582B82 avatar
    4 posts
    Member since:
    Feb 2014

    Posted 29 Jun 2015 Link to this post

    Please Help me with this. 

     I have created a RadHtmlChart and trying to export it to pdf. I followed this http://demos.telerik.com/aspnet-ajax/client-export-manager/applicationscenarios/export-radhtmlchart/defaultcs.aspx?product=htmlchart

     But for me its not working which means im getting a blank pdf without datas.

  15. 80FA0A9D-FAFC-4475-9D17-D7C5FA582B82
    80FA0A9D-FAFC-4475-9D17-D7C5FA582B82 avatar
    4 posts
    Member since:
    Feb 2014

    Posted 29 Jun 2015 in reply to 80FA0A9D-FAFC-4475-9D17-D7C5FA582B82 Link to this post

    i binded the data in client side.
  16. D8784C23-C40A-4B86-AE50-298AA29321B6
    D8784C23-C40A-4B86-AE50-298AA29321B6 avatar
    33 posts
    Member since:
    Mar 2010

    Posted 10 Feb 2016 Link to this post

    Hello Admin,

     I have tried your solution and it works really well on my localhost, But How can i use the same approach when publishing the code on Shared hosting environment like Godaddy.com.

     Is is possible or not?

    Quick reply will be really appreciated.

    Thanks

    Mandeep

  17. 7139E256-95FF-431E-9493-9F1BB746FDC4
    7139E256-95FF-431E-9493-9F1BB746FDC4 avatar
    23 posts
    Member since:
    Feb 2012

    Posted 11 Feb 2016 in reply to D8784C23-C40A-4B86-AE50-298AA29321B6 Link to this post

    Hey Mandy.
    I dont know what solution you're using but I ended up using the RadClientExportManager:

    This javascript function finds the image (#foo) and exports it:

                    function Print(s, e) {
                        var exportMngr = $find("<%= RadClientExportManager1.ClientID %>");
                        exportMngr.exportImage($telerik.$("#foo"));
                    }


    This is a javascript event on the ClientExportManager that sends the image to the server via a pagemethod and triggers an ajax request by clicking a button when done.

                    function ClientImageExporting(sender, args) {
                        var dataRaw = args.get_dataURI().split(',');

                        PageMethods.InitiateTransfer(gQuestionnaireGuid, dataRaw[1], function () {
                            $find('<%=DoPrint.ClientID %>').click();
                        });

                        args.set_cancel(true);
                    }


    On the server-side i get the image from the pageMethod like this :
            public static void InitiateTransfer(string rkaGUID, string encodedImage)
            {

                    var image = Functions.Imaging.Base64ToBitmap(encodedImage);
    //Save the image or images to some cache..  I use session variables
            }


                public static object Base64ToBitmap(string encodedImage)
                {
                        Bitmap bmpReturn = null;

                        byte[] byteBuffer = Convert.FromBase64String(encodedImage);
                        using (MemoryStream memoryStream = new MemoryStream(byteBuffer))
                        {
                            memoryStream.Position = 0;

                            bmpReturn = (Bitmap)Bitmap.FromStream(memoryStream);

                            memoryStream.Close();
                        }
                        byteBuffer = null;

                        return bmpReturn;
                }

            protected void DoPrint_Click(object sender, EventArgs e)
            {
    //In the ajax callback I get the images from the session variable and clears it, since I don't need that image anymore.
            }


    Using this approach I'm not reliant on any 3rd party libraries being installed on the server..  Its all plain MS and Telerik and should work on all hosts.Hey Mandy.
    I dont know what solution you're using but I ended up using the RadClientExportManager:

                    function Print(s, e) {
                        var exportMngr = $find("<%= RadClientExportManager1.ClientID %>");
                        exportMngr.exportImage($("#foo"));
                    }

                    function ClientImageExporting(sender, args) {
                        var dataRaw = args.get_dataURI().split(',');

                        PageMethods.InitiateTransfer(gQuestionnaireGuid, dataRaw[1], function () {
                            $find('<%=DoPrint.ClientID %>').click();
                        });

                        args.set_cancel(true);
                    }

  18. 1F27713B-06D0-4673-B51E-CC32E25F6C39
    1F27713B-06D0-4673-B51E-CC32E25F6C39 avatar
    1 posts
    Member since:
    Feb 2016

    Posted 01 Mar 2016 Link to this post

    Hello,

    I am using following code to get RadHtml chart in pdf

    HtmlChartExportSettings currentSettings = new HtmlChartExportSettings();

            currentSettings.Height = (int)RadHtmlChart1.Height.Value;
            currentSettings.Width = (int)RadHtmlChart1.Width.Value;

     

    but for HtmlChartExportSettings shows namespace could not be found

    I have included

    using System.Web.UI;

    using Telerik.Web.UI;

  19. 0E2F26BC-A45A-41DA-A470-D57CB32DFE93
    0E2F26BC-A45A-41DA-A470-D57CB32DFE93 avatar
    1028 posts
    Member since:
    Mar 2017

    Posted 04 Mar 2016 Link to this post

    Hi Michael,

    To avoid inconsistency of the thread please open a new support ticket for your case.

    Regards,
    Peter Filipov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  20. 1F48C437-9698-4104-909C-17D37949253A
    1F48C437-9698-4104-909C-17D37949253A avatar
    1 posts
    Member since:
    Oct 2016

    Posted 27 Oct 2016 Link to this post

    i use RadClientExportManager to export RadOrgChart to PNG but i got the problem when i use firefox to export like this

    https://www.mediafire.com/?td7cun457hfd4ci

    How to solve this problem

  21. A0F6404D-0F16-413D-80F6-91E97B6C8828
    A0F6404D-0F16-413D-80F6-91E97B6C8828 avatar
    130 posts
    Member since:
    Apr 2008

    Posted 05 May 2017 in reply to 7139E256-95FF-431E-9493-9F1BB746FDC4 Link to this post

    Hi Henrik 

    Your solution working perfectly thank you very much.....

  22. 06A85FE1-CF53-4B22-A168-3A3467646C27
    06A85FE1-CF53-4B22-A168-3A3467646C27 avatar
    3 posts
    Member since:
    Feb 2013

    Posted 11 May 2017 in reply to 63F75A2C-1F16-4AED-AFE8-B1BBD57646AD Link to this post

    Hi ,

         I need to export the RadHtmlchart as png image and need to save the image file in my local folder . I have downloaded the sample solution from the above link and worked .But While Export as PNG file format it gives blank image (i.e, it doesnt have any chart image) and downloaded pdf also has empty files .

    Here I have attached the sample solution that i have worked and the output files generated are available in the App_Data folder ,I have downloaded  64-bit Windows:installer (exe) file from the below link https://inkscape.org/en/download/windows/  for converting svg file to png conversion .

    Here I have used the telerik licensed version dll (2015.3.1111.35) .

    Please tell me the solution to save the Radhtmlchart as png image .

    Is it possible to converting Radhtmlchart(SVG) to PNG without using inkscape software ? if it is possible please give the working sample for that .

    Please find the attachments and give me the suggestions asap .

    Thanks 

     

     

  23. 5E412F3A-0FD2-4DCE-A4BF-C20838F3BD12
    5E412F3A-0FD2-4DCE-A4BF-C20838F3BD12 avatar
    6 posts
    Member since:
    Jan 2013

    Posted 17 Apr 2018 Link to this post

    Trying to generate the PNG and PDF both, but getting no results.

    The code executes fine using inkscape object in the method "ExportHtmlChart", but nothing is generated.

    I provided the permission to the destination folder and also tried to use another folder, but still doesn't work!

    It doesn't throw any error as well.

  24. BE36EA6A-DFCF-49D7-A317-452D0EC2B6D0
    BE36EA6A-DFCF-49D7-A317-452D0EC2B6D0 avatar
    3 posts
    Member since:
    Jan 2019

    Posted 16 Jul 2019 in reply to 63F75A2C-1F16-4AED-AFE8-B1BBD57646AD Link to this post

    this code just get a small part of the radhtmlchart  :(

    The selected file(s) cannot be attached because it may exceed the maximum attachment size (2 MB) or is from not allowed type (allowed: .zip, .rar).

  25. AA85460C-73DB-46EF-9FDC-E1076B249F5E
    AA85460C-73DB-46EF-9FDC-E1076B249F5E avatar
    30 posts
    Member since:
    Sep 2009

    Posted 15 May 2020 Link to this post

    HI,

    I have been using this approach as described by the author for many years, But with the latest update of telerik controls SVG file itself is coming empty for me. The line chart is not appearing  in the svg file only the X and Y Axis details are getting displayed. Did anything changed with the latest updates?

     

    regards

     

  26. DF60784D-55A5-4263-9F10-A12FA48C9ADC
    DF60784D-55A5-4263-9F10-A12FA48C9ADC avatar
    14477 posts
    Member since:
    Apr 2022

    Posted 18 May 2020 Link to this post

    Hi Everybody,

    The scaling and empty image/pdf issues are due to an update of the Inkscape software.

    You can find below the steps on how to resolve the problem with the latest versions of Telerik UI for ASP.NET AJAX (R2 2020/ 2020.2.512) and Inkscape 1.0.

    1) Download the latest version of Inkscape 1.0 from https://inkscape.org/release/1.0/windows/. Note that the 32-bit installation will install the tool in C:\Program Files (x86)\Inkscape\bin\inkscape.exe, while the 64-bit one in the C:\Program Files\Inkscape\bin\inkscape.exe folder.

    Depending on the installation folder, you have to update the path to the inkscape.exe in the InkscapePath path constant inside the \App_Code\HtmlChartExportSettings.cs file:

    public const string InkscapePath = @"C:\Program Files\Inkscape\bin\inkscape.exe";//the path to the Inkscape.exe on your machine (64-bit setup)
    

    2) Inkscape 1.0 has a new API that you can find out on https://inkscape.org/doc/inkscape-man.html.

     

    Having this in mind, in order to fix the scaling issue you must update the following line inside the ExportHtmlChart in the \App_Code\HtmlChartExporter.cs file, from

     

    String.Format("--file \"{0}\" --export-{1} \"{2}\" --export-width {3} --export-height {4}",
    						settings.SvgFilePath, settings.Extension, settings.OutputFilePath, settings.Width, settings.Height);

     

    to

    String.Format("--export-type={1} {0} --export-filename={2} --export-area-drawing",
    						settings.SvgFilePath,settings.Extension, settings.OutputFilePath, settings.Width, settings.Height);


    Note that the width and height options from the original code are replaced with --export-area-drawing, which solves the cropping and calling problem.
    The --export-filename is also added to set the name of the output file. 
    There is also a --export-type property specifying the file type to export.

    -D, --export-area-drawing

    In SVG, PNG, PDF, PS, and EPS export, exported area is the drawing (not page), i.e. the bounding box of all objects of the document (or of the exported object if --export-id is used). With this option, the exported image will display all the visible objects of the document without margins or cropping. This is the default export area for EPS. For PNG, it can be used in combination with --export-use-hints.


    -o, --export-filename=FILENAME
    Sets the name of the output file. The default is to re-use the name of the input file. If --export-type is also used, the file extension will be adjusted (or added) as appropriate. Otherwise, the file type to export will be inferred from the extension of the specified filename.

    Usage of the special filename "-" makes Inkscape write the image data to standard output (stdout).

    --export-type=TYPE[,TYPE]*
    Specify the file type to export. Possible values: svg, png, ps, eps, pdf, emf, wmf, xaml. It is possible to export more than one file type at a time.



    For your convenience, I recorded a short video that you can review and use it as a guide on how to proceed. Here you go: https://youtu.be/TcGlA9wcBeM.

     

    Best Regards, 
    Rumen
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
  27. AA85460C-73DB-46EF-9FDC-E1076B249F5E
    AA85460C-73DB-46EF-9FDC-E1076B249F5E avatar
    30 posts
    Member since:
    Sep 2009

    Posted 19 May 2020 in reply to DF60784D-55A5-4263-9F10-A12FA48C9ADC Link to this post

    Thankyou for response. Followed your steps and upgraded the code as per your description.

    But issue was something else. I have a button trigger to call the PNG conversion. Timer I used was not enough after the Telerik  update. Changing the timer and delaying the read of the SVG content made it work.

     

    Thanks for the help Rumen

Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.