ASP.NET page to image or PDF - asp.net

I have an ASP.NET page like this:
http://farm4.static.flickr.com/3631/3690714302_c17b259863.jpg
My table is Gridview and some Label, anybody can tell me how to create a button to convert my page to image or PDF file and save it to desktop.

It can be done by code on the backend that renders the html into a memory Graphic object and then serves that resultant bitmap back, or by printing the page (on the server) through a PDF writer and then capturing the result.
Javascript, no.
Or google "online convert html pdf" and use one of those services.

Hai use iTextSharp to convert your webpage to pdf.
It looks very nice.
Just download the dll file and add reference to your application.
Then in button click give the coding as follows.
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", attachment; filename="jaison.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
Me.Page.RenderControl(hw)
Dim sr As New StringReader(sw.ToString())
Dim pdfDoc As New Document(iTextSharp.text.PageSize.A2, 10, 10, 10, 10)
Dim htmlparser As New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Write(pdfDoc)
Response.End()
Surely this code will help you.
All the best!

Related

Trying to convert a gridview into a PDF document

Basic principle is that I am trying to take a GridView and turn it into a PDF document. The GridView includes controls like checkboxes and some css classes with some colouring.
I have tried putting this together, but I think I am misunderstanding how something works and now hit a dead end. At present it all compiles and runs, but when you try and open the returned PDF it says it fails to load. The returned file is 1KB in size, so I am guessing that it isn't actually a valid PDF.
I am using iText7 and htmlPDF, with my code looking like this.
Protected Sub ExportToPDF(sender As Object, e As ImageClickEventArgs)
Using sw As New StringWriter()
Using hw As New HtmlTextWriter(sw)
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
gvCSC.DataBind()
gvCSC.RenderControl(hw)
Dim pdfWriter As New PdfWriter(Response.OutputStream)
Dim pdfDoc As New PdfDocument(pdfWriter)
Dim sr As New StringReader(sw.ToString)
pdfDoc.SetDefaultPageSize(New PageSize(PageSize.A4.Rotate))
Dim doc As Document = HtmlConverter.ConvertToDocument(sr.ToString, pdfDoc, New ConverterProperties)
Response.End()
pdfDoc.Close()
End Using
End Using
End Sub
I am hoping that I am just being stupid as I've not used iText7 before and it just needs someone to point out the obvious for me. Any help in solving this will be appreciated.

Unable to open PDF after it's generated (vb.net) (asp.net)

Sorry for this question, but I'm new in .net and vb.net. I really need your help! I generate a PDF file in asp.net (vb.net). It should be opened in a browser automatically, but it gives an error - Failed to load PDF document. I see it's created on a drive and opens properly. What could be an issue?
Protected Sub ExportToPDF(sender As Object, e As EventArgs)
Response.ContentType = "application/pdf"
Dim pdfDoc As New Document(PageSize.A4, 50.0F, 10.0F, 10.0F, 10.0F)
PdfWriter.GetInstance(pdfDoc, New
FileStream(Context.Server.MapPath("~/ejik.pdf"), FileMode.CreateNew))
pdfDoc.Open()
pdfDoc.Add(New Paragraph("What is going on?"))
pdfDoc.Close()
Response.Write(pdfDoc)
Response.End()
End Sub
Thank you in advance!
P.S.: Sorry, I forgot to mention that I use iTextSharp to create a PDF
Your code is all wrong. You are creating a web application, and you create an object of type Document so that you can write a PDF file to disk. (Do you even need that file on disk? You are writing a web application!) Then, instead of sending the file to the end user, you write a Document object to the Response instead of (the bytes of) the file. That can never work, can it?
It would be better if you created the PDF file in memory first:
MemoryStream ms = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, ms);
document.Open();
document.Add(Assinatura());
document.Close();
Then you take the bytes of that file in memory, and send them to the Response:
byte[] bytes = ms.ToArray();
ms.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=ejik.pdf");
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
This is what the comment meant. Instead of pdfDoc, you need to send the byte array of the PDF file. See also iTextSharp is producing a corrupt PDF with Response

Export Devexpress XtraPivotGrid with theme to pdf using Itextsharp

