Power App - generate PDF
Asked Answered
O

1

6

I got an assignment to see if I can make power app that will generate some PDF file for end user to see. After through research on this topic I found out that this is not an easy to achieve :) In order to make power app generate and download/show generated pdf I made these steps:

  1. Created power app with just one button :) to call Azure function from step 2
  2. Created Azure function that will generate and return pdf as StreamContent

Due to power app limitations (or I just could not find the way) there was no way for me to get pdf from response inside power app. After this, I changed my Azure function to create new blob entry but know I have problem to get URL for that new entry inside Azure function in order to return this to power app and then use inside power app Download function

My Azure function code is below

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;
}

I left old code (the one that streams pdf back under comments just as reference of what I tried)

Is there any way to get download URL for newly generated blob entry inside Azure function? Is there any better way to make power app generate and download/show generated PDF?

P.S. I tried to use PDFViewer control inside power app, but this control is completely useless cause U can not set Document value via function

EDIT: Response from @mathewc helped me a lot to finally wrap this up. All details are below. New Azure function that works as expected

#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:

  1. Wee need to add "WindowsAzure.Storage" : "7.2.1" inside project.json. This package MUST be the same version as one with same name that is in %USERPROFILE%\AppData\Local\Azure.Functions.Cli
Olag answered 1/3, 2017 at 11:35 Comment(1)
Well done! I have a similar problem. What did you do with the URI back in the PowerApp? How does the security work? I will check out the link below as well :-)Congregation
K
4

If you change your blob output binding type from Stream to CloudBlockBlob you will have access to CloudBlockBlob.Uri which is the blob path you require (documentation here). You can then return that Uri back to your Power App. You can use CloudBlockBlob.UploadFromStreamAsync to upload your PDF Stream to the blob.

Kongo answered 2/3, 2017 at 16:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.