Solved

Save generated output to folder instead of displaying in browser

Posted on 2014-02-08
9
288 Views
Last Modified: 2014-02-28
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

0
Comment
Question by:takwirirar
  • 6
  • 3
9 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39844239
Save it in a folder where:  on the server or on the client?
0
 
LVL 1

Author Comment

by:takwirirar
ID: 39844242
Apologies on the server
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39844244
Replace lines 3 - 5 with:

System.IO.File.WriteAllBytes("path\to\folder", pdfData)  ' First parameter may need to change
                                                         '  to Server.MapPath("path\to\folder")
                                                         '  depending on which folder you are
                                                         '  referencing

Open in new window

0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 1

Author Comment

by:takwirirar
ID: 39844259
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

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39844260
That sounds like an issue in your markup file, not your code-behind file. Do you have two <form> tags in your HTML somewhere?
0
 
LVL 1

Author Comment

by:takwirirar
ID: 39844266
No I have just 1
Default.aspx
0
 
LVL 1

Author Comment

by:takwirirar
ID: 39844307
I have noticed that it creates the file which is brilliant news but its just the error that I need to get rid off.
0
 
LVL 1

Author Comment

by:takwirirar
ID: 39844324
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?
0
 
LVL 1

Author Closing Comment

by:takwirirar
ID: 39894270
Thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question