无法打开以 C#/ASP.NET MVC 应用程序生成的 PDF 文件

PDF file generated in C# / ASP.NET MVC application can not be opened

提问人:Jonathan Small 提问时间:10/19/2023 最后编辑:marc_sJonathan Small 更新时间:10/19/2023 访问量:33

问:

我有一个 C# ASP.NET MVC 应用程序,它使用以下代码生成一个文件:.pdf

public ActionResult BuildPDF() 
{
    // <code executes to establish printdocs>>
    PdfFileEditor oEditor = new PdfFileEditor();
    oEditor.Concatenate(printdocs.ToArray(), printfinalStream);
    printfinalStream.Seek(0, SeekOrigin.Begin);

    return File(printfinalStream, "application/pdf", "Authorization_" + GetNewGuid() + ".pdf");
}

此代码正确生成一个文件并将其返回到浏览器,并将其放入下载文件夹中。.pdf

现在,我需要将文本字符串与文件一起返回,以便在该过程完成时可以向用户发出文本字符串警报。

这是新代码的样子:

public ActionResult BuildPDF() 
{
    // <code executes to establish printdocs>>
    PdfFileEditor oEditor = new PdfFileEditor();
    oEditor.Concatenate(printdocs.ToArray(), printfinalStream);
    printfinalStream.Seek(0, SeekOrigin.Begin);

    var combinedData = new CombinedData
    {
        Text = msg,
        PdfData = printfinalStream.ToArray(), // Convert the MemoryStream to a byte array.
        FileName = "Authorization_" + GetNewGuid() + ".pdf"
    };

    return Json(combinedData, JsonRequestBehavior.AllowGet);
}

这就是我的 Javascript 的样子:

$.ajax({
   type: 'GET',
   url: '@Url.Action("PrintandEmailAuthorization", "Forms")',
   data: {
          "LetterDated": obj.LetterDated, "AuthDated": obj.AuthDated, "ReturnBy": obj.ReturnBy, "ProtestMonth": obj.ProtestMonth,
          "TaxYear": obj.TaxYear, "FromFileNum": obj.FromFileNum, "ToFileNum": obj.ToFileNum, "Suffix": obj.Suffix, "PaperSource": obj.PaperSource,
          "LetterSignedBy": obj.LetterSignedBy, "Title": obj.Title, "ConsentAttorney": obj.ConsentAttorney, "includeexemption": obj.Includeexemption,
          "includethirdrequest": obj.Includethirdrequest, "includeconsent": obj.Includeconsent, "includeref": obj.Includeref,
          "AttorneyName": obj.AttorneyName, "AttorneyEmail": obj.AttorneyEmail, "TemplateName": obj.TemplateName, "TemplatePath": obj.TemplatePath, "Enclosures": obj.Enclosures,
          "OldContingencyFee": obj.OldContingencyFee, "NewContingencyFee": obj.NewContingencyFee, "ContactID": obj.ContactID, "OptionSelected": obj.OptionSelected,
          "IncludeMultLotsRe": obj.IncludeMultLotsRe, "VillTaxYear": obj.VillTaxYear, "isTest": false
      },
      success: function (data) {
                alert(data.Text);
                // Create a Blob for the PDF data.
                var blob = new Blob([data.PdfData], { type: 'application/pdf' });

                // Create a temporary URL for the Blob.
                var url = window.URL.createObjectURL(blob);

                // Create a hidden anchor element to trigger the download.
                var a = document.createElement('a');
                a.style.display = 'none';
                a.href = url;
                a.download = data.FileName;
                document.body.appendChild(a);

                // Simulate a click on the anchor element to trigger the download.
                a.click();

                // Clean up the temporary URL and anchor element.
                window.URL.revokeObjectURL(url);
                document.body.removeChild(a);
            }
        });

该过程运行,我收到短信警报,文件确实下载了,但是当我尝试打开结果时,我收到一个错误,告诉我数据已损坏。.pdf.pdf

我确定这是将数据从服务器转换到客户端的简单问题,但我不太确定。.pdf

有人有什么建议吗?

谢谢。

C# ajax asp.net-mvc PDF PDF生成

评论

0赞 Jonathan Small 10/19/2023
@KJ - 我用记事本打开了正确生成的.pdf(当只有一个文件返回到浏览器时)。文件的第一行是“%PDF-1.7”。然后我用记事本打开了坏文件(用 blob 创建)。文件的第一行以“37,80,68,70,45,49,46,55,13,37,200,200,200”开头。因此,数据不会添加到文件中。也许是因为我执行了两次 ToArray(一次在 oEditor.Concatenate 命令中,一次在创建 combinedData 对象时)?我必须运行一些测试。
0赞 Eduardo Molteni 10/20/2023
如果它有效,那就太棒了!
1赞 Jonathan Small 10/20/2023
@KJ - 我最终将生成的.pdf保存到服务器文件夹,并将一个对象返回给视图,其中包含要显示的文本和保存的.pdf文件的路径/文件名。然后在视图中,在ajax成功时,我提醒短信,然后执行windows.href返回控制器,传回路径/文件名。然后,控制器返回文件。我还放入了一个计时器,因此在 5 秒后,我返回控制器以从服务器中删除文件并返回视图。我知道这需要很多来回,但它完成了我需要做的事情。

答: 暂无答案