Skip to content

Commit

Permalink
refactor: add positioning parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
schoero committed Nov 1, 2023
1 parent 43b9fa9 commit 74316c2
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 12 deletions.
66 changes: 66 additions & 0 deletions src/pdf/table.test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4904,3 +4904,69 @@ Q
",
]
`;

exports[`table > should render the table on the specified position 1`] = `
[
"1 0 0 -1 0 595.28 cm
370.945 247.64 m
470.945 247.64 m
370.945 247.64 m
470.945 247.64 m
370.945 247.64 m
470.945 247.64 m
370.945 247.64 m
470.945 247.64 m
[] 0 d
0 j
0 J
/Gs1 gs
370.945 348.14 m
370.945 247.14 l
/DeviceRGB CS
0 0 0 SCN
1 w
S
470.945 247.14 m
470.945 348.14 l
/DeviceRGB CS
0 0 0 SCN
1 w
S
370.445 247.64 m
471.445 247.64 l
/DeviceRGB CS
0 0 0 SCN
1 w
S
471.445 347.64 m
370.445 347.64 l
/DeviceRGB CS
0 0 0 SCN
1 w
S
370.945 247.64 m
470.945 247.64 m
/DeviceRGB cs
0 0 0 scn
/Gs2 gs
q
1 0 0 -1 0 595.28 cm
BT
1 0 0 1 399.847 294.8295 Tm
/F1 11 Tf
[<31303078313030> 0] TJ
ET
Q
370.945 247.64 m
470.945 247.64 m
370.945 247.64 m
470.945 247.64 m
370.945 247.64 m
470.945 247.64 m
370.945 247.64 m
470.945 247.64 m
370.945 247.64 m
470.945 247.64 m
",
]
`;
12 changes: 12 additions & 0 deletions src/pdf/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
header,
paddingOverrides,
paddingVariants,
singleCell,
textColorOverrides
} from "swissqrbill:tests:data/table";
import { TestDocument } from "swissqrbill:tests:utils/pdf";
Expand Down Expand Up @@ -121,4 +122,15 @@ describe("table", async () => {
expect(pdf.snapshots).toMatchSnapshot();
});

it("should render the table on the specified position", async () => {
const pdf = new TestDocument("table/position.pdf", { layout: "landscape", margin: 0, size: "A4" });
const headerTable = new Table(singleCell);

headerTable.attachTo(pdf, pdf.page.width / 2 - 50, pdf.page.height / 2 - 50);

await pdf.writeFile();

expect(pdf.snapshots).toMatchSnapshot();
});

});
19 changes: 7 additions & 12 deletions src/pdf/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ export interface PDFTable {
/** Width of whole table. */
width?: number;
/** Horizontal start position of the table. */
x?: number;
/** Vertical start position of the table. */
y?: number;
}

export interface PDFRow {
Expand Down Expand Up @@ -152,10 +149,11 @@ export class Table {
/**
* Attaches the table to a PDFKit document instance.
* @param doc The PDFKit document instance
* @returns The Table instance.
* @param x The horizontal position in points where the table be placed.
* @param y The vertical position in points where the table will be placed.
* @throws { Error } Throws an error if no table rows are provided.
*/
public attachTo(doc: PDFKit.PDFDocument) {
public attachTo(doc: PDFKit.PDFDocument, x: number = doc.x ?? 0, y: number = doc.y ?? 0) {

if(this.data.rows === undefined){
throw new Error("No table rows provided.");
Expand All @@ -168,13 +166,12 @@ export class Table {
// Buffer pages to be able to create table spanning multiple pages
doc.options.bufferPages = true;

const startX = doc.x;
const tableX = x;
const tableY = y;

const startPage = this.getCurrentPage(doc);
const tableX = this.data.x ? this.data.x : doc.x;
const tableY = this.data.y ? this.data.y : doc.y;
const tableWidth = this.data.width ? this.data.width : doc.page.width - tableX - doc.page.margins.right;

const tableWidth = this.data.width ? this.data.width : doc.page.width - tableX - doc.page.margins.right;
const tableBackgroundColor = this.data.backgroundColor ? this.data.backgroundColor : undefined;
const tableBorder = this.data.borderWidth ? this.data.borderWidth : undefined;
const tableBorderColors = this.data.borderColor ? this.data.borderColor : "#000000";
Expand Down Expand Up @@ -442,9 +439,7 @@ export class Table {

}

doc.x = startX;

return this;
doc.x = tableX;

}

Expand Down
17 changes: 17 additions & 0 deletions tests/data/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,3 +580,20 @@ export const header: PDFTable = {
],
verticalAlign: "center"
};

export const singleCell: PDFTable = {
align: "center",
borderWidth: 1,
rows: [
{
columns: [
{
text: "100x100"
}
],
height: 100
}
],
verticalAlign: "center",
width: 100
};

0 comments on commit 74316c2

Please sign in to comment.