Link to home
Start Free TrialLog in
Avatar of David C
David CFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Save generated output to folder instead of displaying in browser

Hi Experts

My code below takes the current html of a page and creates a PDF. I am struggling to find a way to save the PDF into a folder instead of showing it in the browser

Dim htmlToPdf = New HtmlToPdfConverter()
        Dim pdfData = htmlToPdf.GeneratePdf(html)
        Response.ContentType = "application/pdf"
        Response.OutputStream.Write(pdfData, 0, pdfData.Length)
        Response.[End]()

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

Save it in a folder where:  on the server or on the client?
Avatar of David C

ASKER

Apologies on the server
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of David C

ASKER

Thank you it tries to do it then says

 System.Web.HttpException: A page can have only one server-side Form tag.

Open in new window


I tried it with

 Public Overrides Sub VerifyRenderingInServerForm(control As Control)
    End Sub

Open in new window


and without but get the same error

I have placed the line as follows

System.IO.File.WriteAllBytes(Server.MapPath("~\PDF\test.pdf"), pdfData) 

Open in new window

That sounds like an issue in your markup file, not your code-behind file. Do you have two <form> tags in your HTML somewhere?
Avatar of David C

ASKER

No I have just 1
Default.aspx
Avatar of David C

ASKER

I have noticed that it creates the file which is brilliant news but its just the error that I need to get rid off.
Avatar of David C

ASKER

I have also noticed that if I remove the only form tag and place the code in the form load, everything works fine however this renders the form useless as none of the controls work without the form tag.

This is how I am generating the html

  'Generate from current page
        Dim renderedOutput As StringBuilder
        Dim strWriter As StringWriter
        Dim tWriter As HtmlTextWriter
        Dim html As String

        'create a HtmlTextWriter to use for rendering the page
        renderedOutput = New StringBuilder
        strWriter = New StringWriter(renderedOutput)
        tWriter = New HtmlTextWriter(strWriter)
        'render the page output
        Page.RenderControl(tWriter)
        html = renderedOutput.ToString

Open in new window


Maybe if I change that it'll work? Is there any other way to get the HTML of the current page?
Avatar of David C

ASKER

Thanks