[Solved] Convert HTML File to PDF Error : string can not be converted to text in ASP.Net

fahimahmed
 
on Nov 26, 2021 01:47 AM
Sample_962031.zip
678 Views

Convert HTML File to PDF using C# and VB.Net in ASP.Net

Above Solution is working fine but i need to modify html strings before converting into pdf below are the changes which works while sending in email but getting error that string can not be converted to text.

Send Email using HTML Templates in ASP.Net with C# and VB.Net

Instead of sending email i need to make chaanges through database and save as pdf

Dim reader As New StreamReader(Server.MapPath("Customers.htm"))
Dim readFile As String = reader.ReadToEnd()
Dim myString As String = ""
myString = readFile
myString = myString.Replace("$$Username$$", Titles.Text & " " & FullName.Text)
myString = myString.Replace("$$Email$$", EmailAddress.Text)
Download FREE API for Word, Excel and PDF in ASP.Net: Download
arjunv
 
on Nov 26, 2021 06:26 AM

Hi fahimahmed, 

 Please refer below Sample.

HTML File

<img src="https://www.aspsnippets.com/images/Blue/Logo.png" alt="Alternate Text" />
<br />
<br />
<div>
    <div style="border-top: 3px solid #22BCE5">
        &nbsp;
    </div>
    <span>
        Hello <b>{UserName}</b>
        <br /><br />
        A new article has been published on ASPSnippets.
        <br />
        <a style="color: #22BCE5" href="{Url}">{Title}</a><br />
        {Description}
        <br /><br />
        Thanks<br />
        ASPSnippets
    </span>
</div>

HTML

<asp:Button Text="Export" ID="btnExport" runat="server" OnClick="Export" />

Namespaces

C#

using System.IO;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

VB.Net

Imports System.IO
Imports System.Text
Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf

Code

C#

protected void Export(object sender, EventArgs e)
{
    string body = string.Empty;
    using (StreamReader reader = new StreamReader(Server.MapPath("~/EmailTemplate.html")))
    {
        body = reader.ReadToEnd();
    }
    body = body.Replace("{UserName}", "Mudassar");
    body = body.Replace("{Title}", "Convert HTML File to PDF using C# and VB.Net in ASP.Net");
    body = body.Replace("{Url}", "https://www.aspsnippets.com/Articles/Send-Email-using-HTML-Templates-in-ASPNet-with-C-and-VBNet.aspx");
    body = body.Replace("{Description}", "Here Mudassar Ahmed Khan has explained with an example, how to send Email using HTML Templates in ASP.Net with C# and VB.Net.");
    byte[] byteArray = Encoding.UTF8.GetBytes(body);
    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
        {
            using (StreamReader sr = new StreamReader(new MemoryStream(byteArray)))
            {
                Document pdfDoc = new Document(PageSize.A2, 80f, 80f, 80f, 80f);
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();
                htmlparser.Parse(sr);
                pdfDoc.Close();
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=HTMLExport.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Write(pdfDoc);
                Response.End();
            }
        }
    }
}

VB.Net

Protected Sub Export(ByVal sender As Object, ByVal e As EventArgs)
    Dim body As String = String.Empty
    Using reader As StreamReader = New StreamReader(Server.MapPath("~/EmailTemplate.html"))
        body = reader.ReadToEnd()
    End Using
    body = body.Replace("{UserName}", "Mudassar")
    body = body.Replace("{Title}", "Convert HTML File to PDF using C# and VB.Net in ASP.Net")
    body = body.Replace("{Url}", "https://www.aspsnippets.com/Articles/Send-Email-using-HTML-Templates-in-ASPNet-with-C-and-VBNet.aspx")
    body = body.Replace("{Description}", "Here Mudassar Ahmed Khan has explained with an example, how to send Email using HTML Templates in ASP.Net with C# and VB.Net.")
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(body)
    Using sw As StringWriter = New StringWriter()
        Using hw As HtmlTextWriter = New HtmlTextWriter(sw)
            Using sr As StreamReader = New StreamReader(New MemoryStream(byteArray))
                Dim pdfDoc As Document = New Document(PageSize.A2, 80.0F, 80.0F, 80.0F, 80.0F)
                Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc)
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
                pdfDoc.Open()
                htmlparser.Parse(sr)
                pdfDoc.Close()
                Response.ContentType = "application/pdf"
                Response.AddHeader("content-disposition", "attachment;filename=HTMLExport.pdf")
                Response.Cache.SetCacheability(HttpCacheability.NoCache)
                Response.Write(pdfDoc)
                Response.End()
            End Using
        End Using
    End Using
End Sub

Screenshot