Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert asp.net page to PDF

Status
Not open for further replies.

taree

Technical User
May 31, 2008
316
US
Will it possible to conver the asp.net page to pdf file.
I could convert gridview to pdf but I am not sure how to convert the whole page to pdf file.
I am using : iTextSharp thank you

Code:
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Response.AddHeader("content-disposition", "attachment;filename=ItemsUsedForProject.pdf")
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Dim sw As New StringWriter()
        Dim hw As New HtmlTextWriter(sw)
        gvItemDetail.AllowPaging = False

        gvItemDetail.DataBind()
        gvItemDetail.RenderControl(hw)
        Dim sr As New StringReader(sw.ToString())
        Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
        Dim htmlparser As New HTMLWorker(pdfDoc)
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
        pdfDoc.Open()
        htmlparser.Parse(sr)
        pdfDoc.Close()
        Response.Write(pdfDoc)
        Response.[End]()


    End Sub
[code]
 
what is iTextSharp?
If it is a 3rd party control, contact the vendor.
 
iText is a library that allows you to generate PDF files on the fly.
 
good luck. iTextSharp is very picky. 1 markup error and you get nothing. no errors, no logs, just a blank page. There is virtually no online help either. I believe Manning, or one of the other technical book publishers has a refence book about. It may be worth purchasing.

As for conversion; Spark has this built in. You could take a look at their implementation to determine how to do it. Another thought, use a HttpModule, check the request for the "to pdf" flag, if the flag exists, push the stream through iTextSharp and send the results as the response. something like
Code:
public class PdfModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.PostRequestHandlerExecute += RenderAsPdf;
    }

    private void RenderAsPdf(object sender, EventArgs e)
    {
        var context = HttpContext.Current;
        if (bool.Parse(context.Request.Params["pdf"]) == false) return;

        var response = context.Response;
        var stream = response.OutputStream;
        string pdf = new iTextSharp.Render(stream);

        response.Clear();
        response.Output.Write(pdf);
        response.ContentType = "application/pdf";
    }

    public void Dispose()
    {
    }
}


Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
I had a requirement to do this and itextsharp failed me completely.

Ended up creating a copy of the page as an SSRS report (simple database table to hold any user chosen options so that SSRS can recreate as the user sees the page) then use the rswebservice to run the report and render to PDF....you don't even need to do that tbh as you can control the render functionality via the SSRS url. I only used the webservice as I needed to save the report as well as creating it as a PDF...

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top