使用Winnovative PDFConverter保存到C#中由会话数据填充的ASP表文件

使用Winnovative PDFConverter保存到C#中由会话数据填充的ASP表文件,c#,asp.net,file,session,winnovative,C#,Asp.net,File,Session,Winnovative,我正在使用PDFConverter将“收据页”保存到计算机上。它的设置方式是使用从第X页到Response.Redirect()到Receipt页的链接,因此它传输会话数据 然后我使用Session[“Cart”]数据用收据中的数据填充表。问题是,当我使用Winnovative PDFConverter将页面保存到文件时,它不会保存表。这是因为没有发送会话数据(或者它创建了一个新会话,不确定)时,表不会被填充,因此我保存的文件不包含该表,它是接收页面的整个点。我该怎么做?我在网上找到了一个“解决

我正在使用PDFConverter将“收据页”保存到计算机上。它的设置方式是使用从第X页到Response.Redirect()到Receipt页的链接,因此它传输会话数据

然后我使用Session[“Cart”]数据用收据中的数据填充表。问题是,当我使用Winnovative PDFConverter将页面保存到文件时,它不会保存表。这是因为没有发送会话数据(或者它创建了一个新会话,不确定)时,表不会被填充,因此我保存的文件不包含该表,它是接收页面的整个点。我该怎么做?我在网上找到了一个“解决办法”,但似乎不起作用。它甚至不会保存文件,而是返回一个错误

protected void Save_BtnClick(object sender, EventArgs e) 
    {
        PdfConverter converter = new PdfConverter();
        string url = HttpContext.Current.Request.Url.AbsoluteUri;

        StringWriter sw = new StringWriter();
        Server.Execute("Receipt.aspx", sw); //this line triggers an HttpException
        string htmlCodeToConvert = sw.GetStringBuilder().ToString();

        converter.SavePdfFromUrlToFile(htmlCodeToConvert, @"C:\Users\myname\Downloads\output.pdf"); // <--not even sure what method i should be running here

        //converter.SavePdfFromUrlToFile(url,@"C:\Users\myname\Downloads\output.pdf"); <-- this saves it without the table
    }
protectedvoid保存\u btn单击(对象发送方,事件参数e)
{
PdfConverter转换器=新的PdfConverter();
字符串url=HttpContext.Current.Request.url.AbsoluteUri;
StringWriter sw=新的StringWriter();
Server.Execute(“Receipt.aspx”,sw);//此行触发HttpException
字符串htmlCodeToConvert=sw.GetStringBuilder().ToString();

converter.SavePdfFromUrlToFile(htmlcdetoconvert,@“C:\Users\myname\Downloads\output.pdf”);//我不知道服务器.Execute引发异常的原因,但这里有一个替代使用该调用的方法

您正在使用Server.Execute来获取要转换为HTML的网页。使用Winnovative来代替该方法进行调用。诀窍是允许该页面请求访问当前用户的会话。您指定要调用的URL,然后在PDFConverter的cookie集合中提供用户凭据

请参阅本页,并根据您的身份验证技术选择方法

编辑:

@LordHonydew,我们在评论中走了一个大圈子,在开头就结束了。让我再试一次

首先,在Server.Execute exception中是否存在内部异常?可能有更多信息可以帮助解释出现了什么问题。即使在修复该问题后,也有其他项目必须修复

第二,当使用Winnovative从安全的网页生成PDF时,您必须向PDFConverter提供凭据,以便它可以访问该网页及其资源。Winnovative有两种获取HTML的方法:一种是向PDFConverter提供要调用的URL,另一种是使用服务器。执行以获取HTML d直接向PDFConverter提供HTML,这就是您的做法。无论哪种方式,PDFConverter仍然需要与服务器对话以获取额外的页面资源。图像和CSS文件等内容不在HTML中,它们由HTML引用。转换器将调用服务器以获取这些项目。因为您的应用程序是安全的。您必须向转换器提供访问服务器的凭据。我们将使用用于每个页面请求的相同身份验证cookie来实现此目的。还有其他方法,例如提供用户名和密码。上面的链接解释了各种方法

此代码从当前请求获取身份验证cookie,并将其提供给转换器:

pdfConverter.HttpRequestCookies.Add(FormsAuthentication.FormsCookieName,
     Request.Cookies[FormsAuthentication.FormsCookieName].Value);
最后,converter.SavePdfFromUrlToFile不是正确的使用方法。这只会将回执保存到本地服务器的驱动器。您需要将其流回到用户

尝试以下操作。在catch块中设置断点,以便查看是否存在内部异常

protected void Save_BtnClick(object sender, EventArgs e)
    {

        // Get the web page HTML as a string
        string htmlCodeToConvert = null;
        using (StringWriter sw = new StringWriter())
        {
            try
            {
                System.Web.HttpContext.Current.Server.Execute("Receipt.aspx", sw);
                htmlCodeToConvert = sw.ToString();

            }
            catch (Exception ex)
            {
                // set breakpoint below and on an exception see if there is an inner exception.
                throw;                    
            }
        }

        PdfConverter converter = new PdfConverter();
        // Supply auth cookie to converter
        converter.HttpRequestCookies.Add(System.Web.Security.FormsAuthentication.FormsCookieName,
            Request.Cookies[System.Web.Security.FormsAuthentication.FormsCookieName].Value);

        // baseurl is used by converter when it gets CSS and image files
        string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority +
            Request.ApplicationPath.TrimEnd('/') + "/";

        // create the PDF and get as bytes
        byte[] pdfBytes = converter.GetPdfBytesFromHtmlString(htmlCodeToConvert, baseUrl);

        // Stream bytes to user
        Response.Clear();
        Response.AppendHeader("Content-Disposition", "attachment;filename=Receipt.pdf");
        Response.ContentType = "application/pdf";
        Response.OutputStream.Write(pdfBytes, 0, pdfBytes.Length);
        HttpContext.Current.ApplicationInstance.CompleteRequest();

    }
请检查。您可以在其中找到转换期间用于保留会话数据的方法的说明以及C#示例代码。下面的代码是从中复制的:

protected void convertToPdfButton_Click(object sender, EventArgs e)
{
    // Save variables in Session object
    Session["firstName"] = firstNameTextBox.Text;
    Session["lastName"] = lastNameTextBox.Text;
    Session["gender"] = maleRadioButton.Checked ? "Male" : "Female";
    Session["haveCar"] = haveCarCheckBox.Checked;
    Session["carType"] = carTypeDropDownList.SelectedValue;
    Session["comments"] = commentsTextBox.Text;

    // Execute the Display_Session_Variables.aspx page and get the HTML string 
    // rendered by this page
    TextWriter outTextWriter = new StringWriter();
    Server.Execute("Display_Session_Variables.aspx", outTextWriter);

    string htmlStringToConvert = outTextWriter.ToString();

    // Create a HTML to PDF converter object with default settings
    HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

    // Set license key received after purchase to use the converter in licensed mode
    // Leave it not set to use the converter in demo mode
    htmlToPdfConverter.LicenseKey = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=";

    // Use the current page URL as base URL
    string baseUrl = HttpContext.Current.Request.Url.AbsoluteUri;

    // Convert the page HTML string to a PDF document in a memory buffer
    byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlStringToConvert, baseUrl);

    // Send the PDF as response to browser

    // Set response content type
    Response.AddHeader("Content-Type", "application/pdf");

    // Instruct the browser to open the PDF file as an attachment or inline
    Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Convert_Page_in_Same_Session.pdf; size={0}", outPdfBuffer.Length.ToString()));

    // Write the PDF document buffer to HTTP response
    Response.BinaryWrite(outPdfBuffer);

    // End the HTTP response and stop the current page processing
    Response.End();
}


