import { PdfPageSettings, SizeF} from '@syncfusion/ej2-pdf-export';
// Grid’s created event handler
created() {
// The ‘processSectionExportProperties’ method of the pdf export module is overridden
(this.grid.pdfExportModule as any).processSectionExportProperties = function (section, pdfExportProperties) {
// A new page settings variable is initialized
let pdfPageSettings: PdfPageSettings = new PdfPageSettings();
// Grid’s total page count is retrieved
var pageCount = this.parent.pageSettings.pageCount;
// Grid’s size properties are retrieved
var gridResult = this.parent.element.getBoundingClientRect();
// The new size(width and height) for the pdf documented is calculated
// new SizeF(Current pdf width, Current Grid element height * page count)
let result: SizeF = new SizeF(pdfPageSettings.size.width, gridResult.height * pageCount);
// The new size is appended to the pdf page settings size property and assigned to the pdf document
pdfPageSettings.size = new SizeF((<any>result).width, (<any>result).height);
section.setPageSettings(pdfPageSettings);
return section;
}
} |
Hi Sujith,
Your Stackblitz sample doesn't seem to address the original problem. He wants to fit the grid on A4 paper. But that code changes it to a different custom paper size, and then when trying to print the PDF in Chrome, Chrome coerces it to A4, and the scale is very small.
Is there a solution that will keep the page size the same, but resize the grid to fit?
Thanks
Hi Sujith,
Using your stackblitz sample, it's not breaking pages. It prints just one page with whatever content fits on that first page. The rest of the content is truncated.
BTW, I'm trying to attach screenshots, but this web site is finicky about size. It would be nice if it would just compress it automatically to the desired size.
Thanks.
I achieved a solution for the grid as shown in code below, but setting the column width doesn't work 100% on the the pivot table. What is the proper way to set width on pivot table when exporting to pdf?
Hi Sujith, It's both. My code snippet above works for the EJ2 Grid component. I'm also trying to get it to work for the grid inside the EJ2 Pivot table component, but it doesn't really work, i.e., there's some minor effect on width but not what what the function is specifying. I'm accessing the grid in the EJ2 Pivot table via this.$refs.pivot.ej2Instances.grid
dataBound() {
if (this.isInit) {
this.isInit = false;
this.pivotObj.grid.pdfQueryCellInfo = this.pdfQueryCellInfo.bind(this);
}
}
pdfQueryCellInfo(args) {
(this.pivotObj.renderModule as any).pdfRowEvent(args);
const pdfColumns = args.cell.gridRow.pdfGrid.columns.internalColumns;
if (pdfColumns._widthWasFitToPage) return;
let gridWidth = 0;
pdfColumns.forEach((col) => {
gridWidth += col.width;
});
const pageWidthPixels = 500;
const widthRatio = Math.min(1, pageWidthPixels / gridWidth);
pdfColumns.forEach((pdfColumn) => {
pdfColumn.width *= widthRatio;
pdfColumn._widthWasFitToPage = true;
});
pdfColumns._widthWasFitToPage = true;
} |