Rank: Newbie Groups: Member
Joined: 1/6/2012 Posts: 4
|
Hi
I'm trying to save dynamically created invoices to a directory on my server so that I can send them as attachments to my clients once they've submitted their information. I don't want it to bring up the dialog box asking if I want to open or save the .pdf.
I found an earlier post on your site that deals with this exact issue (ASPXToPdf1_AfterRender) but when I try to implement the suggestions, I cannot get it to work.
1. I've created the AfterRender event as per code I found in your pages (I'm using VB.Net): Protected Sub ASPXToPDF1_AfterRender(ByVal sender As Object, ByVal e As EventArgs) Dim result As HtmlToPdfResult = CType(ASPXToPDF1.Result, HtmlToPdfResult) Dim doc As New PdfDocument doc = result.PdfDocument Dim str As String str = Server.MapPath("Downloads\MyFileName.pdf") doc.Save(str) End Sub
2. After I called ASPXToPDF1.RenderAsPDF(str) I call ASPXToPDF1_AfterRender(sender, e) (ASPXToPDF1.RenderAsPDF(str) works fine if I run it alone. It brings up the Open/Save dialog and the pdf is rendered.) When I call ASPXToPDF1_AfterRender(sender, e), the system crashes with a NullReferenceException on the line "doc = result.PdfDocument". When I check the content for "result" it is "Nothing".
What am I doing wrong? Why is ASPXToPDF1.Result being cleared? Am I calling the ASPXToPDF1_AfterRender sub correctly?
I would appreciate your feedback.
Btw, I tried putting the commands of the Protected Sub ASPXToPDF1_AfterRender directly underneath the RenderAsPDF command as opposed to calling the Sub, but I got the same results. i.e. ASPXToPDF1.RenderAsPDF(str) Dim result As HtmlToPdfResult = CType(ASPXToPDF1.Result, HtmlToPdfResult) .....
The ASPXToPDF1.RenderAsPDF(str) is called in my Page_Load event.
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,194
|
Hi, In order to prevent the open/save dialog, you must call RenderAsPDF with "false" argument:
Code: C#
//The "false" argument prevent the open/save dialog from opening
ASPXToPDF1.RenderAsPDF(false);
In order to save the PDF, you must handle AfterRender event. This is an EVENT just like the Click event for a Button control. You DO NOT CALL IT directly. The ASPXToPDF control calls it with the right arugment. Hope this helps. Please let us know if you still have any questions. Thanks
|
Rank: Newbie Groups: Member
Joined: 1/6/2012 Posts: 4
|
Thanks for the feedback.
It is sorted out now.
ASPXToPDF1.RenderAsPDF(False) renders the pdf without showing the open/save dialog
In order for the AfterRender event to fire I had to add the "Handles" bit to the Sub declaration. For other newbies like myself here's the code I used:
Protected Sub ASPXToPDF1_AfterRender(ByVal sender As Object, ByVal e As EventArgs) Handles ASPXToPDF1.AfterRender Dim result As HtmlToPdfResult result = CType(ASPXToPDF1.Result, HtmlToPdfResult) Dim doc As New PdfDocument doc = result.PdfDocument Dim str As String str = Server.MapPath("Downloads\MyFileName.pdf") doc.Save(str) End Sub
This matter is now resolved and can be closed. Thanks.
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,194
|
Glad to hear that it works for you! Please feel free to let us know if you have any more questions.
Thanks!
|