HTML To PDF Using
HTML To PDF Using
HTML To PDF Using
NET
--------------------------------------------------------------------------------
The best one for its price the HTML to PDF Converter Library for .NET 2.0 from h
ttp://www.dotnet-reporting.com or the HTML to PDF Converter from http://www.winn
ovative-software.com . You can convert a HTML string or a web page URL to PDF in
only a few lines of code:
PdfConverter pdfConverter = new PdfConverter();
pdfConverter.LicenseKey = "P38cBx6AWW7b9c81TjEGxnrazP+J7rOjs+9omJ3TUycauK+cLWdrI
TM5T59hdW5r";
// set the converter options
pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4;
pdfConverter.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel.Normal
;
pdfConverter.PdfDocumentOptions.PdfPageOrientation = PDFPageOrientation.Portrait
;
pdfConverter.PdfDocumentOptions.ShowHeader = false;
pdfConverter.PdfDocumentOptions.ShowFooter = false;
pdfConverter.PdfDocumentOptions.GenerateSelectablePdf = selectablePDF;
// Performs the conversion and get the pdf document bytes that you can further
// save to a file or send as a browser response
byte[] pdfBytes = pdfConverter.GetPdfFromUrlBytes(urlToConvert);
// send the PDF document as a response to the browser for download
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
response.AddHeader("Content-Type", "binary/octet-stream");
response.AddHeader("Content-Disposition",
"attachment; filename=ConversionResult.pdf; size=" + pdfBytes.Length.ToString())
;
response.Flush();
response.BinaryWrite(pdfBytes);
response.Flush();
response.End();
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-------
Dim attach As String = "attachment;filename =export.xls"
Response.ClearContent()
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("content-disposition", attach)
Dim str As New StringWriter
Dim htr As New HtmlTextWriter(str)
dg1.RenderControl(htr)
Response.Write(str.ToString())
'Response.Output.GetType()
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
----------------
--------------------------------------------------------------------------------
-------------------
using System.IO;
using System.Diagnostics;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
Document myDocument;
myDocument = new Document(PageSize.A4.Rotate());
PdfWriter.GetInstance(myDocument, new FileStream("c:\\zz.pdf", FileMode.Create))
;
myDocument.Open();
// myDocument.Add(new Paragraph(tmp));
//myDocument.Close();
--------------------------------------------------------------------------------
-----------------------------------
the only way I could get my datagrid to excel was to surround it with table tags
.
<form>
<table id="gridtable">
<tr>
<td>
<datagrid>
</td>
</tr>
</table>
</form>
then in button click event
Dim attach As String = "attachment;filename =export.xls"
Response.Clear()
Response.Buffer = True '
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Response.AddHeader("content-disposition", attach)
Dim strWriter As New StringWriter()
Dim htmlWriter As New HtmlTextWriter(strWriter)
Me.gridtable.RenderControl(htmlWriter)
Response.Write(strWriter.ToString())
Response.End()
--------------------------------------------------------------------------------
----------------------------------------------
try this code to copy gridview to pdf file. Its working code !
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Han
dles Me.Load
Dim connect As String = "Data Source=com20;Initial Catalog=Test;Integrated Secur
ity=True"
Dim con As New SqlConnection(connect)
con.Open()
Dim str As String = "SELECT * from login"
Dim cmd As New SqlCommand(str, con)
Dim rd As SqlDataReader = cmd.ExecuteReader
gridview1.DataSource = rd
gridview1.DataBind()
End Sub
--------------------------------------------------------------------------------
--------------------------------------------
Hi,
My code does not shows a SaveAs dialogue box to download the datagrid into Excel
file. My code has no errors. It Doesn't do anything.
I am using this code:
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=Sample.xls");
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new HtmlTextWriter(sw);
dg.DataSource = dmpset;
dg.DataBind();
dg.RenderControl(htw);
//GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
Please anyone knows helps me.
Thanks in advance...
--------------------------------------------------------------------------------
--------------------------------------------------------------
Gridview1.RenderControl(hw)
Dim sr As New StringReader(sw.ToString())
Dim htmlparser As HTMLWorker
Dim pdfDoc As Document
pdfDoc = New Document(PageSize.A0, 10.0F, 10.0F, 10.0F, 0.0F)
htmlparser = New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Output.Write(pdfDoc)
Response.End()
Reagrds
SAJID
--------------------------------------------------------------------------------
-------------------------------------
I am using the below code to export the Grid View data to Excel.
Imports System.IO
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Han
dles Me.Load
If Not Page.IsPostBack Then
gv.AllowPaging = False
gv.DataSource = oDv
gv.DataBind()
GridViewExportUtil.Export("Export.xls", Me.gv)
End If
End Sub
Thanks
--------------------------------------------------------------------------------
---
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
--------------------------------------------------------------------------------
----------------------------
Add reference ABCpdf.dll
Render web page in pdf in landscape or portrait format
Write pdf to browser or save to disk
Save HTML as pdf or save the whole web page as pdf by given URL
Imports WebSupergoo.ABCpdf6
Imports System.Text
Imports System.IO
End Sub
litTop.RenderControl(hw)
litBottom.RenderControl(hw)
Return sb.ToString
End Function
'set properties
Dim theDoc As New Doc
theDoc.HtmlOptions.PageCacheEnabled = False
theDoc.HtmlOptions.BrowserWidth = 0
theDoc.HtmlOptions.ImageQuality = 101
theDoc.MediaBox.Width = 1000
theDoc.Rect.Width = 1000
'paging
'http://www.websupergoo.com/helppdf5/default.html?page=source%2F4-examples%2F08-
landscape.htm
Do While True
theDoc.FrameRect()
If (theDoc.Chainable(theID) = False) Then
Exit Do
End If
theDoc.Page = theDoc.AddPage()
theID = theDoc.AddImageToChain(theID)
Loop
theDoc.HtmlOptions.LinkPages()
For x As Integer = 1 To theDoc.PageCount
theDoc.PageNumber = x
theDoc.Flatten()
Next
End Sub
Enum SourceType
URL
HTML
End Enum
Enum ExportType
Browser
Disk
End Enum
Reference:
--------------------------------------------------------------------------------
---------------------------------------------------