Export Html to PDF

3 posts, 0 answers
  1. Arun
    Arun avatar
    4 posts
    Member since:
    Apr 2011

    Posted 12 Jun 2014 Link to this post

    Why below code is not working. I am try to convert html to pdf. "radEditor.ExportToPdf()" line of code given error "Object reference not set to an instance of an object at Telerik.Web.UI.Editor.Export.RadEditorExportTemplate.Export()" 
           static void Main(string[] args)
            {
                RadEditor radEditor = new RadEditor();
                radEditor.Content = "<div>Hello</div>";
                radEditor.ExportContent += radEditor_ExportContent;
                radEditor.ExportToPdf();
            }

            static void radEditor_ExportContent(object sender, EditorExportingArgs e)
            {
                string path = "C:/tmp/NewFile.pdf";
                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                using (FileStream fs = File.Create(path))
                {
                    Byte[] info = System.Text.Encoding.Default.GetBytes(e.ExportOutput);
                    fs.Write(info, 0, info.Length);
                }

            }
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 12 Jun 2014 in reply to Arun Link to this post

    Hi Arun,

    Please try the below C# code snippet which export the RadEditor Content on a Button click.

    C#:
    RadEditor radEditor = new RadEditor();
    public string output;
    protected void Page_Load(object sender, EventArgs e)
    {
        radEditor.Content = "<div>Hello</div>";
        radEditor.ExportContent += new EditorExportContentEventHandler(radEditor_ExportContent);
        form1.Controls.Add(radEditor); 
    }
    void radEditor_ExportContent(object sender, EditorExportingArgs e)
    {
        this.output = e.ExportOutput;
    }
    protected void button1_Click(object sender, EventArgs e)
    {
        radEditor.ExportSettings.FileName = "TextFile.pdf";
        radEditor.ExportSettings.OpenInNewWindow = true;
        radEditor.ExportToPdf();
    }

    Thanks,
    Shinu.
  3. Ianko
    Admin
    Ianko avatar
    1890 posts

    Posted 13 Jun 2014 Link to this post

    Hello,

    From the static void Main(string[] args) line I can determine that this is not an ASP.NET web application, which does not need a Main method such as other .NET applications.

    Note that the RadEditor control is a ASP.NET AJAX UI control and supported only under ASP.NET AJAX environment. Also, converting HTML string to PDF is not possible without a rendered in the browser RadEditor, therefore the Telerik.Web.UI cannot be used as a third party library to export HTML to PDF in another that ASP.NET projects.

    Regards,
    Ianko
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top