Power App - 生成 PDF

jok*_*ksa 6 pdf azure-functions powerapps

我收到了一项任务,看看我是否可以制作强大的应用程序来生成一些 PDF 文件供最终用户查看。经过对这个主题的研究后,我发现这并不容易实现:)为了使 power app 生成并下载/显示生成的 pdf,我执行了以下步骤:

  1. 只需一键创建电源应用程序:)即可从步骤 2 调用 Azure 函数
  2. 创建了 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;
}
Run Code Online (Sandbox Code Playgroud)

我留下了旧代码(在注释下将 pdf 流回的代码,作为我尝试过的参考)

有没有办法获取 Azure 函数中新生成的 blob 条目的下载 URL?有没有更好的方法让 Power App 生成并下载/显示生成的 PDF?

PS我尝试在power app中使用PDFViewer控件,但是这个控件完全没用,因为你无法通过函数设置文档值

编辑:@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);
}
Run Code Online (Sandbox Code Playgroud)

评论

  1. 我们需要在project.json 中添加“WindowsAzure.Storage”:“7.2.1”。此包必须与 %USERPROFILE%\AppData\Local\Azure.Functions.Cli 中的同名包版本相同

mat*_*ewc 4

如果将 Blob 输出绑定类型从 更改StreamCloudBlockBlob,您将可以访问CloudBlockBlob.Uri所需的 Blob 路径(此处的文档)。然后,您可以将该 Uri 返回到您的 Power App。您可以使用CloudBlockBlob.UploadFromStreamAsync将 PDF 流上传到 blob。