尝试将 PDF 流显示到网页以进行打印

Trying to display PDF stream to web page for printing

我正在尝试获取包含 HTML 或 PDF(我的选择)的内存流。

我 运行 遇到的问题是我无法将此内容呈现到我的页面。内容将显示在页面上,但在屏幕上全是乱码。如果我采用相同的流并将其保存到文件并打开它,它会正确显示。

从服务器 returns 我是不是设置有误?

在我的网页上我有:

function getData()
{
        $.ajax({
            type: 'GET',
            url: '/Main/MyController/GetPrintData',
            data: {
                reportID: 1,
                format: 'pct',
                cumeReach: 'abc',
                showAddl: 'true',
                precision: 'false'
            },
            contentType: 'application/pdf' //'text/html'
        }).success(function (result) {
            $('#reportData').append(result);
        }).error(function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
        });
};

HTML 看起来像这样:

<html>
  <head>
     <title>Print3</title>
  </head>
  <body onload="getData();">
    <div id="reportData">

    </div>
  </body>
</html>

注意 .success() , .error() 在 jQuery 中被弃用;可以用 .done() , .fail() 代替 .success() , .error()

尝试

html

<embed width="100%" height="100%" name="plugin" id="result" />

js

$("#result")[0].src = URL.createObjectURL(new Blob([result], {"type":"application/pdf"}));

看来我原来的解决方案是正确的。 检查代码后,我发现内存流的创建方式存在问题。

我正在使用 Aspose 创建流。 我以前有 return book.SaveToStream(); 我把它换成了 流 finalStream = new MemoryStream(); book.Save(finalStream, saveOptions); return 最终流;

一旦我做了那个改变,显示就成功了!