天天看點

界面控件DevExtreme——輕松将TreeList資料導出為PDF格式

作者:慧都科技

DevExtreme擁有高性能的HTML5 / JavaScript小部件集合,使您可以利用現代Web開發堆棧(包括React,Angular,ASP.NET Core,jQuery,Knockout等)建構互動式的Web應用程式,該套件附帶功能齊全的資料網格、互動式圖表小部件、資料編輯器等。

DevExtreme Complete Subscription官方最新版免費下載下傳試用,曆史版本下載下傳,線上文檔和幫助檔案下載下傳-慧都網

導出為PDF

要啟用Data Grid的PDF導出選項,請導入jsPDF庫。在元件中,設定export.enabled屬性為true然後指定導出格式。接下來的操作顯示DataGrid中的Export按鈕,單擊此按鈕時,将觸發dataGrid.onExporting函數(其中開發者應該調用pdfExporter.exportDataGrid(options)方法)。

界面控件DevExtreme——輕松将TreeList資料導出為PDF格式
<dx-data-grid ...
(onExporting)="onExporting($event)"
>
<dxo-export
[enabled]="true"
[formats]="['pdf']"
></dxo-export>
</dx-data-grid>           
import { Component } from '@angular/core';
import { exportDataGrid } from 'devextreme/pdf_exporter';
import { jsPDF } from 'jspdf';

// ...

export class AppComponent {
onExporting(e) {
const doc = new jsPDF();
exportDataGrid({
jsPDFDocument: doc,
component: e.component,
}).then(() => {
doc.save('DataGrid.pdf');
});
}
}           

單元格自定義

開發人員可以自定義單元格内容并在PDF文檔中繪制自定義單元格。

customizeCell函數允許開發者為導出的PDF文檔自定義單個DataGrid單元格的外觀(例如,可以更改單元格使用的字型樣式)。

界面控件DevExtreme——輕松将TreeList資料導出為PDF格式
onExporting(e) {
const doc = new jsPDF();
exportDataGrid({
jsPDFDocument: doc,
component: e.component,
customizeCell({ gridCell, pdfCell }) {
//...
}
}).then(() => {
doc.save('DataGrid.pdf');
});
}           

如果希望在繪制單元格時覆寫預設繪制邏輯并應用自己的繪制邏輯,請使用customDrawCell函數。在下面的例子中,這個函數在PDF文檔中為" Website "列繪制并自定義一個單元格:

界面控件DevExtreme——輕松将TreeList資料導出為PDF格式
onExporting(e) {
const doc = new jsPDF();
exportDataGrid({
jsPDFDocument: doc,
component: e.component,
customDrawCell({ gridCell, pdfCell }) {
//...
}
}).then(() => {
doc.save('DataGrid.pdf');
});
}           

頁眉和頁腳

開發人員可以輕松地向導出的DataGrid添加頁眉和頁腳。

DataGrid元件位于PdfExportDataGridProps中指定點的PdfExportDataGridProps.topLeft 屬性的頁眉之前。

對于頁腳位置,使用customDrawCell函數,這個函數允許開發者計算元件最右邊單元格的坐标。

界面控件DevExtreme——輕松将TreeList資料導出為PDF格式

導出圖檔

jsPDF庫API還允許将自定義内容導出為PDF(如圖像),對于圖像導出,可以調用jsPDF.addImage方法,然後處理onRowExporting事件為導出的DataGrid定制行。

界面控件DevExtreme——輕松将TreeList資料導出為PDF格式
onExporting(e) {
const doc = new jsPDF();
exportDataGrid({
jsPDFDocument: doc,
component: e.component,
onRowExporting: (e) => {
// Customize rows
},
customDrawCell: (e) => {
// Detect picture cell
doc.addImage(e.gridCell.value, 'PNG', e.rect.x, e.rect.y, e.rect.w, e.rect.h);
e.cancel = true;
}
}).then(() => {
doc.save('DataGrid.pdf');
});
}           

導出多個網格

要将多個DataGrid元件導出到一個檔案中,并在PDF文檔的不同頁面上排列它們,請在Promises鍊中使用pdfExporter.exportDataGrid(options)方法。

exportGrids() {
const doc = new jsPDF();
DevExpress.pdfExporter.exportDataGrid({
jsPDFDocument: doc,
component: gridOneInstance,
}).then(() => {
doc.addPage();
DevExpress.pdfExporter.exportDataGrid({
jsPDFDocument: doc,
component: gridTwoInstance,
}).then(() => {
doc.save('MultipleGrids.pdf');
});
});
}           

繼續閱讀