feature: generate pdf from actor
This commit is contained in:
@@ -20,7 +20,13 @@ export abstract class AbstractElement {
|
||||
this.maxWidth = maxWidth;
|
||||
}
|
||||
|
||||
public abstract prepareRender(doc: jsPDF, maxWidth?: number): jsPDF;
|
||||
|
||||
public abstract render(doc: jsPDF, maxWidth?: number): jsPDF;
|
||||
|
||||
public abstract getHeight(doc?: jsPDF): number;
|
||||
|
||||
public abstract getCheckNewPageHeight(doc?: jsPDF): number;
|
||||
|
||||
public abstract getElements(): AbstractElement[];
|
||||
}
|
||||
|
||||
@@ -11,6 +11,10 @@ export class Box extends AbstractElement {
|
||||
this.h = h;
|
||||
}
|
||||
|
||||
public prepareRender(doc: jsPDF, _maxWidth?: number): jsPDF {
|
||||
return doc;
|
||||
}
|
||||
|
||||
public render(doc: jsPDF, _maxWidth?: number): jsPDF {
|
||||
doc.rect(this.x, this.y, this.w, this.h);
|
||||
return doc;
|
||||
@@ -19,4 +23,12 @@ export class Box extends AbstractElement {
|
||||
public getHeight(_doc): number {
|
||||
return this.h;
|
||||
}
|
||||
|
||||
public getCheckNewPageHeight(doc?: jsPDF): number {
|
||||
return this.getHeight(doc);
|
||||
}
|
||||
|
||||
public getElements(): AbstractElement[] {
|
||||
return [this];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,20 +9,24 @@ export class Column extends AbstractElement {
|
||||
this.elements = elements;
|
||||
}
|
||||
|
||||
public render(doc: jsPDF, _maxWidth?: number): jsPDF {
|
||||
public prepareRender(doc: jsPDF, _maxWidth?: number): jsPDF {
|
||||
const elements = this.elements ?? [];
|
||||
|
||||
let currentY = this.y;
|
||||
for (let i = 0; i < elements.length; i++) {
|
||||
const element = elements[i];
|
||||
element.x = this.x;
|
||||
element.x = Math.max(element.x, this.x);
|
||||
element.y = currentY;
|
||||
element.render(doc);
|
||||
element.prepareRender(doc);
|
||||
currentY += element.getHeight(doc) + 2;
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
|
||||
public render(doc: jsPDF, _maxWidth?: number): jsPDF {
|
||||
return doc;
|
||||
}
|
||||
|
||||
public getHeight(doc): number {
|
||||
return this.elements
|
||||
.map((e) => e.getHeight(doc))
|
||||
@@ -33,4 +37,18 @@ export class Column extends AbstractElement {
|
||||
return p + c + 2;
|
||||
});
|
||||
}
|
||||
|
||||
public getCheckNewPageHeight(doc?: jsPDF): number {
|
||||
return this.elements.length > 0
|
||||
? this.elements[0].getCheckNewPageHeight(doc)
|
||||
: 0;
|
||||
}
|
||||
|
||||
public getElements(): AbstractElement[] {
|
||||
const elements: AbstractElement[] = [];
|
||||
for (const element of this.elements) {
|
||||
elements.push(...element.getElements());
|
||||
}
|
||||
return elements;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Box } from './box';
|
||||
import jsPDF from 'jspdf';
|
||||
import { AbstractElement } from './abstract-element';
|
||||
|
||||
export class Image extends Box {
|
||||
public imageData: string;
|
||||
@@ -19,4 +20,8 @@ export class Image extends Box {
|
||||
});
|
||||
return doc;
|
||||
}
|
||||
|
||||
public getElements(): AbstractElement[] {
|
||||
return [this];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import jsPDF, { TextOptionsLight } from 'jspdf';
|
||||
import { Text } from './text';
|
||||
import { i18nLocalize, LABEL_SIZE, TEXT_SIZE } from '../constants';
|
||||
import { AbstractElement } from './abstract-element';
|
||||
|
||||
export class LabelledText extends Text {
|
||||
public label: string;
|
||||
@@ -23,8 +24,12 @@ export class LabelledText extends Text {
|
||||
this.updateMaxWidth(maxWidth);
|
||||
}
|
||||
|
||||
public render(doc: jsPDF, maxWidth?: number): jsPDF {
|
||||
public prepareRender(doc: jsPDF, maxWidth?: number): jsPDF {
|
||||
this.updateMaxWidth(maxWidth);
|
||||
return super.prepareRender(doc, maxWidth);
|
||||
}
|
||||
|
||||
public render(doc: jsPDF, _maxWidth?: number): jsPDF {
|
||||
const yLabel = this.y + this.getHeightFromPx(doc, LABEL_SIZE);
|
||||
const yText = yLabel + this.getHeightFromPx(doc, TEXT_SIZE) + 1;
|
||||
doc
|
||||
@@ -50,4 +55,12 @@ export class LabelledText extends Text {
|
||||
public getHeight(doc): number {
|
||||
return this.getHeightFromPx(doc, TEXT_SIZE + LABEL_SIZE) + 1;
|
||||
}
|
||||
|
||||
public getCheckNewPageHeight(doc?: jsPDF): number {
|
||||
return this.getHeight(doc);
|
||||
}
|
||||
|
||||
public getElements(): AbstractElement[] {
|
||||
return [this];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ export class LabelledValues extends Row {
|
||||
}
|
||||
}
|
||||
|
||||
public render(doc: jsPDF, maxWidth?: number): jsPDF {
|
||||
public prepareRender(doc: jsPDF, maxWidth?: number): jsPDF {
|
||||
const pageWidth = doc.internal.pageSize.width;
|
||||
const rowWidth = pageWidth - this.x - MARGINS.right;
|
||||
for (const column of this.elements) {
|
||||
@@ -77,6 +77,10 @@ export class LabelledValues extends Row {
|
||||
labelledValue.maxWidth = rowWidth / this.nbrOfCol;
|
||||
}
|
||||
}
|
||||
return super.render(doc, maxWidth);
|
||||
return super.prepareRender(doc, maxWidth);
|
||||
}
|
||||
|
||||
public render(doc: jsPDF, _maxWidth?: number): jsPDF {
|
||||
return doc;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ export class Row extends AbstractElement {
|
||||
this.maxWidths = maxWidths ?? [];
|
||||
}
|
||||
|
||||
public render(doc: jsPDF, maxWidth?: number): jsPDF {
|
||||
public prepareRender(doc: jsPDF, maxWidth?: number): jsPDF {
|
||||
const elements = this.elements ?? [];
|
||||
let maxWidths = this.maxWidths ?? [];
|
||||
let widthPercents = this.widthPercents ?? [];
|
||||
@@ -50,12 +50,16 @@ export class Row extends AbstractElement {
|
||||
const maxChildWidth = maxWidths[i] ?? percentWidth;
|
||||
element.x = currentX;
|
||||
element.y = this.y;
|
||||
element.render(doc, maxChildWidth);
|
||||
element.prepareRender(doc, maxChildWidth);
|
||||
currentX += percentWidth;
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
|
||||
public render(doc: jsPDF, _maxWidth?: number): jsPDF {
|
||||
return doc;
|
||||
}
|
||||
|
||||
public getHeight(doc?: jsPDF): number {
|
||||
let maxHeight = 0;
|
||||
for (const element of this.elements) {
|
||||
@@ -63,4 +67,20 @@ export class Row extends AbstractElement {
|
||||
}
|
||||
return maxHeight;
|
||||
}
|
||||
|
||||
public getCheckNewPageHeight(doc?: jsPDF): number {
|
||||
let maxHeight = 0;
|
||||
for (const element of this.elements) {
|
||||
maxHeight = Math.max(maxHeight, element.getCheckNewPageHeight(doc));
|
||||
}
|
||||
return maxHeight;
|
||||
}
|
||||
|
||||
public getElements(): AbstractElement[] {
|
||||
const elements: AbstractElement[] = [];
|
||||
for (const element of this.elements) {
|
||||
elements.push(...element.getElements());
|
||||
}
|
||||
return elements;
|
||||
}
|
||||
}
|
||||
|
||||
39
src/elements/separator.ts
Normal file
39
src/elements/separator.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { AbstractElement } from './abstract-element';
|
||||
import jsPDF from 'jspdf';
|
||||
import { MARGINS } from '../constants';
|
||||
|
||||
export class Separator extends AbstractElement {
|
||||
constructor(x: number, y: number, maxWidth?: number | undefined) {
|
||||
super(x, y, maxWidth);
|
||||
}
|
||||
|
||||
public getHeight(_doc?: jsPDF): number {
|
||||
return 0.5;
|
||||
}
|
||||
|
||||
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 {
|
||||
const pageWidth = doc.internal.pageSize.width;
|
||||
const maxPageWidth = pageWidth - MARGINS.left - MARGINS.right;
|
||||
const finalWidth = Math.min(
|
||||
maxWidth ?? this.maxWidth ?? maxPageWidth,
|
||||
maxPageWidth
|
||||
);
|
||||
|
||||
doc.setLineWidth(0.25);
|
||||
doc.line(this.x, this.y, this.x + finalWidth, this.y);
|
||||
|
||||
return doc;
|
||||
}
|
||||
|
||||
public getElements(): AbstractElement[] {
|
||||
return [this];
|
||||
}
|
||||
}
|
||||
@@ -17,11 +17,18 @@ export class Text extends AbstractElement {
|
||||
this.textOptions = textOptions;
|
||||
}
|
||||
|
||||
public render(doc: jsPDF, maxWidth?: number): jsPDF {
|
||||
public prepareRender(doc: jsPDF, maxWidth?: number): jsPDF {
|
||||
this.updateMaxWidth(maxWidth);
|
||||
let finalText = [i18nLocalize(this.text)];
|
||||
return doc;
|
||||
}
|
||||
|
||||
public render(doc: jsPDF, _maxWidth?: number): jsPDF {
|
||||
let finalText: string[] = [i18nLocalize(this.text)];
|
||||
if (this.maxWidth != null) {
|
||||
finalText = doc.splitTextToSize(finalText[0], maxWidth ?? 0);
|
||||
finalText = doc.splitTextToSize(finalText[0], this.maxWidth ?? 0);
|
||||
}
|
||||
if (finalText.length > 1) {
|
||||
finalText[0] = finalText[0].replace(/(.){3}$/, '...');
|
||||
}
|
||||
const yText = this.y + this.getHeightFromPx(doc, TEXT_SIZE);
|
||||
doc
|
||||
@@ -42,4 +49,12 @@ export class Text extends AbstractElement {
|
||||
public getHeight(doc): number {
|
||||
return this.getHeightFromPx(doc, TEXT_SIZE);
|
||||
}
|
||||
|
||||
public getCheckNewPageHeight(doc?: jsPDF): number {
|
||||
return this.getHeight(doc);
|
||||
}
|
||||
|
||||
public getElements(): AbstractElement[] {
|
||||
return [this];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ export class Texts extends Row {
|
||||
}
|
||||
}
|
||||
|
||||
public render(doc: jsPDF, maxWidth?: number): jsPDF {
|
||||
public prepareRender(doc: jsPDF, maxWidth?: number): jsPDF {
|
||||
const pageWidth = doc.internal.pageSize.width;
|
||||
const rowWidth = pageWidth - this.x - MARGINS.right;
|
||||
for (const column of this.elements) {
|
||||
@@ -61,6 +61,10 @@ export class Texts extends Row {
|
||||
labelledValue.maxWidth = rowWidth / this.nbrOfCol;
|
||||
}
|
||||
}
|
||||
return super.render(doc, maxWidth);
|
||||
return super.prepareRender(doc, maxWidth);
|
||||
}
|
||||
|
||||
public render(doc: jsPDF, _maxWidth?: number): jsPDF {
|
||||
return doc;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user