Forum Discussion

Kayvon's avatar
Kayvon
Frequent Visitor
4 years ago
Solved

export to pdf

I have a single page report designed in power bi. It is designed to show information for one project at a time. We have a total of 100 projects. Instead of clicking in power bi, pick a project and th...
  • v-kkf-msft's avatar
    v-kkf-msft
    4 years ago

    Hi Kayvon ,

     

    Please try to use PowerShell script to call Reports - Export To File In Group , Reports - Get Export To File Status In Group  and Get File Of Export To File In Group APIs to export multiple reports to PDF at one time. The report needs to be located in Premium or Embedded capacity, and PPU capacity is not supported. For more restrictions, please refer to Considerations and limitations .

     

    Below is my sample PowerShell script to set 3 different parameter values for the report and then export 3 PDF files and save them locally.

     

    # Parameters - fill these in before running the script!
    
    $authUrl = 'https://login.microsoftonline.com/416e-----tenant id-----f54/oauth2/token';
    $clientId="98------------20"
    $clientSecret = "37G7Q--------------fk"
    
    #This is the location where the file is finally exported to. You can change this location.
    
    $Folder="C:\Users\Administrator\Desktop\"
    
    # Get token
    
    $body = @{
        'grant_type' = 'client_credentials';
        'resource' = 'https://analysis.windows.net/powerbi/api';
        'client_id' = $clientId;
       'client_secret' = $clientSecret;
    };
    $authResponse = Invoke-RestMethod -Uri $authUrl –Method POST -Body $body
    
    # Parameters - fill these in before running the script!
    
    $pbireportId = "--------"
    $groupID = "------------"
    $restURL = "https://api.powerbi.com/v1.0/myorg/groups/$groupID/reports/$pbireportId/ExportTo"
    
    # Building Rest API header with authorization token
    
    $headers = @{
        "Content-Type" = "application/json";
        "Authorization" = $authResponse.token_type + " " + $authResponse.access_token
        }
    
    # Export File 1 - set the file format as pdf
    
    $body = "{`"format`":`"pdf`", 
               `"powerBIReportConfiguration`": { `"reportLevelFilters`": [{ `"filter`": `"DimDate/CalendarYear eq 2012`" }]} 
     }"
    $body
    $output= Invoke-RestMethod -Uri $restURL -headers $headers -Method POST -Body $body
    
    # Get File 1 Status
    
    $fileid=$output.id
    $restURL01 = "https://api.powerbi.com/v1.0/myorg/groups/$groupID/reports/$pbireportId/Exports/$fileid"
    $filestatus = "";
    Do
    {
    Write-Output "Waiting for the Export to be available"
    $filestatus=Invoke-RestMethod -Uri $restURL01 -headers $headers -Method GET
    $filestatus=$filestatus.status
    $filestatus
    Start-Sleep -Seconds 20
    }
    While($filestatus -ne "Succeeded")
    
    start-sleep -Seconds 20
    
    # Download the file 1
    
    $restURL03 = "https://api.powerbi.com/v1.0/myorg/groups/$groupID/reports/$pbireportId/Exports/$fileid/file"
    Invoke-RestMethod -Uri $restURL03 -headers $headers -Method GET -OutFile $Folder"file1.pdf"
    
    # ==============================
    
    # Export File 2 - set the file format as pdf
    
    $body = "{`"format`":`"pdf`", 
               `"powerBIReportConfiguration`": { `"reportLevelFilters`": [{ `"filter`": `"DimDate/CalendarYear eq 2005`" }]} 
     }"
    $body
    $output= Invoke-RestMethod -Uri $restURL -headers $headers -Method POST -Body $body
    
    # Get File 2 Status
    
    $fileid=$output.id
    $restURL01 = "https://api.powerbi.com/v1.0/myorg/groups/$groupID/reports/$pbireportId/Exports/$fileid"
    $filestatus = "";
    Do
    {
    Write-Output "Waiting for the Export to be available"
    $filestatus=Invoke-RestMethod -Uri $restURL01 -headers $headers -Method GET
    $filestatus=$filestatus.status
    $filestatus
    Start-Sleep -Seconds 20
    }
    While($filestatus -ne "Succeeded")
    
    start-sleep -Seconds 20
    
    # Download the file 2
    
    $restURL03 = "https://api.powerbi.com/v1.0/myorg/groups/$groupID/reports/$pbireportId/Exports/$fileid/file"
    Invoke-RestMethod -Uri $restURL03 -headers $headers -Method GET -OutFile $Folder"file2.pdf"
    
    # ==============================
    
    # Export File 3 - set the file format as pdf
    
    $body = "{`"format`":`"pdf`", 
               `"powerBIReportConfiguration`": { `"reportLevelFilters`": [{ `"filter`": `"DimDate/CalendarYear eq 2010`" }]} 
     }"
    $body
    $output= Invoke-RestMethod -Uri $restURL -headers $headers -Method POST -Body $body
    
    # Get File 3 Status
    
    $fileid=$output.id
    $restURL01 = "https://api.powerbi.com/v1.0/myorg/groups/$groupID/reports/$pbireportId/Exports/$fileid"
    $filestatus = "";
    Do
    {
    Write-Output "Waiting for the Export to be available"
    $filestatus=Invoke-RestMethod -Uri $restURL01 -headers $headers -Method GET
    $filestatus=$filestatus.status
    $filestatus
    Start-Sleep -Seconds 20
    }
    While($filestatus -ne "Succeeded")
    
    start-sleep -Seconds 20
    
    # Download the file 3
    
    $restURL03 = "https://api.powerbi.com/v1.0/myorg/groups/$groupID/reports/$pbireportId/Exports/$fileid/file"
    Invoke-RestMethod -Uri $restURL03 -headers $headers -Method GET -OutFile $Folder"file3.pdf"

     

    Referencing: PowerShell Scripts to Export Power BI Reports to PBIX/PDF/PPT 

     

     

    If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
    Best Regards,
    Winniz
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.