Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
deryl1974
Frequent Visitor

Export PDF from paginated report in c#

Hi All,

 

I am trying to export a paginated report into pdf from c# application. Below is my code:

 

private async Task<string> PostExportRequest(Guid reportId, Guid groupId)
{
try
{
var paginatedReportExportConfiguration = new PaginatedReportExportConfiguration()
{
FormatSettings = new Dictionary<string, string>() { { "PageHeight", "14in" }, { "PageWidth", "8.5in" }, { "StartPage", "1" }, { "EndPage", "1" } }
};

var exportRequest = new ExportReportRequest { Format = FileFormat.PDF, PaginatedReportConfiguration = paginatedReportExportConfiguration };

var _apiUrl = new Uri("https://app.powerbi.com/groups/<groupId>/rdlreports/<reportId>/ExportTo");
var handler = new System.Net.Http.HttpClientHandler() { UseDefaultCredentials = false };
string clientID = "xxx-xxx-xxx-xxx-xxx-xxxx";
string secret = "xxx-xxx-xxx-xxx-xxx-xxxx";
var credentials = new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(clientID, secret);
var AuthorityUrl = "https://login.microsoftonline.com/consumers/oauth2/v2.0/<my application tenentid>";
var authenticationContext = new AuthenticationContext(AuthorityUrl);
var authenticationResult = await authenticationContext.AcquireTokenAsync("https://analysis.windows.net/powerbi/api", credentials).ConfigureAwait(false);
var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");
var pbClient = new PowerBIClient(_apiUrl, tokenCredentials);
var export = await pbClient.Reports.ExportToFileInGroupAsync(groupId, reportId, exportRequest);
return export.Id;
}
catch (Exception ex){} return null;

}

 

 

Whenever i try to run the code I get auth token correctly, however ExportToFileInGroupAsync gives me an error of below:

 

{"Operation returned an invalid status code 'InternalServerError'"}

 

Does anybody know what I am doing wrong here?

 

Appreciate your help in advance.

 

Thanks
Lloyd

5 REPLIES 5
Anonymous
Not applicable

HI @deryl1974,

AFAIK, the 'internal server error' normally means you may send the wrong contents or send to the wrong target so that the remote server can't respond to these requests.

You can take a look at the following link if helps:

REST API error code 500 handling - Stack Overflow

Regards,

Xiaoxin Sheng

AmosHersch
Microsoft Employee
Microsoft Employee

Hi Lloyd,

 

Sorry for the confusion. Since you're using Power BI C# SDK for the API call you should just use the base URL instead of the full URL of the API:

var _apiUrl = new Uri("https://api.powerbi.com/");

 

The SDK will build the full URL and send the request to Power BI

AmosHersch
Microsoft Employee
Microsoft Employee

Hi Lloyd.

 

I believe that in the URL "rdlreports" should be "reports" (same API URL for both power bi reports and paginated reports)

Can you please try with:

var _apiUrl = new Uri("https://app.powerbi.com/groups/<groupId>/reports/<reportId>/ExportTo");

 

Thanks

Hi AmosHersch,

 

I tried the below and got the same error. 

 

var _apiUrl = new Uri("https://app.powerbi.com/groups/<groupId>/reports/<reportId>/ExportTo");

 

Thanks

Lloyd

HI Lloyd,

Did you have any luck with this one.  I've started the same process and am getting the same error.  My code is identical to yours.

Thanks

Keegan

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.