Display Session Variables in Converted HTML Page

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        firstNameLabel.Text = Session["firstName"] != null ? (String)Session["firstName"] : String.Empty;
        lastNameLabel.Text = Session["lastName"] != null ? (String)Session["lastName"] : String.Empty;
        genderLabel.Text = Session["gender"] != null ? (String)Session["gender"] : String.Empty;

        bool iHaveCar = Session["haveCar"] != null ? (bool)Session["haveCar"] : false;
        haveCarLabel.Text = iHaveCar ? "Yes" : "No";
        carTypePanel.Visible = iHaveCar;
        carTypeLabel.Text = iHaveCar && Session["carType"] != null ? (String)Session["carType"] : String.Empty;

        commentsLabel.Text = Session["comments"] != null ? (String)Session["comments"] : String.Empty;
    }
}

您好,谢谢您的回复。我对您所说的有点困惑,我不知道您所说的身份验证技术是什么意思。从您发送给我的链接来看,似乎我唯一要做的事情就是使用“pdfConverter.LicenseKey=”B4MyijubijiniayijuzhpMahprkze=“;”设置许可证密钥它可以很好地保存pdf。因此,如果我理解正确,请在我的原始URL调用中使用Winnovative,还可以创建并发送必要的cookies来生成表?通过身份验证技术,我指的是用户如何通过身份验证-Windows或表单。是的,我的观点是,您需要提供cookies,以便Win发出的请求novative可以访问相同的会话信息。我一定错过了一些重要的内容,因为我不明白为什么需要对用户进行身份验证。至于作为cookie发送,我目前有会话[“ProductCart”]当然,我可以创建cookie,但我如何让页面加载知道如何获取cookie而不是会话数据?如何将会话数据存储到cookie中?我是否必须转换为字符串,将字符串放入cookie,将cookie传递到winnovative方法,并为页面加载处理cookie而不是会话设置条件数据?@LordHoneydew,所以这个过程不需要用户登录?是的,他们登录了,但他们看不到保存选项,除非他们登录并在最终收据上?你能帮我回答我的cookies问题吗?对不起,我有点纠结。如果我不知道购物车中有多少物品,我如何通过所有这些ose项目转换成cookies,因为我需要先将它们转换成字符串。作为记录,我这样做主要是为了学习:)这可能解释了我的新手能力。