Export a Power Apps gallery as PDF file using the new PDF function, and email it with the Outlook connector

With the recently experimental PDF feature that is available from Power Apps authoring versions 3.22094.* we are now able to export screens, galleries and containers to PDF and obtain the output of this export as bytes, and use it on the PDF viewer or send it to a flow, so we can save it to SharePoint, OneDrive and etc.

The advantage of this approach instead using the Print function to print screens is that we can get the full content of galleries/containers in a PDF, so we are not limited to only what is displayed in the screen.

The text in the PDF file exported using this function is also selectable, so we are here exporting ‘real’ PDFs, and not screenshots from screens into PDF files:

Sending the exported PDF in an email message

Consider you have a gallery with content you want to export as below. The gallery content is not fully visible as below:

The PDF export function is really simple, you can use it to generate content directly or to save it in variables to be used later.

But in this example let’s only call it directly from the Office 365 Outlook Connector (Send an email V2) action.

Add the Office 365 Outlook connector to your app, and then simply add the code as the example below to a button/icon to Export the gallery to PDF and send the content as an Attachment:

Office365Outlook.SendEmailV2(
    "user@tenant.onmicrosoft.com",
    "Your pdf",
    "Your PDF is attached",
    {
        From: User().Email,
        Attachments: Table(
            {
                ContentBytes: PDF(
                    galSessionsData,
                    {
                        Orientation: PaperOrientation.Portrait,//Portrait or Landscape
                        Size: PaperSize.A4,//Select your page size
                        DPI: 150,
                        ExpandContainers: true /* This property makes sure the full content of gallery/containers is expanded as PDF */
                    }
                ),
                Name: "Export.pdf"
            }
        )
    }
);

Note that the Attachments parameter in the SendEmailV2 action expects a table of attachment objects with the ContentBytes and FileName as parameters. As ContentBytes we are using the PDF function to export it directly and send in the Email.

For the PDF function, use the proper DPI and page sizes you wish, but keep the ExpandContainers property value as true if you want to export the full gallery.

Results

After the button is clicked, the recipient will receive an email with a PDF copy of the gallery attached, as below:

Note: As of now, the PDF export function will only export items that were already loaded in the app. So for example if you have a SharePoint list with 8000 items, by default the gallery component loads the items in batches as you scroll (not all the 8000 at first), and if you call the PDF function in a situation like this, it will export only the items that were already loaded into the gallery.

Reference

[New] Create PDF from Power Apps with PDF function by Hiro Nagao

7 comments

  1. The expand containers doesn’t work when published although it works fine in the development environment (preview). The pdf keeps getting cutoff to just the limit of the screen and showing the blank background for the rest. any suggestions?

    1. Hello Funmi, testing here and it seems to work fine with me. Are you using standard Galleries? What container are you using? Can you send how are you using the formula?

  2. Hello Michel,

    Great article, Thank you !
    I have an application with 2 galleries. The first one is populated with items that are users from a group, and the other is a collection that is populated with users from first table. Items with users from first gallery are “moved” to a collection and then, displayed to the secondary gallery.
    Like in your example, I put a button that convert both galleries in pdf and send them as attachments to mail. This work great, but if one of these gallery are empty, of course, we get an error…. cannot generate a pdf with an empty gallery .
    The main problem is: I try every kind of “combination” to eliminate first gallery from conversion to pdf if this is empty, but with no success !
    Details:
    Both galleries have at Visible -> !IsEmpty(GalleryX….. (to be hidden if they do not contain items)
    I try to eliminate the first gallery from conversion by using:
    If(!IsEmpty(Gallery1.AllItems),
    or…
    If(Gallery1.Visible = true,
    Office365Outlook.SendEmailV2(…..
    and other kind of combinations, but seems that any conditions are ignored in PowerApps and it get the error to generate that pdf with that gallery, NOT ONLY WHEN IS EMPTY !!
    Any kind of condition added at the beginning of “Office365Outlook.SendEmailV2(…..” to eliminate that gallery from export to pdf, is ignored !!! I get the error to generate that pdf … with that gallery… because all conditions are ignored…
    Can you give me any suggestions please ? How is correct to put a condition to eliminate a gallery in this case ? I have other cases with conditions to eliminate a gallery, all are functional, they run, but not in this case….
    What I do wrong ?

    Thank you !

    1. Hello Virgiliu,

      I just tested the scenario you described and it seems to be a glitch in the functionality.

      What I did and made it to work was: Add a toggle control to your screen, rename it to togGalIsEmpty make it hidden and set the default value to: IsEmpty(Gallery1.AllItems).

      In your formula use instead:
      If(!togIsGalEmpty.Value, Office365Outlook.SendEmailV2(…..

      I know it’s a workaround but we have to bear in mind the functionality is not yet in GA.

  3. Hi, great article and instructions – exactly what i was looking for. One small problem I’m having is the print out of gallery through this pdf function, the page breaks are cutting off vital lines of data in the gallery, is there any workaround for this?

    1. Hey Chris, not that I am aware of. I believe that if you use fixed height gallery you can tweak it to match the page size (or export to PDF in a page size that matches it)

Leave a Reply

Your email address will not be published. Required fields are marked *