-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdfEditor_v1_loadPdf.js
More file actions
234 lines (210 loc) · 5.95 KB
/
pdfEditor_v1_loadPdf.js
File metadata and controls
234 lines (210 loc) · 5.95 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
const { PDFDocument, StandardFonts, rgb, degrees, PageSizes } = PDFLib;
var globalPdfDoc;
// - - - - - - - - - - - - - - - - - - - - -
const pdfEditor_load = (encodedFile) => {
// - - - - -
async function loadPdf(pdfFile) {
try {
const pdfDoc = await PDFDocument.load(pdfFile);
const pdfDataUri = await pdfDoc.saveAsBase64({ dataUri: true });
console.log("loadPdf function successfully saved as Base64 encoded");
return pdfDataUri;
//
} catch (error) {
console.log("loadPdf try failed, catch: ", error);
FileMaker.PerformScriptWithOption(
"FMP_Console",
"loadPdf try failed, catch: " + error,
5
);
}
}
// - - - - -
loadPdf(encodedFile)
.then((returnedDoc) => {
console.log("loadPdf function successfully completed");
globalPdfDoc = returnedDoc;
FileMaker.PerformScriptWithOption(
"1.32_Load-Add_results",
returnedDoc,
5
);
})
.catch((err) => {
console.log("loadPdf ERROR, err: ", err);
FileMaker.PerformScriptWithOption(
"FMP_Console",
"loadPdf ERROR, err: " + err,
5
);
});
};
// - - - - - - - - - - - - - - - - - - - - -
const pdfEditor_getSize = () => {
// - - - - -
async function getSize(theDoc) {
try {
const pdfDoc = await PDFDocument.load(theDoc);
const totalPages = pdfDoc.getPageCount();
const pages = pdfDoc.getPages();
const lastPage = pages[totalPages - 1]; // The last page of the document
const { width, height } = lastPage.getSize();
const jsonDimensions = {
w: width,
h: height,
};
console.log("getSize successful");
return jsonDimensions;
//
} catch (error) {
console.log("getSize try failed, catch: ", error);
FileMaker.PerformScriptWithOption(
"FMP_Console",
"getSize try failed, catch: " + error,
5
);
}
}
// - - - - -
const pdfDoc = globalPdfDoc;
getSize(pdfDoc)
.then((dimensions) => {
console.log("getSize function successfully completed");
FileMaker.PerformScriptWithOption(
"1.33_GetSize_results",
JSON.stringify(dimensions),
5
);
})
.catch((err) => {
console.log("getSize ERROR, err: ", err);
FileMaker.PerformScriptWithOption(
"FMP_Console",
"getSize ERROR, err: " + err,
5
);
});
};
// - - - - - - - - - - - - - - - - - - - - - - - - - -
const pdfEditor_addPage = (pageSize) => {
// - - - - -
async function addPage(theParams) {
try {
console.log("addPage, size: ", theParams.pageSize);
const pdfDoc = await PDFDocument.load(theParams.pdfDoc);
let whichSize = "";
switch (theParams.pageSize) {
case "A7":
whichSize = PageSizes.A7;
break;
case "Legal":
whichSize = PageSizes.Legal;
break;
default:
whichSize = PageSizes.Letter;
break;
}
const newPage = pdfDoc.addPage(whichSize);
const pdfDataUri = await pdfDoc.saveAsBase64({ dataUri: true });
console.log("addPage function successfully saved as Base64 encoded");
return pdfDataUri;
//
} catch (err) {
console.log("addPage try failed, catch: ", err);
FileMaker.PerformScriptWithOption(
"FMP_Console",
"addPage try failed, catch: " + err,
5
);
}
}
// - - - - -
const jsonParameters = {
pdfDoc: globalPdfDoc,
pageSize: pageSize,
};
addPage(jsonParameters)
.then((returnedDoc) => {
console.log("addPage function successfully completed");
globalPdfDoc = returnedDoc;
FileMaker.PerformScriptWithOption(
"1.32_Load-Add_results",
returnedDoc,
5
);
})
.catch((err) => {
console.log("addPage ERROR, err: ", err);
FileMaker.PerformScriptWithOption(
"FMP_Console",
"addPage ERROR, err: " + err,
5
);
});
};
// - - - - - - - - - - - - - - - - - - - - -
const pdfEditor_addText = (theText, whereX, whereY, rotation) => {
// - - - - -
async function modifyPdf(theDoc, theText, whereX, whereY, rotate) {
try {
console.log(
"modifyPdf, text: ",
theText,
", x: ",
whereX,
", y: ",
whereY
);
const pdfDoc = await PDFDocument.load(theDoc);
const totalPages = pdfDoc.getPageCount();
const pages = pdfDoc.getPages();
const lastPage = pages[totalPages - 1]; // The last page of the document
// const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman);
const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);
lastPage.drawText(theText, {
x: whereX,
y: whereY,
size: 12,
font: helveticaFont,
color: rgb(0, 0.53, 0.71),
maxWidth: 575,
lineHeight: 16,
rotate: degrees(rotate),
});
const pdfDataUri = await pdfDoc.saveAsBase64({ dataUri: true });
console.log("modifyPdf function successfully saved as Base64 encoded");
return pdfDataUri;
//
} catch (error) {
console.log("modifyPdf try failed, catch: ", error);
FileMaker.PerformScriptWithOption(
"FMP_Console",
"modifyPdf try failed, catch: " + error,
5
);
}
}
// - - - - -
const pdfDoc = globalPdfDoc;
const posX = parseInt(whereX);
const posY = parseInt(whereY);
const rotate = parseInt(rotation);
modifyPdf(pdfDoc, theText, posX, posY, rotate)
.then((returnedDoc) => {
console.log("modifyPdf function successfully completed");
globalPdfDoc = returnedDoc;
FileMaker.PerformScriptWithOption(
"1.32_Load-Add_results",
returnedDoc,
5
);
})
.catch((err) => {
console.log("modifyPdf ERROR, err: ", err);
FileMaker.PerformScriptWithOption(
"FMP_Console",
"modifyPdf ERROR, err: " + err,
5
);
});
};