首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在从DynamicPDF Cloud生成的浏览器中呈现pdf?

如何在从DynamicPDF Cloud生成的浏览器中呈现pdf?
EN

Stack Overflow用户
提问于 2022-07-21 15:51:16
回答 3查看 57关注 0票数 0

使用DynamicPDF的Cloud,而不是生成一个pdf返回到本地文件系统,我希望它直接打开在另一个浏览器选项卡可以立即打印。我该如何做到这一点?

我使用的方法(.NET Core 6/ Blazor)如下:

代码语言:javascript
复制
public async Task CallDynPDFCloudAPI()
    {
  var basePath = @"JSONFiles\";
  var apiKey = "foo";
  var cloudPath = "bar.dlex";
  Pdf pdf = new Pdf();
  pdf.ApiKey = apiKey;
  LayoutDataResource layoutDataResource = new LayoutDataResource(basePath + "FooBar.json");
  pdf.AddDlex(cloudPath, layoutDataResource);
  PdfResponse pdfResponse = pdf.Process();
  if (pdfResponse.IsSuccessful)
  {
    File.WriteAllBytes(basePath + "Manifest_" + manifestBranch + ".pdf", pdfResponse.Content);
  }
  else
  {
    Console.WriteLine(pdfResponse.ErrorJson);
  }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-07-21 16:19:01

重读关于https://learn.microsoft.com/en-us/aspnet/core/blazor/file-downloads?view=aspnetcore-6.0的文章

代码语言:javascript
复制
@page "/file-download-1"
@using System.IO
@inject IJSRuntime JS

<h1> File Download Example</h1>

<button @onclick = "DownloadFileFromStream" >
    Download File From Stream
</button>

@code {

    private Stream CallDynPDFCloudAPI()
    {
        var basePath = @"JSONFiles\";
        var apiKey = "foo";
        var cloudPath = "bar.dlex";
        Pdf pdf = new Pdf();
        pdf.ApiKey = apiKey;
        LayoutDataResource layoutDataResource = new LayoutDataResource(basePath + "FooBar.json");
        pdf.AddDlex(cloudPath, layoutDataResource);
        PdfResponse pdfResponse = pdf.Process();
        if (pdfResponse.IsSuccessful)
        {
            return new MemoryStream(pdfResponse.Content);
        }
        else
        {
            throw new Exception("");
        }
    }


    private async Task DownloadFileFromStream()
    {
        var fileStream = CallDynPDFCloudAPI();
        var fileName = "file.pdf";

        using var streamRef = new DotNetStreamReference(stream: fileStream);

        await JS.InvokeVoidAsync("downloadFileFromStream", fileName, streamRef);
    }
}
票数 1
EN

Stack Overflow用户

发布于 2022-07-21 20:32:21

您将无法从另一个浏览器选项卡访问此请求的PDF内容。我建议在打电话之前打开新的选项卡,然后在那里播放。如果您使用的是'a href‘链接,则可以通过设置’target=‘_blank’属性来实现这一点。如果这是表单提交,则可以设置‘表单’的‘target=“_blank’属性。

另一种选择是将PDF暂时存储在某个地方(作为文件、DB或BLOB存储),然后在打开后将其流到其他选项卡。

票数 1
EN

Stack Overflow用户

发布于 2022-07-22 12:37:30

我用@DynamicPDF建议的修改代码实现了@Mihal的答案,以实现我正在寻找的结果。我的两个目标是:

  1. 不需要将文件保存在服务器或DB

中,客户端设备不需要过多地加载文件

Javascript:

代码语言:javascript
复制
   <script>
        window.downloadFileFromStream = async (fileName, 
            contentStreamReference) => {
            const arrayBuffer = await contentStreamReference.arrayBuffer();
            const blob = new Blob([arrayBuffer], { type: 'application/pdf' });
            const url = URL.createObjectURL(blob);

//--Opens PDF file in new Tab
            fetch(url)
                .then(response => response.blob())
                .then(data => window.open(URL.createObjectURL(data), '_blank'))

//--Downloads file to Browser (uncomment if desired)
            //const anchorElement = document.createElement('a');
            //anchorElement.href = url;
            //anchorElement.download = fileName ?? 'Manifest';
            //anchorElement.click();
            //anchorElement.remove();
            //URL.revokeObjectURL(url);
        }
    </script>

*注意!我的申请纯粹是面向我们组织的内部申请。我们的Windows客户端计算机和浏览器由组策略管理。我还没有在Mac / Safari客户端上测试这个。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73069239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档