Preciso fazer um PDF a partir de uma página HTML
, porém ao converter recebo o seguinte erro:
Não é possível converter um objeto do tipo 'iTextSharp.text.html.simpleparser.TableWrapper' no tipo 'iTextSharp.text.Meta'.
Segue o código:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PaginaQueSeraConvertida.aspx.cs" Inherits="converterHTMLparaPDF.PaginaQueSeraConvertida" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Exportação de ASPX para PDF
<span id="export" runat="server"></span>
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Text;
namespace converterHTMLparaPDF
{
public partial class PaginaQueSeraConvertida : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
criatabela();
ConverteAspx2Pdf();
}
private void criatabela()
{
StringBuilder sb = new StringBuilder();
sb.Append("<table width='95%' border='0'><tr><td width='8%' class='tblNormal' align='Right'>Protocolo:</td>");
sb.Append("<td width='8%' class='tblNormal' align='Right'>Devedor:</td>");
sb.Append("<td width='16%' class='tblNormal' align='Right'>CNPJ/CPF:</td>");
sb.Append("</tr><tr>");
sb.Append("<td class='tblNormal' align='Right'>Título:</td>");
sb.Append("<td class='tblNormal' align='Right'>Apresentante:</td>");
sb.Append("<td class='tblNormal' align='Right'>Data Vencimento:</td>");
sb.Append("</tr><tr>");
sb.Append("<td class='tblNormal' align='Right'>Valor:</td>");
sb.Append("<td class='tblNormal' align='Right'>Saldo:</td>");
sb.Append("<td class='tblNormal' align='Right'>Data apontamento:</td>");
sb.Append("</tr><tr>");
sb.Append("<td class='tblNormal' align='Right'>Livro:</td>");
sb.Append("<td class='tblNormal' align='Right'>Folha:</td>");
sb.Append("<td class='tblNormal' align='Right'>Data protesto:</td>");
sb.Append("</tr><tr>");
export.InnerHtml = sb.ToString();
}
private void ConverteAspx2Pdf()
{
string attachment = "inline; filename=ExportacaoAspx2Pdf.pdf";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/pdf";
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
this.RenderControl(htextw);
Document document = new Document();
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
StringReader str = new StringReader(stw.ToString());
HTMLWorker htmlworker = new HTMLWorker(document);
htmlworker.Parse(str);
document.Close();
Response.Write(document);
Response.End();
}
}
}