I am using DevExpress ASPXPivotGrid control for my ASP.NET site.I am also using DevExpress ASPXPivotGridExporter control to ASPXPivotGrid to pdf. The problem I am having is that ASPXPivotGridExporter cannot export themes and right to left languages. Exported output always come out with default formating and just left to right languages. I would like to know if its possible to export pivot grid with theme and with rtl languages ? If not is there work around it with ITextSharp?
this what I tried so far
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "inline;filename=TestPage.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
ASPXPivotGrid1.RenderControl(hw)
Dim sr As New StringReader(sw.ToString())
Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 0.0F)
Dim htmlparser As New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
Using memoryStream As New MemoryStream
Chart1.SaveImage(memoryStream, ChartImageFormat.Png)
Dim img As Image = Image.GetInstance(memoryStream.GetBuffer())
img.ScalePercent(75.0F)
pdfDoc.Add(img)
End Using
pdfDoc.Close()
Response.Write(pdfDoc)
Response.[End]()
using aspxpivotgridexporter doesn't support right to left languages. but finally i was able to find a work around by exporting pivot grid to html using pivot grid exporter then I can add the needed styles to the resulting html file and adding the direction to the text wither rtl or ltr

HTML code appearing in CSV FILE

We want to create a CSV file from data .It is showing HTML tags .Same working fine for xlx file.
Below is code.
Dim response As System.Web.HttpResponse = System.Web.HttpContext.Current.Response
response.Clear()
response.ClearHeaders()
response.Write("<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8""/>")
'Chose what type of file needed ie.e CSV /XLS
If _exportType = ExportTypeEnum.CSV Then
response.ContentType = "application/vnd.xls"
Else
response.ContentType = "application/vnd.xls"
End If
response.AddHeader("Content-Disposition", "attachment;filename=" & FileNameToExport)
Dim sw As System.IO.StringWriter = New StringWriter()
Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
Dim dg As DataGrid = New DataGrid()
Dim dv1 As DataView = data
dg.DataSource = dv1
dg.BorderStyle = BorderStyle.None
dg.DataBind()
dg.RenderControl(htw)
response.Write(sw.ToString())
response.End()
Please suggest WHY it is not working for CSV file , although it is fine for xls file.
Many thanks in advance
You're explicitly writing the output in HTML format. Notice this line:
response.Write("<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8""/>")
And notice the object you're using to write output:
Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
This is all emitting HTML to the output. It "works fine" for an Excel file because Excel understands HTML structures when rendering a file. CSV, however, is a much "flatter" format and only knows plain text (delimited by a comma). You'll also see the HTML tags if, for example, you use this code to write to a .txt file.
If you want a CSV file without HTML, you shouldn't write HTML content to it.

iTextSharp generate PDF and show it on the browser directly

How to open the PDF file after created using iTextSharp on ASP.Net? I don't want to save it on the server, but directly when the new PDF is generated, it shows on the browser. Is it possible to do that?
Here is the example for what I mean: Click here . But on this example, the file directly downloaded.
How can I do that?
Dim doc1 = New Document()
'use a variable to let my code fit across the page...
Dim path As String = Server.MapPath("PDFs")
PdfWriter.GetInstance(doc1, New FileStream(path & "/Doc1.pdf", FileMode.Create))
doc1.Open()
doc1.Add(New Paragraph("My first PDF"))
doc1.Close()
The code above do save the PDF to the server.
Thank you very much in advance! :)
You need to set Content Type of the Response object and add the binary form of the pdf in the header
private void ReadPdfFile()
{
string path = #"C:\Somefile.pdf";
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path);
if (buffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length",buffer.Length.ToString());
Response.BinaryWrite(buffer);
}
}
(or) you can use System.IO.MemoryStream to read and display:
Here you can find that way of doing it
Open Generated pdf file through code directly without saving it onto the disk
The problem solved with the code below:
HttpContext.Current.Response.ContentType = "application/pdf"
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim pdfDoc As New Document()
PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream)
pdfDoc.Open()
'WRITE PDF <<<<<<
pdfDoc.Add(New Paragraph("My first PDF"))
'END WRITE PDF >>>>>
pdfDoc.Close()
HttpContext.Current.Response.Write(pdfDoc)
HttpContext.Current.Response.End()
Hope help! :)

Resources