-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.js
More file actions
90 lines (80 loc) · 2.36 KB
/
Copy pathimage.js
File metadata and controls
90 lines (80 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const BaseLayoutComponent = require('./base-layout-component.js');
const { Alignment } = require('./enums/index.js');
const isUrl = require('is-url');
const request = require('sync-request');
const isXml = require('is-xml');
const SVGtoPDF = require('svg-to-pdfkit');
module.exports = class Image extends BaseLayoutComponent {
constructor(properties) {
super(properties);
this.source = properties.source;
this.stretch = properties.stretch;
}
initializeComponent(data) {
super.initializeComponent(data);
if (this.binding) {
this.source = this.getBinding(data);
}
if (isUrl(this.source)) {
const response = request('GET', this.source);
this.source = response.body;
}
}
generateComponent(document, data) {
super.generateComponent(document, data);
let horizontalAlignmentValue = '';
switch (this.horizontalAlignment) {
case Alignment.start:
horizontalAlignmentValue = 'left';
break;
case Alignment.middle:
case Alignment.fill:
horizontalAlignmentValue = 'center';
break;
case Alignment.end:
horizontalAlignmentValue = 'right';
break;
}
let verticalAlignmentValue = '';
switch (this.verticalAlignment) {
case Alignment.start:
verticalAlignmentValue = 'top';
break;
case Alignment.middle:
case Alignment.fill:
verticalAlignmentValue = 'center';
break;
case Alignment.end:
verticalAlignmentValue = 'bottom';
break;
}
if (isXml(this.source)) {
// render image as SVG
if (this.source.constructor.name === 'Buffer') {
this.source = this.source.toString('utf8');
}
SVGtoPDF(
document,
this.source,
this._originX + this.x + this.margin.left,
this._originY + this.y + this.margin.top, {
width: this.width,
height: this.height,
}
);
} else {
// render image normally
document.image(
this.source,
this._originX + this.x + this.margin.left,
this._originY + this.y + this.margin.top, {
fit: !this.stretch && [this.width, this.height],
width: this.stretch && this.width,
height: this.stretch && this.height,
align: horizontalAlignmentValue,
valign: verticalAlignmentValue,
}
);
}
}
}