使用DynamicPDF的Cloud,而不是生成一个pdf返回到本地文件系统,我希望它直接打开在另一个浏览器选项卡可以立即打印。我该如何做到这一点?
我使用的方法(.NET Core 6/ Blazor)如下:
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);
}
}发布于 2022-07-21 16:19:01
重读关于https://learn.microsoft.com/en-us/aspnet/core/blazor/file-downloads?view=aspnetcore-6.0的文章
@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);
}
}发布于 2022-07-21 20:32:21
您将无法从另一个浏览器选项卡访问此请求的PDF内容。我建议在打电话之前打开新的选项卡,然后在那里播放。如果您使用的是'a href‘链接,则可以通过设置’target=‘_blank’属性来实现这一点。如果这是表单提交,则可以设置‘表单’的‘target=“_blank’属性。
另一种选择是将PDF暂时存储在某个地方(作为文件、DB或BLOB存储),然后在打开后将其流到其他选项卡。
发布于 2022-07-22 12:37:30
我用@DynamicPDF建议的修改代码实现了@Mihal的答案,以实现我正在寻找的结果。我的两个目标是:
中,客户端设备不需要过多地加载文件
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客户端上测试这个。
https://stackoverflow.com/questions/73069239
复制相似问题