Skip to content

Commit 81fa82a

Browse files
committed
[Components] fileforge #13975
Actions - Generate PDF
1 parent 224c4ed commit 81fa82a

File tree

4 files changed

+109
-141
lines changed

4 files changed

+109
-141
lines changed
Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,85 @@
1-
import fileforge from "../../fileforge.app.mjs";
2-
import { axios } from "@pipedream/platform";
31
import FormData from "form-data";
42
import fs from "fs";
5-
import stream from "stream";
6-
import { promisify } from "util";
7-
8-
const pipeline = promisify(stream.pipeline);
3+
import {
4+
checkTmp,
5+
parseObject,
6+
} from "../../common/utils.mjs";
7+
import fileforge from "../../fileforge.app.mjs";
98

109
export default {
1110
key: "fileforge-generate-pdf",
1211
name: "Generate PDF",
1312
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}}",
13+
version: "0.0.1",
1514
type: "action",
1615
props: {
1716
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+
},
1828
files: {
19-
propDefinition: [
20-
fileforge,
21-
"files",
22-
],
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.",
2332
},
2433
test: {
25-
propDefinition: [
26-
fileforge,
27-
"test",
28-
],
29-
},
30-
host: {
31-
propDefinition: [
32-
fileforge,
33-
"host",
34-
],
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,
3538
},
36-
expiresat: {
37-
propDefinition: [
38-
fileforge,
39-
"expiresat",
40-
],
39+
expiresAt: {
40+
type: "string",
41+
label: "Expires At",
42+
description: "The expiration timestamp for the PDF in ISO 8601 format",
43+
optional: true,
4144
},
42-
filename: {
43-
propDefinition: [
44-
fileforge,
45-
"filename",
46-
],
45+
fileName: {
46+
type: "string",
47+
label: "Filename",
48+
description: "The desired filename for the generated PDF.",
49+
optional: true,
4750
},
48-
allowviewing: {
49-
propDefinition: [
50-
fileforge,
51-
"allowviewing",
52-
],
51+
allowViewing: {
52+
type: "boolean",
53+
label: "Allow Viewing",
54+
description: "Specifies whether viewing is allowed.",
55+
optional: true,
5356
},
5457
},
5558
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-
});
59+
const {
60+
fileforge,
61+
files,
62+
...data
63+
} = this;
64+
65+
const formData = new FormData();
66+
const parsedFiles = parseObject(files);
6467

65-
const pdfPath = "/tmp/generated.pdf";
66-
const pdfStream = await axios($, {
67-
method: "GET",
68-
url: response.url,
69-
responseType: "stream",
68+
for (const file of parsedFiles) {
69+
formData.append("files", fs.createReadStream(checkTmp(file)));
70+
}
71+
72+
formData.append("options", JSON.stringify(data), {
73+
contentType: "application/json",
7074
});
7175

72-
await pipeline(pdfStream, fs.createWriteStream(pdfPath));
76+
const response = await fileforge.generatePDF({
77+
$,
78+
data: formData,
79+
headers: formData.getHeaders(),
80+
});
7381

74-
$.export("$summary", `Successfully generated PDF at ${pdfPath}`);
75-
return {
76-
pdfPath,
77-
};
82+
$.export("$summary", "Successfully generated the PDF file.");
83+
return response;
7884
},
7985
};
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+
};

components/fileforge/fileforge.app.mjs

Lines changed: 14 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3,102 +3,30 @@ import { axios } from "@pipedream/platform";
33
export default {
44
type: "app",
55
app: "fileforge",
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-
},
436
methods: {
44-
authKeys() {
45-
console.log(Object.keys(this.$auth));
46-
},
477
_baseUrl() {
488
return "https://api.fileforge.com";
499
},
50-
async _makeRequest(opts = {}) {
51-
const {
52-
$ = this, method = "POST", path, headers, ...otherOpts
53-
} = opts;
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+
}) {
5419
return axios($, {
55-
...otherOpts,
56-
method,
5720
url: this._baseUrl() + path,
58-
headers: {
59-
...headers,
60-
"X-API-Key": this.$auth.api_key,
61-
"Content-Type": "multipart/form-data",
62-
},
21+
headers: this._headers(headers),
22+
...opts,
6323
});
6424
},
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-
25+
generatePDF(opts = {}) {
9626
return this._makeRequest({
27+
method: "POST",
9728
path: "/pdf/generate/",
98-
data: formData,
99-
headers: {
100-
Accept: "application/pdf",
101-
},
29+
...opts,
10230
});
10331
},
10432
},

components/fileforge/package.json

Lines changed: 4 additions & 1 deletion
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,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.1"
1417
}
1518
}

0 commit comments

Comments
 (0)