Skip to content

Commit 224c4ed

Browse files
committed
fileforge init
1 parent db59016 commit 224c4ed

File tree

3 files changed

+177
-4
lines changed

3 files changed

+177
-4
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import fileforge from "../../fileforge.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
import FormData from "form-data";
4+
import fs from "fs";
5+
import stream from "stream";
6+
import { promisify } from "util";
7+
8+
const pipeline = promisify(stream.pipeline);
9+
10+
export default {
11+
key: "fileforge-generate-pdf",
12+
name: "Generate PDF",
13+
description: "Generate a PDF from provided HTML. [See the documentation](https://docs.fileforge.com/api-reference/api-reference/pdf/generate)",
14+
version: "0.0.{{ts}}",
15+
type: "action",
16+
props: {
17+
fileforge,
18+
files: {
19+
propDefinition: [
20+
fileforge,
21+
"files",
22+
],
23+
},
24+
test: {
25+
propDefinition: [
26+
fileforge,
27+
"test",
28+
],
29+
},
30+
host: {
31+
propDefinition: [
32+
fileforge,
33+
"host",
34+
],
35+
},
36+
expiresat: {
37+
propDefinition: [
38+
fileforge,
39+
"expiresat",
40+
],
41+
},
42+
filename: {
43+
propDefinition: [
44+
fileforge,
45+
"filename",
46+
],
47+
},
48+
allowviewing: {
49+
propDefinition: [
50+
fileforge,
51+
"allowviewing",
52+
],
53+
},
54+
},
55+
async run({ $ }) {
56+
const response = await this.fileforge.generatePDF({
57+
files: this.files,
58+
test: this.test,
59+
host: this.host,
60+
expiresat: this.expiresat,
61+
filename: this.filename,
62+
allowviewing: this.allowviewing,
63+
});
64+
65+
const pdfPath = "/tmp/generated.pdf";
66+
const pdfStream = await axios($, {
67+
method: "GET",
68+
url: response.url,
69+
responseType: "stream",
70+
});
71+
72+
await pipeline(pdfStream, fs.createWriteStream(pdfPath));
73+
74+
$.export("$summary", `Successfully generated PDF at ${pdfPath}`);
75+
return {
76+
pdfPath,
77+
};
78+
},
79+
};
Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,105 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "fileforge",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
files: {
8+
type: "string[]",
9+
label: "HTML Files",
10+
description: "The HTML files to convert to PDF. Each file should be a valid URL or path to an HTML file.",
11+
},
12+
test: {
13+
type: "boolean",
14+
label: "Test",
15+
description: "Enable test mode",
16+
optional: true,
17+
},
18+
host: {
19+
type: "string",
20+
label: "Host",
21+
description: "Specifies the host for the PDF generation.",
22+
optional: true,
23+
},
24+
expiresat: {
25+
type: "string",
26+
label: "Expires At",
27+
description: "The expiration timestamp for the PDF in ISO 8601 format",
28+
optional: true,
29+
},
30+
filename: {
31+
type: "string",
32+
label: "Filename",
33+
description: "The desired filename for the generated PDF.",
34+
optional: true,
35+
},
36+
allowviewing: {
37+
type: "boolean",
38+
label: "Allow Viewing",
39+
description: "Specifies whether viewing is allowed.",
40+
optional: true,
41+
},
42+
},
543
methods: {
6-
// this.$auth contains connected account data
744
authKeys() {
845
console.log(Object.keys(this.$auth));
946
},
47+
_baseUrl() {
48+
return "https://api.fileforge.com";
49+
},
50+
async _makeRequest(opts = {}) {
51+
const {
52+
$ = this, method = "POST", path, headers, ...otherOpts
53+
} = opts;
54+
return axios($, {
55+
...otherOpts,
56+
method,
57+
url: this._baseUrl() + path,
58+
headers: {
59+
...headers,
60+
"X-API-Key": this.$auth.api_key,
61+
"Content-Type": "multipart/form-data",
62+
},
63+
});
64+
},
65+
async generatePDF({
66+
files, test, host, expiresat, filename, allowviewing,
67+
}) {
68+
const formData = new FormData();
69+
70+
// Append files
71+
files.forEach((file, index) => {
72+
formData.append("files", file, `file${index}.html`);
73+
});
74+
75+
// Construct options object
76+
const options = {
77+
...(test !== undefined && {
78+
test,
79+
}),
80+
...(host && {
81+
host,
82+
}),
83+
...(expiresat && {
84+
expiresat,
85+
}),
86+
...(filename && {
87+
filename,
88+
}),
89+
...(allowviewing !== undefined && {
90+
allowviewing,
91+
}),
92+
};
93+
94+
formData.append("options", JSON.stringify(options));
95+
96+
return this._makeRequest({
97+
path: "/pdf/generate/",
98+
data: formData,
99+
headers: {
100+
Accept: "application/pdf",
101+
},
102+
});
103+
},
10104
},
11-
};
105+
};

components/fileforge/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
"publishConfig": {
1313
"access": "public"
1414
}
15-
}
15+
}

0 commit comments

Comments
 (0)