首页 文章

Power App - 生成PDF

提问于
浏览
4

我得到了一项任务,看看我是否可以制作能够为最终用户生成一些PDF文件的电源应用程序 . 通过对这个主题的研究后我发现这不容易实现:)为了使电源应用程序生成和下载/显示生成的pdf,我做了以下步骤:

  • 只用一个按钮创建的电源应用程序:)从第2步调用Azure功能

  • 创建Azure函数,该函数将生成并返回pdf作为StreamContent

由于强大的应用程序限制(或者我找不到方法),我无法从power应用程序中的响应中获取pdf . 在此之后,我更改了我的Azure功能以创建新的blob条目但是知道我有问题在Azure函数中获取该新条目的URL以便将其返回到power app然后使用内部power app下载功能

我的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?

附:我尝试在power app里面使用PDFViewer控件,但是这个控件完全没用,因为U无法通过函数设置Document值

EDIT :来自@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);
}

REMARKS

  • 我们需要在project.json中添加"WindowsAzure.Storage":"7.2.1" . 此包 MUST 与%USERPROFILE%\ AppData \ Local \ Azure.Functions.Cli中具有相同名称的版本相同

1 回答

  • 3

    如果将blob输出绑定类型从 Stream 更改为 CloudBlockBlob ,则可以访问 CloudBlockBlob.Uri ,这是您需要的blob路径(文档here) . 然后,您可以将该Uri返回到您的Power App . 您可以使用 CloudBlockBlob.UploadFromStreamAsync 将PDF Stream上传到blob .

相关问题