我正在尝试为嵌入的 Power BI 报告实现“导出为 PDF”功能 (https://learn.microsoft.com/en-us/power-bi/developer/embedded/export-to)。文档中解释的过程非常清楚,但我只有一个疑问尚未解决:我们需要如何传递应用的切片器/过滤器才能将当前报告视图作为 PDF 文件?我的意思是,假设我有一个报告,其中包含在特定时间应用了特定值的自定义切片器,例如要查看与德国国家/地区相关的所有数据,我们如何传递此切片器?它必须在 ODATA 查询字符串中转换吗?我认为这非常不舒服,因为从 JavaScript API 来看,这意味着我们需要检索报告中所有切片器对象的所有状态并将它们转换为 ODATA 查询字符串,还管理特定情况(切片器不适用于特定视觉和其他案例...)不是一项简单的工作。还有其他选择吗?
查询字符串过滤不适用于发布到 Web 或导出到 PDF。但是您可以捕获书签中的过滤器,然后使用您应用的过滤器或切片器将报告导出为 pdf。
// Capture the Bookmark state
const capturedBookmark = await report.bookmarksManager.capture();
捕获的状态(只是一个字符串)然后被传递到 PowerBI .NET SDK 方法
ExportToFileInGroupAsync
作为必要的 PowerBIReportExportConfiguration
的一部分。
参考资料:
我遇到了完全相同的问题。该企业希望导出带有选定筛选器的嵌入式 powerBI 仪表板。我用以下函数解决了这个问题。您需要访问嵌入报告的容器。在下面的代码中它是
@ViewChild('reportContainer') reportContainer?: PowerBIReportEmbedComponent;
。通过过滤器的关键是使用 await report.bookmarksManager.capture()
,然后使用 defaultBookmark 参数,如下所示。
async exportAndDownloadReport(): Promise<void> {
if (!this.reportContainer) {
return;
}
this.percentComplete = 1;
const report = this.reportContainer.getReport();
const reportId = report.getId();
console.log(`reportId= ${reportId}`);
// Capture the Bookmark state
const capturedBookmark = await report.bookmarksManager.capture();
// Define the export settings
const settings = {
format: 'PDF',
powerBIReportConfiguration: {
defaultBookmark: {
state: capturedBookmark.state
}
}
};
const headers = new HttpHeaders({
'Content-Type': 'application/json',
Authorization: 'Bearer ' + this.embedInfo?.accessToken
});
try {
// Send the export request
const response: any = await this.http.post(`https://api.powerbi.com/v1.0/myorg/reports/${reportId}/ExportTo`,
settings, {headers}).toPromise();
const exportId = response.id;
console.log(`exportId= ${exportId}`);
// Poll to check if the export is complete
const intervalId = setInterval(async () => {
const exportStatus = await this.checkExportStatus(reportId, exportId, headers);
this.percentComplete = exportStatus.percentComplete === 0 ? 1 : exportStatus.percentComplete;
if (exportStatus.status === 'Succeeded') {
await this.downloadExportedFile(reportId, exportId, headers);
this.reset(intervalId, 'Export Succeeded');
} else if (exportStatus.status !== 'Running') {
this.reset(intervalId, 'Export failed');
}
}, 1000);
setTimeout(() => {
this.reset(intervalId, 'Export timeout');
}, 300000);
} catch (error) {
console.log(`Export failed: ${error}`);
}
}
您还可以使用
setInterval(async ()
实现轮询订阅,以便导出完成后即可下载文件。
private async checkExportStatus(reportId: string, exportId: string, headers: HttpHeaders): Promise<any> {
try {
const response: any = await this.http.get(`https://api.powerbi.com/v1.0/myorg/reports/${reportId}/exports/${exportId}`, {
responseType: 'json',
headers
}).toPromise();
return {percentComplete: response.percentComplete, status: response.status};
} catch (error) {
console.log(`Failed to check export status: ${error}`);
}
}
最后,当 PDF 文件准备好时,您将需要代码来下载它。
private async downloadExportedFile(reportId: string, exportId: any, headers: HttpHeaders): Promise<void> {
// Download the exported file
const file = await this.http.get(`https://api.powerbi.com/v1.0/myorg/reports/${reportId}/exports/${exportId}/file`, {
responseType: 'blob',
headers
}).toPromise();
const link = document.createElement('a');
link.href = window.URL.createObjectURL(file);
link.download = reportId + '.pdf';
link.click();
}