开发者

ASP.NET Response.Rewrite Not Taking Effect At First

Edit: Problem solved. Thanks to all for your suggestions.

Working on a commercial product to print a given page to PDF.

The way the system works is that a PrintToPDF.aspx page is loaded, with details on the page to be converted submitted via post arguments.

The Page_Loaded function creates a PDF file using wkHtmlToPdf, then uses Response.Write to write a new pages to load that PDF File, as such:

Response.Clear();

Response.Write("<html><body onload=\"parent.OpenPDF('tmp/" + Path.GetFileName(sPdfFile) + "');\"></body></html>");

The .aspx page by default contains the following code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PrintToPdf.aspx.cs" Inherits="DataServer.PrintToPdf" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>X-Info Maps Print to PDF&开发者_JAVA技巧lt;/title>
    </head>
    <body onload="alert('An error occurred generating the pdf');">
    </body>
</html>

So, what SHOULD happen is the PDF is generated (this works correctly), then the page's HTML response is rewritten to load the PDF via a Javascript call attached to the onload event.

INSTEAD, no matter what, the default page's alert triggers. I've traced through the program, and it definitely calls the Response.Clear() and .Write() functions.

What's interesting is that, after the first failure, if I press the printToPdf button a second time, it correctly loads the PDF page.

Any hints as to what is going wrong?


at the end you need a Response.End()

EDIT - so your code should look like:

Response.Clear();
Response.Write("<html><body onload=\"parent.OpenPDF('tmp/" + Path.GetFileName(sPdfFile) + "');\"></body></html>"); 
Response.End();


So, turns out there was a separate issue causing the branch of code to not run. (I had forgotten to include WaitForExit on the process in which I was running wkHtml2PDF =S)


Move that response.write code to page_prerender event from page_load. It should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