0

I'm trying to create a pdf of the content on a page ("returnsPage.aspx?id="returnId) and allow the user to download this directly when clicking the button.

However in my onClick method I have the following code:

lnkLoadPDF.CommandArgument = "/returns/returnsPage.aspx?id="+returnId.ToString(); 
string virtualPath = lnkLoadPDF.CommandArgument;
string fileName = System.IO.Path.GetFileName(virtualPath);

Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.WriteFile(virtualPath);
Response.ContentType = "";
Response.End();
Response.Redirect("/returns/returnsPage.aspx?id="+returnId);

which returns this error:

'/returns/returnsPage.aspx?id=23' is not a valid virtual path.

Can anyone please tell me what I'm doing wrong?

Thanks!

1
  • Is this a build error or a runtime error? Commented Nov 28, 2011 at 0:17

2 Answers 2

1

In order to turn a webpage into a pdf, you must convert it to pdf on the server. In order to do that, you must have a program on the server that can do that for you.

I've tried a variety of webpage-to-pdf converters and one of the better ones is a free, open source program called wkhtmltopdf.

After you create the pdf, you can either redirect the user to the newly created pdf (discouraged), or prompt them to download it with a savefile dialog.

If you get stuck, just search for wkhtmltopdf on stackoverflow or post another question.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks I'm going to try this. Sorry if my question was too simple.
1

You can't send a file to the client and redirect him to a new location during the same request. You also can't create a PDF from a webpage without some kind of component that converts the HTML into a PDF, it's (quite a bit) more tricky that what I think you're trying to attempt.

As for your exception, are you sure returnsPage.aspx exists? :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.