Skip to content

Commit d83f8ee

Browse files
authored
New Components - fileforge (#13989)
* fileforge init * [Components] fileforge #13975 Actions - Generate PDF * pnpm update * some adjusts * add form-data * pnpm update
1 parent 004872e commit d83f8ee

File tree

5 files changed

+158
-8
lines changed

5 files changed

+158
-8
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import FormData from "form-data";
2+
import fs from "fs";
3+
import {
4+
checkTmp,
5+
parseObject,
6+
} from "../../common/utils.mjs";
7+
import fileforge from "../../fileforge.app.mjs";
8+
9+
export default {
10+
key: "fileforge-generate-pdf",
11+
name: "Generate PDF",
12+
description: "Generate a PDF from provided HTML. [See the documentation](https://docs.fileforge.com/api-reference/api-reference/pdf/generate)",
13+
version: "0.0.1",
14+
type: "action",
15+
props: {
16+
fileforge,
17+
alert: {
18+
type: "alert",
19+
alertType: "warning",
20+
content: `An **\`index.html\`** file is required, and will be used as the main document.
21+
Other documents may also be attached, such as stylesheets or images.
22+
The path in the **\`filename\`** part of the multipart attachement will be respected during generation.
23+
**Important notice:** during generation, the **\`index.html\`** file will be processed to include the base URL of the document.
24+
This is required for assets to be loaded correctly.
25+
To link your assets from the HTML file, you should not use a leading slash in the URL.
26+
For example, use **\`<img src="image.jpg" />\`** instead of **\`<img src="/image.jpg" />\`**.`,
27+
},
28+
files: {
29+
type: "string[]",
30+
label: "HTML Files",
31+
description: "The HTML files to convert to PDF. Each file should be a valid path to an HTML file saved to the `/tmp` directory (e.g. `/tmp/image.png`). [See the documentation](https://pipedream.com/docs/workflows/steps/code/nodejs/working-with-files/#the-tmp-directory)..",
32+
},
33+
test: {
34+
type: "boolean",
35+
label: "Test",
36+
description: "Generate a test document instead of a production document. The generated document will contain a watermark. Defaults to true.",
37+
optional: true,
38+
},
39+
expiresAt: {
40+
type: "string",
41+
label: "Expires At",
42+
description: "The expiration timestamp for the PDF in ISO 8601 format",
43+
optional: true,
44+
},
45+
fileName: {
46+
type: "string",
47+
label: "Filename",
48+
description: "The desired filename for the generated PDF.",
49+
optional: true,
50+
},
51+
allowViewing: {
52+
type: "boolean",
53+
label: "Allow Viewing",
54+
description: "Specifies whether viewing is allowed.",
55+
optional: true,
56+
},
57+
},
58+
async run({ $ }) {
59+
const {
60+
fileforge,
61+
files,
62+
...data
63+
} = this;
64+
65+
const formData = new FormData();
66+
const parsedFiles = parseObject(files);
67+
68+
for (const file of parsedFiles) {
69+
formData.append("files", fs.createReadStream(checkTmp(file)));
70+
}
71+
72+
formData.append("options", JSON.stringify({
73+
...data,
74+
host: true,
75+
}), {
76+
contentType: "application/json",
77+
});
78+
79+
const response = await fileforge.generatePDF({
80+
$,
81+
data: formData,
82+
headers: formData.getHeaders(),
83+
});
84+
85+
$.export("$summary", "Successfully generated the PDF file.");
86+
return response;
87+
},
88+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export const parseObject = (obj) => {
2+
if (!obj) return undefined;
3+
4+
if (Array.isArray(obj)) {
5+
return obj.map((item) => {
6+
if (typeof item === "string") {
7+
try {
8+
return JSON.parse(item);
9+
} catch (e) {
10+
return item;
11+
}
12+
}
13+
return item;
14+
});
15+
}
16+
if (typeof obj === "string") {
17+
try {
18+
return JSON.parse(obj);
19+
} catch (e) {
20+
return obj;
21+
}
22+
}
23+
return obj;
24+
};
25+
26+
export const checkTmp = (filename) => {
27+
if (!filename.startsWith("/tmp")) {
28+
return `/tmp/${filename}`;
29+
}
30+
return filename;
31+
};
Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "fileforge",
4-
propDefinitions: {},
56
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
7+
_baseUrl() {
8+
return "https://api.fileforge.com";
9+
},
10+
_headers(headers = {}) {
11+
return {
12+
"X-API-Key": this.$auth.api_key,
13+
...headers,
14+
};
15+
},
16+
_makeRequest({
17+
$ = this, path, headers, ...opts
18+
}) {
19+
return axios($, {
20+
url: this._baseUrl() + path,
21+
headers: this._headers(headers),
22+
...opts,
23+
});
24+
},
25+
generatePDF(opts = {}) {
26+
return this._makeRequest({
27+
method: "POST",
28+
path: "/pdf/generate/",
29+
...opts,
30+
});
931
},
1032
},
11-
};
33+
};

components/fileforge/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/fileforge",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Fileforge Components",
55
"main": "fileforge.app.mjs",
66
"keywords": [
@@ -11,5 +11,9 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.1",
17+
"form-data": "^4.0.0"
1418
}
15-
}
19+
}

pnpm-lock.yaml

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)