-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Reporting/Tech Debt] Convert PdfMaker class to TypeScript (#81242)
* convert pdf.js to TS * more typescript * simplify caller * more typescript * more typescript * fix the code to match the expected interface * very cool comment * interface correction * remove unused class method * add unit test for PdfMaker * file rename for typo correction Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
99e90aa
commit ae95bf2
Showing
14 changed files
with
448 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/get_doc_options.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { BufferOptions } from 'pdfmake/interfaces'; | ||
|
||
export function getDocOptions(tableBorderWidth: number): BufferOptions { | ||
return { | ||
tableLayouts: { | ||
noBorder: { | ||
// format is function (i, node) { ... }; | ||
hLineWidth: () => 0, | ||
vLineWidth: () => 0, | ||
paddingLeft: () => 0, | ||
paddingRight: () => 0, | ||
paddingTop: () => 0, | ||
paddingBottom: () => 0, | ||
}, | ||
simpleBorder: { | ||
// format is function (i, node) { ... }; | ||
hLineWidth: () => tableBorderWidth, | ||
vLineWidth: () => tableBorderWidth, | ||
hLineColor: () => 'silver', | ||
vLineColor: () => 'silver', | ||
paddingLeft: () => 0, | ||
paddingRight: () => 0, | ||
paddingTop: () => 0, | ||
paddingBottom: () => 0, | ||
}, | ||
}, | ||
}; | ||
} |
22 changes: 22 additions & 0 deletions
22
x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/get_font.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
// @ts-ignore: no module definition | ||
import xRegExp from 'xregexp'; | ||
|
||
export function getFont(text: string) { | ||
// Once unicode regex scripts are fully supported we should be able to get rid of the dependency | ||
// on xRegExp library. See https://github.com/tc39/proposal-regexp-unicode-property-escapes | ||
// for more information. We are matching Han characters which is one of the supported unicode scripts | ||
// (you can see the full list of supported scripts here: http://www.unicode.org/standard/supported.html). | ||
// This will match Chinese, Japanese, Korean and some other Asian languages. | ||
const isCKJ = xRegExp('\\p{Han}').test(text, 'g'); | ||
if (isCKJ) { | ||
return 'noto-cjk'; | ||
} else { | ||
return 'Roboto'; | ||
} | ||
} |
130 changes: 130 additions & 0 deletions
130
x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/get_template.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { i18n } from '@kbn/i18n'; | ||
import path from 'path'; | ||
import { TDocumentDefinitions } from 'pdfmake/interfaces'; | ||
import { LayoutInstance } from '../../../../lib/layouts'; | ||
import { getFont } from './get_font'; | ||
|
||
export function getTemplate( | ||
layout: LayoutInstance, | ||
logo: string | undefined, | ||
title: string, | ||
tableBorderWidth: number, | ||
assetPath: string | ||
): Partial<TDocumentDefinitions> { | ||
const pageMarginTop = 40; | ||
const pageMarginBottom = 80; | ||
const pageMarginWidth = 40; | ||
const headingFontSize = 14; | ||
const headingMarginTop = 10; | ||
const headingMarginBottom = 5; | ||
const headingHeight = headingFontSize * 1.5 + headingMarginTop + headingMarginBottom; | ||
const subheadingFontSize = 12; | ||
const subheadingMarginTop = 0; | ||
const subheadingMarginBottom = 5; | ||
const subheadingHeight = subheadingFontSize * 1.5 + subheadingMarginTop + subheadingMarginBottom; | ||
|
||
return { | ||
// define page size | ||
pageOrientation: layout.getPdfPageOrientation(), | ||
pageSize: layout.getPdfPageSize({ | ||
pageMarginTop, | ||
pageMarginBottom, | ||
pageMarginWidth, | ||
tableBorderWidth, | ||
headingHeight, | ||
subheadingHeight, | ||
}), | ||
pageMargins: [pageMarginWidth, pageMarginTop, pageMarginWidth, pageMarginBottom], | ||
|
||
header() { | ||
return { | ||
margin: [pageMarginWidth, pageMarginTop / 4, pageMarginWidth, 0], | ||
text: title, | ||
font: getFont(title), | ||
style: { | ||
color: '#aaa', | ||
}, | ||
fontSize: 10, | ||
alignment: 'center', | ||
}; | ||
}, | ||
|
||
footer(currentPage: number, pageCount: number) { | ||
const logoPath = path.resolve(assetPath, 'img', 'logo-grey.png'); // Default Elastic Logo | ||
return { | ||
margin: [pageMarginWidth, pageMarginBottom / 4, pageMarginWidth, 0], | ||
layout: 'noBorder', | ||
table: { | ||
widths: [100, '*', 100], | ||
body: [ | ||
[ | ||
{ | ||
fit: [100, 35], | ||
image: logo || logoPath, | ||
}, | ||
{ | ||
alignment: 'center', | ||
text: i18n.translate('xpack.reporting.exportTypes.printablePdf.pagingDescription', { | ||
defaultMessage: 'Page {currentPage} of {pageCount}', | ||
values: { currentPage: currentPage.toString(), pageCount }, | ||
}), | ||
style: { | ||
color: '#aaa', | ||
}, | ||
}, | ||
'', | ||
], | ||
[ | ||
logo | ||
? { | ||
text: i18n.translate( | ||
'xpack.reporting.exportTypes.printablePdf.logoDescription', | ||
{ | ||
defaultMessage: 'Powered by Elastic', | ||
} | ||
), | ||
fontSize: 10, | ||
style: { | ||
color: '#aaa', | ||
}, | ||
margin: [0, 2, 0, 0], | ||
} | ||
: '', | ||
'', | ||
'', | ||
], | ||
], | ||
}, | ||
}; | ||
}, | ||
|
||
styles: { | ||
heading: { | ||
alignment: 'left', | ||
fontSize: headingFontSize, | ||
bold: true, | ||
margin: [headingMarginTop, 0, headingMarginBottom, 0], | ||
}, | ||
subheading: { | ||
alignment: 'left', | ||
fontSize: subheadingFontSize, | ||
italics: true, | ||
margin: [0, 0, subheadingMarginBottom, 20], | ||
}, | ||
warning: { | ||
color: '#f39c12', // same as @brand-warning in Kibana colors.less | ||
}, | ||
}, | ||
|
||
defaultStyle: { | ||
fontSize: 12, | ||
font: 'Roboto', | ||
}, | ||
}; | ||
} |
Oops, something went wrong.