feature: generate pdf from actor
This commit is contained in:
@@ -16,6 +16,10 @@ export abstract class AbstractElement {
|
||||
return size / doc.internal.scaleFactor;
|
||||
}
|
||||
|
||||
public getPxFromSize(doc: jsPDF, size: number) {
|
||||
return size * doc.internal.scaleFactor;
|
||||
}
|
||||
|
||||
protected updateMaxWidth(maxWidth?: number) {
|
||||
this.maxWidth = maxWidth;
|
||||
}
|
||||
|
||||
29
src/elements/blank.ts
Normal file
29
src/elements/blank.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { AbstractElement } from './abstract-element';
|
||||
import jsPDF from 'jspdf';
|
||||
import { Box } from './box';
|
||||
|
||||
export class Blank extends Box {
|
||||
constructor(x: number, y: number, w: number, h: number) {
|
||||
super(x, y, w, h);
|
||||
}
|
||||
|
||||
public static heightBlank(h: number) {
|
||||
return new Blank(0, 0, 0, h);
|
||||
}
|
||||
|
||||
public getCheckNewPageHeight(doc?: jsPDF): number {
|
||||
return this.getHeight(doc);
|
||||
}
|
||||
|
||||
public prepareRender(doc: jsPDF, _maxWidth?: number): jsPDF {
|
||||
return doc;
|
||||
}
|
||||
|
||||
public render(doc: jsPDF, _maxWidth?: number): jsPDF {
|
||||
return doc;
|
||||
}
|
||||
|
||||
public getElements(): AbstractElement[] {
|
||||
return [this];
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Row } from './row';
|
||||
import { Text } from './text';
|
||||
import { MultilineText } from './multiline-text';
|
||||
|
||||
export class LabelledValue extends Row {
|
||||
public label: string;
|
||||
@@ -9,13 +10,14 @@ export class LabelledValue extends Row {
|
||||
label: string,
|
||||
value: number,
|
||||
widthPercents?: number[],
|
||||
multiline = false,
|
||||
maxWidth?: number
|
||||
) {
|
||||
super(
|
||||
0,
|
||||
0,
|
||||
[
|
||||
new Text(0, 0, label),
|
||||
multiline ? new MultilineText(0, 0, label) : new Text(0, 0, label),
|
||||
new Text(0, 0, value.toString(), {
|
||||
align: 'right',
|
||||
}),
|
||||
|
||||
@@ -12,7 +12,8 @@ export class LabelledValues extends Row {
|
||||
x: number,
|
||||
y: number,
|
||||
labelledValues: { label: string; value: number }[],
|
||||
nbrOfCol?: number
|
||||
nbrOfCol?: number,
|
||||
multiline = false
|
||||
) {
|
||||
super(x, y, []);
|
||||
this.labelledValues = labelledValues;
|
||||
@@ -47,7 +48,8 @@ export class LabelledValues extends Row {
|
||||
new LabelledValue(
|
||||
labelledValues[i].label,
|
||||
labelledValues[i].value,
|
||||
widthPercent
|
||||
widthPercent,
|
||||
multiline
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -61,7 +63,8 @@ export class LabelledValues extends Row {
|
||||
new LabelledValue(
|
||||
libelledValue.label,
|
||||
libelledValue.value,
|
||||
widthPercent
|
||||
widthPercent,
|
||||
multiline
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
42
src/elements/multiline-text.ts
Normal file
42
src/elements/multiline-text.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { AbstractElement } from './abstract-element';
|
||||
import jsPDF, { TextOptionsLight } from 'jspdf';
|
||||
import { i18nLocalize, TEXT_SIZE } from '../constants';
|
||||
import { Text } from './text';
|
||||
|
||||
export class MultilineText extends Text {
|
||||
private nbrLine = 1;
|
||||
|
||||
constructor(
|
||||
x: number,
|
||||
y: number,
|
||||
text: string,
|
||||
textOptions?: TextOptionsLight
|
||||
) {
|
||||
super(x, y, text, textOptions);
|
||||
}
|
||||
|
||||
public prepareRender(doc: jsPDF, maxWidth?: number): jsPDF {
|
||||
doc.setFontSize(TEXT_SIZE);
|
||||
this.updateMaxWidth(maxWidth);
|
||||
let finalText: string[] = [i18nLocalize(this.text)];
|
||||
if (this.maxWidth != null) {
|
||||
finalText = doc.splitTextToSize(finalText[0], this.maxWidth);
|
||||
}
|
||||
this.nbrLine = finalText.length;
|
||||
return doc;
|
||||
}
|
||||
|
||||
public render(doc: jsPDF, _maxWidth?: number): jsPDF {
|
||||
const yText = this.y + this.getHeightFromPx(doc, TEXT_SIZE);
|
||||
doc.setFontSize(TEXT_SIZE).text(this.text, this.x, yText, this.textOptions);
|
||||
return doc;
|
||||
}
|
||||
|
||||
public getHeight(doc): number {
|
||||
return this.getHeightFromPx(doc, TEXT_SIZE) * this.nbrLine;
|
||||
}
|
||||
|
||||
public getElements(): AbstractElement[] {
|
||||
return [this];
|
||||
}
|
||||
}
|
||||
@@ -23,17 +23,16 @@ export class Text extends AbstractElement {
|
||||
}
|
||||
|
||||
public render(doc: jsPDF, _maxWidth?: number): jsPDF {
|
||||
doc.setFontSize(TEXT_SIZE);
|
||||
let finalText: string[] = [i18nLocalize(this.text)];
|
||||
if (this.maxWidth != null) {
|
||||
finalText = doc.splitTextToSize(finalText[0], this.maxWidth ?? 0);
|
||||
finalText = doc.splitTextToSize(finalText[0], this.maxWidth);
|
||||
}
|
||||
if (finalText.length > 1) {
|
||||
finalText[0] = finalText[0].replace(/(.){3}$/, '...');
|
||||
}
|
||||
const yText = this.y + this.getHeightFromPx(doc, TEXT_SIZE);
|
||||
doc
|
||||
.setFontSize(TEXT_SIZE)
|
||||
.text(finalText[0], this.x, yText, this.textOptions);
|
||||
doc.text(finalText[0], this.x, yText, this.textOptions);
|
||||
return doc;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,12 +3,19 @@ import { Column } from './column';
|
||||
import jsPDF from 'jspdf';
|
||||
import { MARGINS } from '../constants';
|
||||
import { Text } from './text';
|
||||
import { MultilineText } from './multiline-text';
|
||||
|
||||
export class Texts extends Row {
|
||||
public texts: string[];
|
||||
public nbrOfCol: number;
|
||||
|
||||
constructor(x: number, y: number, texts: string[], nbrOfCol?: number) {
|
||||
constructor(
|
||||
x: number,
|
||||
y: number,
|
||||
texts: string[],
|
||||
nbrOfCol?: number,
|
||||
multiline = false
|
||||
) {
|
||||
super(x, y, []);
|
||||
this.texts = texts;
|
||||
this.nbrOfCol = nbrOfCol ?? 4;
|
||||
@@ -39,7 +46,11 @@ export class Texts extends Row {
|
||||
currentIndex = 3;
|
||||
}
|
||||
(<Column>this.elements[currentIndex]).elements.push(
|
||||
new Row(0, 0, [new Text(0, 0, texts[i])])
|
||||
new Row(0, 0, [
|
||||
multiline
|
||||
? new MultilineText(0, 0, texts[i])
|
||||
: new Text(0, 0, texts[i]),
|
||||
])
|
||||
);
|
||||
}
|
||||
} else {
|
||||
@@ -47,7 +58,14 @@ export class Texts extends Row {
|
||||
new Column(
|
||||
0,
|
||||
0,
|
||||
texts.map((text) => new Row(0, 0, [new Text(0, 0, text)]))
|
||||
texts.map(
|
||||
(text) =>
|
||||
new Row(0, 0, [
|
||||
multiline
|
||||
? new MultilineText(0, 0, text)
|
||||
: new Text(0, 0, text),
|
||||
])
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user