2

我找到了一些关于使用控制器从 EvoPDF 创建 PDF 的答案,但似乎没有人处理通过 jQuery AJAX 调用的控制器。

我有一个简单的 jQuery 函数,可以像我的应用程序中的许多其他函数一样将数据发送到控制器:

$.ajax({
    url: "/AnnualFees/showStatement",
    cache: false,
    data: {
        authKey: memberData.authKey,
        entityId: memberData.entityId,
        barNumber: memberData.barNumber,
        statementHTML: encodeURIComponent($("#statementBody").html())
    },
    method: "POST",
    success: function (data) {
    },
});

我遵循了所有示例并拥有此代码。我可以更改它以保存 PDF 并确认正在生成 PDF。

public ActionResult getStatementPDF(string statementHTML)
{
    //initialize the PdfConvert object
    PdfConverter pdfConverter = new PdfConverter();

    // set the license key - required
    pdfConverter.LicenseKey = "uzUmNCcnNCYsIjQgOiQ0JyU6JSY6LS0tLQ==";

    StringBuilder PDFBody = new StringBuilder();
    PDFBody.Append("<!DOCTYPE html>");
    PDFBody.Append("<html lang=\"en\">");
    PDFBody.Append("<head>");
    PDFBody.Append("    <meta charset=\"utf - 8\">");
    PDFBody.Append("    <title>Statement</title>");
    PDFBody.Append("</head>");
    PDFBody.Append("");
    PDFBody.Append("<body>");
    PDFBody.Append("Hello world.");
    PDFBody.Append("</body>");
    PDFBody.Append("</html>");

    byte[] outPdfBuffer = pdfConverter.GetPdfBytesFromHtmlString(PDFBody.ToString());

    // Send the PDF file to browser
    FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf");
    fileResult.FileDownloadName = "Statement.pdf";

    return fileResult;
}

我可以确认它们没有错误,并且使用正确的应用程序/pdf 类型返回 200 成功,并且大小与磁盘上的大小大致相同。但是,从未出现任何 PDF,浏览器中也没有打开任何内容。

4

1 回答 1

0

您需要在 ajax 调用中处理 onSuccess 数据,您可以执行以下操作来打开文件,如果要保存,您可能需要使用 FileSaverJS ( https://github.com/eligrey/FileSaver.js/ )文件

success: function (data) {

    var file = new Blob([data], { type: 'application/pdf' });
    var fileURL = URL.createObjectURL(file);
    window.open(fileURL);
}
于 2016-11-18T23:27:36.020 回答