pdf-lib-extended 1.0.33 → 1.0.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/PDFLibExtended.js +79 -85
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pdf-lib-extended",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.34",
|
|
4
4
|
"description": "This project extends the capabilities of the pdf-lib JavaScript library by providing a set of helper functions that simplify common PDF manipulation tasks. It includes utilities for drawing and formatting text, images, and shapes within PDF documents, allowing for more advanced customization and automation. The class-based architecture, designed as a toolkit, ensures that developers can easily integrate these enhanced features into their existing workflows. With this extension, users can streamline the creation of dynamic and complex PDFs with minimal effort.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": {
|
package/src/PDFLibExtended.js
CHANGED
|
@@ -372,10 +372,18 @@ class PDFLibExtended {
|
|
|
372
372
|
}
|
|
373
373
|
|
|
374
374
|
/**
|
|
375
|
-
*### Moves the pointer to the next line of the current page
|
|
375
|
+
*### Moves the pointer to the next line of the current page, adjusting the Y position by the provided padding amount
|
|
376
|
+
* @param {number} padding - The amount of padding to add to the line
|
|
377
|
+
* @returns {object} - An object containing the added space, the new X position, and the new Y position
|
|
376
378
|
*/
|
|
377
379
|
nextLine(padding = 0) {
|
|
378
|
-
|
|
380
|
+
const addedSpace = this.getTextSize() + padding;
|
|
381
|
+
this.getCurrentPage().moveTo(this.getMargin().left, this.getCurrentPage().getY() - addedSpace);
|
|
382
|
+
return {
|
|
383
|
+
addedSpace: addedSpace,
|
|
384
|
+
x: this.getMargin().left,
|
|
385
|
+
y: this.getCurrentPage().getY()
|
|
386
|
+
}
|
|
379
387
|
}
|
|
380
388
|
|
|
381
389
|
/**
|
|
@@ -468,8 +476,8 @@ class PDFLibExtended {
|
|
|
468
476
|
const defaultOptions = {
|
|
469
477
|
align: "left",
|
|
470
478
|
range: {
|
|
471
|
-
|
|
472
|
-
|
|
479
|
+
left: this.getMargin().left,
|
|
480
|
+
right: this.getCurrentPage().getWidth() - this.getMargin().right
|
|
473
481
|
},
|
|
474
482
|
size: this.getTextSize(),
|
|
475
483
|
color: this.getColor(),
|
|
@@ -490,6 +498,7 @@ class PDFLibExtended {
|
|
|
490
498
|
|
|
491
499
|
let currentLine = "";
|
|
492
500
|
let currentWidth = 0;
|
|
501
|
+
let totalHeight = defaultOptions.size;
|
|
493
502
|
|
|
494
503
|
tokens.forEach((tok, i) => {
|
|
495
504
|
// Build what we would append for this token on this line
|
|
@@ -501,21 +510,23 @@ class PDFLibExtended {
|
|
|
501
510
|
|
|
502
511
|
// If this piece would overflow, draw current line and move down
|
|
503
512
|
if (currentLine && (currentWidth + pieceWidth > maxWidth)) {
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
513
|
+
this.drawText(currentLine, {
|
|
514
|
+
size: defaultOptions.size,
|
|
515
|
+
color: defaultOptions.color,
|
|
516
|
+
opacity: defaultOptions.opacity,
|
|
517
|
+
align: defaultOptions.align,
|
|
518
|
+
range: defaultOptions.range
|
|
519
|
+
});
|
|
520
|
+
let nextLineData = this.nextLine(defaultOptions.padding);
|
|
521
|
+
totalHeight += nextLineData.addedSpace;
|
|
522
|
+
|
|
523
|
+
this.getCurrentPage().moveTo(defaultOptions.range.left, this.getCurrentPage().getY());
|
|
524
|
+
currentLine = tok; // start new line with the token (no leading space)
|
|
525
|
+
currentWidth = this.getCurrentFont().widthOfTextAtSize(tok, defaultOptions.size);
|
|
515
526
|
} else {
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
527
|
+
// Safe to add to this line
|
|
528
|
+
currentLine += piece;
|
|
529
|
+
currentWidth += pieceWidth;
|
|
519
530
|
}
|
|
520
531
|
|
|
521
532
|
// Last token: flush
|
|
@@ -529,6 +540,10 @@ class PDFLibExtended {
|
|
|
529
540
|
});
|
|
530
541
|
}
|
|
531
542
|
});
|
|
543
|
+
|
|
544
|
+
return {
|
|
545
|
+
height: totalHeight
|
|
546
|
+
};
|
|
532
547
|
}
|
|
533
548
|
|
|
534
549
|
/**
|
|
@@ -556,7 +571,6 @@ class PDFLibExtended {
|
|
|
556
571
|
drawCell(text, x, y, width, options = {}){
|
|
557
572
|
let defaultOptions = {
|
|
558
573
|
height: null,
|
|
559
|
-
border: false,
|
|
560
574
|
align: "left",
|
|
561
575
|
newLine: true,
|
|
562
576
|
size: this.getTextSize(),
|
|
@@ -564,86 +578,66 @@ class PDFLibExtended {
|
|
|
564
578
|
color: this.getColor(),
|
|
565
579
|
lineThickness: 1,
|
|
566
580
|
padding: 4,
|
|
581
|
+
border: false,
|
|
582
|
+
borderColor: this.getColor(),
|
|
567
583
|
borderOpacity: 0.3,
|
|
568
584
|
...options
|
|
569
585
|
};
|
|
570
|
-
let page = this.getCurrentPage();
|
|
571
|
-
page.moveTo(x, y);
|
|
572
586
|
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
587
|
+
const page = this.getCurrentPage();
|
|
588
|
+
|
|
589
|
+
/*** DRAW TEXT ***/
|
|
590
|
+
page.moveTo(x, y);
|
|
591
|
+
let drawParagraphResponse = this.drawParagraph(text, {
|
|
592
|
+
align: defaultOptions.align,
|
|
593
|
+
range: {
|
|
594
|
+
left: x,
|
|
595
|
+
right: x + width
|
|
596
|
+
},
|
|
597
|
+
size: defaultOptions.size,
|
|
598
|
+
color: defaultOptions.color,
|
|
599
|
+
opacity: 1,
|
|
600
|
+
});
|
|
577
601
|
|
|
578
|
-
/*** BACKGROUND ***/
|
|
579
|
-
if(defaultOptions.
|
|
580
|
-
|
|
602
|
+
/*** DRAW BACKGROUND ***/
|
|
603
|
+
if(defaultOptions.border){
|
|
604
|
+
page.drawRectangle({
|
|
605
|
+
x: x,
|
|
606
|
+
y: y - drawParagraphResponse.height + defaultOptions.size,
|
|
607
|
+
width: width,
|
|
608
|
+
height: drawParagraphResponse.height,
|
|
609
|
+
color: defaultOptions.backgroundColor,
|
|
610
|
+
borderWidth: defaultOptions.lineThickness,
|
|
611
|
+
borderColor: defaultOptions.borderColor,
|
|
612
|
+
borderOpacity: defaultOptions.borderOpacity
|
|
613
|
+
});
|
|
614
|
+
}else{
|
|
581
615
|
page.drawRectangle({
|
|
582
616
|
x: x,
|
|
583
|
-
y: y -
|
|
617
|
+
y: y - drawParagraphResponse.height + defaultOptions.size,
|
|
584
618
|
width: width,
|
|
585
|
-
height: height,
|
|
586
|
-
color: defaultOptions.backgroundColor
|
|
619
|
+
height: drawParagraphResponse.height,
|
|
620
|
+
color: defaultOptions.backgroundColor,
|
|
587
621
|
});
|
|
588
622
|
}
|
|
589
623
|
|
|
590
|
-
/*** TEXT ***/
|
|
591
|
-
page.moveTo(x, y
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
page.drawLine({
|
|
603
|
-
start: {x: x - defaultOptions.padding, y: y},
|
|
604
|
-
end: {x: x + width, y: y},
|
|
605
|
-
thickness: defaultOptions.lineThickness,
|
|
606
|
-
color: defaultOptions.color,
|
|
607
|
-
opacity: defaultOptions.borderOpacity
|
|
608
|
-
});
|
|
609
|
-
}
|
|
610
|
-
// BOTTOM
|
|
611
|
-
if(defaultOptions.border === true || defaultOptions.border.includes("b") || defaultOptions.border.includes("B")){
|
|
612
|
-
page.drawLine({
|
|
613
|
-
start: {x: x - defaultOptions.padding, y: ((defaultOptions.height) ? y - defaultOptions.height : y - change - defaultOptions.padding)},
|
|
614
|
-
end: {x: x + width, y: ((defaultOptions.height) ? y - defaultOptions.height : y - change -defaultOptions.padding)},
|
|
615
|
-
thickness: defaultOptions.lineThickness,
|
|
616
|
-
color: defaultOptions.color,
|
|
617
|
-
opacity: defaultOptions.borderOpacity
|
|
618
|
-
});
|
|
619
|
-
}
|
|
620
|
-
// LEFT
|
|
621
|
-
if(defaultOptions.border === true || defaultOptions.border.includes("l") || defaultOptions.border.includes("L")){
|
|
622
|
-
page.drawLine({
|
|
623
|
-
start: {x: x - defaultOptions.padding, y: y + (defaultOptions.lineThickness / 2)},
|
|
624
|
-
end: {x: x - defaultOptions.padding, y: ((defaultOptions.height) ? y - defaultOptions.height - (defaultOptions.lineThickness / 2) : y - change - defaultOptions.padding - (defaultOptions.lineThickness / 2))},
|
|
625
|
-
thickness: defaultOptions.lineThickness,
|
|
626
|
-
color: defaultOptions.color,
|
|
627
|
-
opacity: defaultOptions.borderOpacity
|
|
628
|
-
});
|
|
629
|
-
}
|
|
630
|
-
// RIGHT
|
|
631
|
-
if(defaultOptions.border === true || defaultOptions.border.includes("r") || defaultOptions.border.includes("R")){
|
|
632
|
-
page.drawLine({
|
|
633
|
-
start: {x: x + width, y: y + (defaultOptions.lineThickness / 2)},
|
|
634
|
-
end: {x: x + width, y: ((defaultOptions.height) ? y - defaultOptions.height - (defaultOptions.lineThickness / 2) : y - change - defaultOptions.padding - (defaultOptions.lineThickness / 2))},
|
|
635
|
-
thickness: defaultOptions.lineThickness,
|
|
636
|
-
color: defaultOptions.color,
|
|
637
|
-
opacity: defaultOptions.borderOpacity
|
|
638
|
-
});
|
|
639
|
-
}
|
|
640
|
-
}
|
|
624
|
+
/*** DRAW TEXT ***/
|
|
625
|
+
page.moveTo(x, y);
|
|
626
|
+
drawParagraphResponse = this.drawParagraph(text, {
|
|
627
|
+
align: defaultOptions.align,
|
|
628
|
+
range: {
|
|
629
|
+
left: x,
|
|
630
|
+
right: x + width
|
|
631
|
+
},
|
|
632
|
+
size: defaultOptions.size,
|
|
633
|
+
color: defaultOptions.color,
|
|
634
|
+
opacity: 1,
|
|
635
|
+
});
|
|
641
636
|
|
|
642
637
|
if(defaultOptions.newLine){
|
|
643
|
-
|
|
644
|
-
page.moveTo(page.getX(), y - Number(defaultOptions.height) - change - defaultOptions.size - defaultOptions.padding - 5);
|
|
638
|
+
page.moveTo(page.getX(), y - drawParagraphResponse.height);
|
|
645
639
|
}
|
|
646
|
-
else page.moveTo(x + width
|
|
640
|
+
else page.moveTo(x + width, y);
|
|
647
641
|
}
|
|
648
642
|
|
|
649
643
|
/**
|