This is a migrated thread and some comments may be shown as answers.

Export Html to PDF

2 Answers 172 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Arun
Top achievements
Rank 1
Arun asked on 12 Jun 2014, 09:02 PM
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 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 13 Jun 2014, 04:57 AM
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.
0
Ianko
Telerik team
answered on 13 Jun 2014, 06:19 AM
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.

 
Tags
Editor
Asked by
Arun
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Ianko
Telerik team
Share this question
or