画布到 PDF 错误

本文关键字:错误 PDF | 更新日期: 2023-09-27 18:36:55

我有应用程序,要求就像我需要在弹出窗口中将画布保存到 pdf 文档一样。为此,我使用了第三方工具将图像数据转换为pdf。

现在我是怎么做的:

  1. 首先我创建了将画布数据从 UI 发送到服务器的 Web 方法以JSON格式,我将其转换为MemoryStream。

  2. 我调用服务器按钮单击事件,我将内存流转换为pdf公文。它正在成功创建。

但是当我使用以下代码返回它时:

        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();
        // inform the browser about the binary data format
        HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");
        // let the browser know how to open the PDF document
        HttpContext.Current.Response.AddHeader("Content-Disposition",
          String.Format("{0}; filename=HtmlToPdf.pdf;size={1}", "attachment",           pdfBuffer.Length.ToString()));
     HttpContext.Current.Response.BinaryWrite(pdfBuffer);
  //HttpContext.Current.Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();

它给了我以下例外:

[FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters. ]
   System.Convert.FromBase64String(String s) +0
   System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) +77
   System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState) +4
   System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState) +37
   System.Web.UI.HiddenFieldPageStatePersister.Load() +147
[ViewStateException: Invalid viewstate. 
    Client IP: ::1
    Port: 
    Referer: http://[mysite]/Report.aspx
    Path: /Report.aspx
    User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
    ViewState: /wEPDwUJNTc2MDY1MzA3D2QWAgIDD2QWBGYPZBYEZg9kFgxmD2QWAgIBDxYCHglpbm5lcmh0bWwFrgk8c2VsZWN0IGNsYXNzPSdzZWxlY3QnIElEPSAnZGltZW5zaW9uU2VsZWN0JyBvbkNoYW5nZT0nRXhwYW5kQWNjb3JkaW9uKHRoaXMsbGJsVGV4dCknPiA8b3B0Z3JvdXAgbGFiZWw9J1RpbWUnIGlkPSdUX1lfRGltZW5zaW9uXzEnPiA8b3B0aW9uIGlkID0nWScgdmFsdWUgPScwXzJfMV8xX0lOX1RpbWUnPlllYXJseTwvb3B0aW9uPiA8b3B0aW9uIGlkID0nTScgdmFsdWUgPScwXzJfMV8xX0lOX1RpbWUnPk1vbnRobHk8L29wdGlvbj4gPG9wdGlvbiBpZCA9J0QnIHZhbHVlID0nMF8yXzFfMV9JTl9UaW1lJz5EYWlseTwvb3B0aW9uPiA8b3B0aW9uIGlkID0nSCcgdmFsdWUgPScwXzJfMV8xX0lOX1RpbWUnPkhvdXJseTwvb3B0aW9uPiA8L29wdGdyb3VwPiA8b3B0Z3JvdXAgbGFiZWw9J0dlb2dyYXBoeScgaWQ9J0dfWV9EaW1lbnNpb25fMic...

请帮助我这里缺少什么?

画布到 PDF 错误

这个异常似乎很有帮助:

[格式异常:输入不是有效的 Base-64 字符串,因为它 包含非 base 64 字符、两个以上的填充字符,或 填充字符中的非空格字符。]

以这种方式尝试:

Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName );
Response.AddHeader( "Content-Length", strFileSize );
Response.ContentType = "application/pdf" ;