-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Reporting/Tech Debt] Convert PdfMaker class to TypeScript #81242
Changes from 10 commits
911c66a
e9e2edb
2e0a624
ca79e98
bf3dec4
8125e3b
ef22b39
b3743d3
edf6af4
57b94a4
3f2c6ab
58dd494
f4f6bbe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
}, | ||
}, | ||
}; | ||
} |
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'; | ||
} | ||
} |
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], | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous code used kibana/x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/index.js Lines 275 to 276 in a74cea3
|
||||||
}, | ||||||
warning: { | ||||||
color: '#f39c12', // same as @brand-warning in Kibana colors.less | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow, this comment is a bit outdated. Will leave it as-is until it becomes an issue. |
||||||
}, | ||||||
}, | ||||||
|
||||||
defaultStyle: { | ||||||
fontSize: 12, | ||||||
font: 'Roboto', | ||||||
}, | ||||||
}; | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Compare to
kibana/x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/index.js
Lines 268 to 269 in a74cea3
The previous code used invalid properties:
marginTop
,marginBottom
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the
margin(Left|Top|Right|Bottom)
is supported in the code, which means the previous code should be expected to work. So changing tomargin: [x, y, z, q]
should lead to the same result.https://github.com/bpampuch/pdfmake/blob/b4b4906f7db27a78b33051cd90410aec4ec73478/tests/unit/DocMeasure.spec.js#L502