我得到了一项任务,看看我是否可以制作能够生成一些 PDF 文件供最终用户查看的电源应用程序。在通过对这个主题的研究之后,我发现这不是一个容易实现的:) 为了让 power app 生成并下载/显示生成的 pdf,我做了这些步骤:
- 只需一个按钮即可创建电源应用程序 :) 从第 2 步调用 Azure 函数
- 创建的 Azure 函数将生成并返回 pdf 作为 StreamContent
由于电源应用程序的限制(或者我只是找不到方法),我无法从电源应用程序内的响应中获取 pdf。在此之后,我更改了我的 Azure 函数以创建新的 blob 条目,但我知道我无法在 Azure 函数中获取该新条目的 URL,以便将其返回给电源应用程序,然后使用内部电源应用程序下载功能
我的 Azure 功能代码如下
using System;
using System.Net;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using Aspose.Words;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log, Stream outputBlob)
{
log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");
var dataDir = @"D:/home";
var docFile = $"{dataDir}/word-templates/WordAutomationTest.docx";
var uid = Guid.NewGuid().ToString().Replace("-", "");
var pdfFile = $"{dataDir}/pdf-export/WordAutomationTest_{uid}.pdf";
var doc = new Document(docFile);
doc.Save(pdfFile);
var result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new FileStream(pdfFile, FileMode.Open);
stream.CopyTo(outputBlob);
// result.Content = new StreamContent(stream);
// result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
// result.Content.Headers.ContentDisposition.FileName = Path.GetFileName(pdfFile);
// result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
// result.Content.Headers.ContentLength = stream.Length;
return result;
}
我留下了旧代码(将 pdf 流回注释下的代码,作为我尝试过的参考)
有没有办法在 Azure 函数中获取新生成的 blob 条目的下载 URL?有没有更好的方法让电源应用程序生成和下载/显示生成的 PDF?
PS我尝试在power app中使用PDFViewer控件,但是这个控件完全没用,因为U不能通过函数设置文档值
编辑:@mathewc 的回复帮助我最终结束了这个。所有详细信息如下。新的 Azure 功能按预期工作
#r "Microsoft.WindowsAzure.Storage"
using System;
using System.Net;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using Aspose.Words;
using Microsoft.WindowsAzure.Storage.Blob;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log, CloudBlockBlob outputBlob)
{
log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");
var dataDir = @"D:/home";
var docFile = $"{dataDir}/word-templates/WordAutomationTest.docx";
var uid = Guid.NewGuid().ToString().Replace("-", "");
var pdfFile = $"{dataDir}/pdf-export/WordAutomationTest_{uid}.pdf";
var doc = new Document(docFile);
doc.Save(pdfFile);
var result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new FileStream(pdfFile, FileMode.Open);
outputBlob.UploadFromStream(stream);
return req.CreateResponse(HttpStatusCode.OK, outputBlob.Uri);
}
备注:
- 我们需要在 project.json 中添加“WindowsAzure.Storage”:“7.2.1”。此包必须与 %USERPROFILE%\AppData\Local\Azure.Functions.Cli 中同名的包的版本相同