Skip to content

Commit 9427bf6

Browse files
committed
[Components] pdforge #16687
Actions - Generate PDF From HTML - Generate PDF From TEMPLATE
1 parent e42c819 commit 9427bf6

File tree

6 files changed

+274
-189
lines changed

6 files changed

+274
-189
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import fs from "fs";
2+
import stream from "stream";
3+
import { promisify } from "util";
4+
import {
5+
checkTmp,
6+
clearObj,
7+
} from "../../common/utils.mjs";
8+
import pdforge from "../../pdforge.app.mjs";
9+
10+
export default {
11+
props: {
12+
pdforge,
13+
convertToImage: {
14+
type: "boolean",
15+
label: "Convert to Image",
16+
description: "If true, will return a .PNG file instead of a .PDF file",
17+
default: false,
18+
},
19+
asyncMode: {
20+
type: "boolean",
21+
label: "Async Mode",
22+
description: "If `true`, the request will be executed in the background and the response will be sent to the webhook URL.",
23+
default: false,
24+
reloadProps: true,
25+
},
26+
saveS3: {
27+
type: "boolean",
28+
label: "Save to S3",
29+
description: "If `true`, the generated file will be saved to the provided s3 bucket; if `false`, the generated file will be saved to the Pipedream `/tmp` directory.",
30+
default: true,
31+
reloadProps: true,
32+
hidden: true,
33+
},
34+
s3bucket: {
35+
propDefinition: [
36+
pdforge,
37+
"s3bucket",
38+
],
39+
hidden: true,
40+
},
41+
s3key: {
42+
propDefinition: [
43+
pdforge,
44+
"s3key",
45+
],
46+
hidden: true,
47+
},
48+
fileName: {
49+
propDefinition: [
50+
pdforge,
51+
"fileName",
52+
],
53+
hidden: true,
54+
},
55+
webhook: {
56+
propDefinition: [
57+
pdforge,
58+
"webhook",
59+
],
60+
hidden: true,
61+
},
62+
},
63+
async additionalProps(props) {
64+
const isAsync = this.asyncMode;
65+
const saveAtS3 = this.saveS3;
66+
67+
props.webhook.hidden = !isAsync;
68+
props.saveS3.hidden = isAsync;
69+
70+
const showS3 = !isAsync && saveAtS3;
71+
props.s3bucket.hidden = !showS3;
72+
props.s3key.hidden = !showS3;
73+
props.fileName.hidden = showS3;
74+
return {};
75+
},
76+
async run({ $ }) {
77+
let response;
78+
79+
const data = {
80+
...this.getAdditionalData(),
81+
convertToImage: this.convertToImage,
82+
webhook: this.webhook,
83+
};
84+
85+
if (this.saveS3) {
86+
data.s3Bucked = this.s3Bucked;
87+
data.s3Key = this.s3Key;
88+
}
89+
90+
if (this.asyncMode) {
91+
data.webhook = this.webhook;
92+
}
93+
94+
const fn = this.getFunction();
95+
96+
const asyncResponse = await fn({
97+
$,
98+
asyncMode: this.asyncMode,
99+
data: clearObj(data),
100+
});
101+
102+
response = asyncResponse;
103+
104+
if (!this.saveS3 && !this.asyncMode) {
105+
const fileStream = await this.pdforge._makeRequest({
106+
baseUrl: response.signedUrl,
107+
responseType: "stream",
108+
removeHeader: true,
109+
});
110+
111+
const filePath = checkTmp(
112+
`${this.fileName}.${this.convertToImage
113+
? "PNG"
114+
: "PDF"}`,
115+
);
116+
117+
const pipeline = promisify(stream.pipeline);
118+
await pipeline(fileStream, fs.createWriteStream(filePath));
119+
120+
response = {
121+
...asyncResponse,
122+
filePath,
123+
};
124+
}
125+
126+
if (this.asyncMode) {
127+
$.export("$summary", "Asynchronous request initiated. Await the webhook callback for completion.");
128+
} else {
129+
$.export("$summary", this.getSummary(this));
130+
}
131+
132+
return response;
133+
},
134+
};
Lines changed: 25 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,41 @@
1-
import pdforge from "../../pdforge.app.mjs";
2-
import { axios } from "@pipedream/platform";
1+
import { parseObject } from "../../common/utils.mjs";
2+
import common from "../common/base.mjs";
33

44
export default {
5+
...common,
56
key: "pdforge-generate-pdf-from-html",
67
name: "Generate PDF from HTML",
7-
description: "Generate a PDF document from HTML content. [See the documentation](https://docs.pdforge.com)",
8-
version: "0.0.{{ts}}",
8+
description: "Generate a PDF document from HTML content. [See the documentation](https://docs.pdforge.com/html-to-pdf-conversion/synchronous-request)",
9+
version: "0.0.1",
910
type: "action",
1011
props: {
11-
pdforge,
12+
...common.props,
1213
html: {
13-
propDefinition: [
14-
pdforge,
15-
"html",
16-
],
14+
type: "string",
15+
label: "HTML",
16+
description: "The HTML content you want to render",
1717
},
1818
pdfParams: {
19-
propDefinition: [
20-
pdforge,
21-
"pdfParams",
22-
],
19+
type: "object",
20+
label: "PDF Params",
21+
description: "The object containing the parameters for your PDF. [See all the options here](https://docs.pdforge.com/options/pdf-params).",
2322
optional: true,
2423
},
25-
convertToImage: {
26-
propDefinition: [
27-
pdforge,
28-
"convertToImage",
29-
],
30-
optional: true,
24+
},
25+
methods: {
26+
getAdditionalData() {
27+
return {
28+
html: this.html,
29+
pdfParams: parseObject(this.pdfParams),
30+
};
3131
},
32-
s3bucket: {
33-
propDefinition: [
34-
pdforge,
35-
"s3bucket",
36-
],
37-
optional: true,
32+
getFunction() {
33+
return this.pdforge.generatePDFfromHTML;
3834
},
39-
s3key: {
40-
propDefinition: [
41-
pdforge,
42-
"s3key",
43-
],
44-
optional: true,
35+
getSummary({ convertToImage }) {
36+
return `${convertToImage
37+
? "PNG"
38+
: "PDF"} successfully generated from provided HTML content.`;
4539
},
46-
webhook: {
47-
propDefinition: [
48-
pdforge,
49-
"webhook",
50-
],
51-
optional: true,
52-
},
53-
},
54-
async run({ $ }) {
55-
const response = await this.pdforge.generatePDFfromHTML({
56-
html: this.html,
57-
pdfParams: this.pdfParams,
58-
convertToImage: this.convertToImage,
59-
s3bucket: this.s3bucket,
60-
s3key: this.s3key,
61-
webhook: this.webhook,
62-
});
63-
64-
const summary = this.webhook
65-
? `PDF generation initiated with request ID: ${response.requestId}.`
66-
: `PDF generated successfully. Download it using signed URL: ${response.signedUrl}.`;
67-
68-
$.export("$summary", summary);
69-
return response;
7040
},
7141
};
Lines changed: 24 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,40 @@
1-
import pdforge from "../../pdforge.app.mjs";
2-
import { axios } from "@pipedream/platform";
1+
import { parseObject } from "../../common/utils.mjs";
2+
import common from "../common/base.mjs";
33

44
export default {
5+
...common,
56
key: "pdforge-generate-pdf-from-template",
67
name: "Generate PDF from Template",
78
description: "Generate a document from a selected template. [See the documentation](https://docs.pdforge.com/pdfs-from-templates/synchronous-request)",
8-
version: "0.0.{{ts}}",
9+
version: "0.0.1",
910
type: "action",
1011
props: {
11-
pdforge,
12+
...common.props,
1213
templateId: {
13-
propDefinition: [
14-
pdforge,
15-
"templateId",
16-
],
14+
type: "string",
15+
label: "Template ID",
16+
description: "The ID of the template from which to generate the document",
1717
},
1818
data: {
19-
propDefinition: [
20-
pdforge,
21-
"data",
22-
],
23-
optional: true,
19+
type: "object",
20+
label: "Data",
21+
description: "The object containing the variables for your PDF template",
2422
},
25-
convertToImage: {
26-
propDefinition: [
27-
pdforge,
28-
"convertToImage",
29-
],
30-
optional: true,
31-
},
32-
s3bucket: {
33-
propDefinition: [
34-
pdforge,
35-
"s3bucket",
36-
],
37-
optional: true,
23+
},
24+
methods: {
25+
getAdditionalData() {
26+
return {
27+
templateId: this.templateId,
28+
data: parseObject(this.data),
29+
};
3830
},
39-
s3key: {
40-
propDefinition: [
41-
pdforge,
42-
"s3key",
43-
],
44-
optional: true,
31+
getFunction() {
32+
return this.pdforge.generateDocumentFromTemplate;
4533
},
46-
webhook: {
47-
propDefinition: [
48-
pdforge,
49-
"webhook",
50-
],
51-
optional: true,
34+
getSummary({ convertToImage }) {
35+
return `${convertToImage
36+
? "PNG"
37+
: "PDF"} generated successfully from template ID: ${this.templateId}`;
5238
},
5339
},
54-
async run({ $ }) {
55-
const response = await this.pdforge.generateDocumentFromTemplate({
56-
templateId: this.templateId,
57-
data: this.data,
58-
convertToImage: this.convertToImage,
59-
s3bucket: this.s3bucket,
60-
s3key: this.s3key,
61-
webhook: this.webhook,
62-
});
63-
64-
if (this.webhook) {
65-
$.export("$summary", "Asynchronous request initiated. Await the webhook callback for completion.");
66-
} else {
67-
$.export("$summary", `PDF generated successfully from template ID: ${this.templateId}`);
68-
}
69-
70-
return response;
71-
},
7240
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export const checkTmp = (filename) => {
2+
if (!filename.startsWith("/tmp")) {
3+
return `/tmp/${filename}`;
4+
}
5+
return filename;
6+
};
7+
8+
export const parseObject = (obj) => {
9+
if (!obj) return undefined;
10+
11+
if (Array.isArray(obj)) {
12+
return obj.map((item) => {
13+
if (typeof item === "string") {
14+
try {
15+
return JSON.parse(item);
16+
} catch (e) {
17+
return item;
18+
}
19+
}
20+
return item;
21+
});
22+
}
23+
if (typeof obj === "string") {
24+
try {
25+
return JSON.parse(obj);
26+
} catch (e) {
27+
return obj;
28+
}
29+
}
30+
return obj;
31+
};
32+
33+
export const clearObj = (obj) => {
34+
return Object.entries(obj)
35+
.filter(([
36+
,
37+
v,
38+
]) => (v != null && v != "" && JSON.stringify(v) != "{}"))
39+
.reduce((acc, [
40+
k,
41+
v,
42+
]) => ({
43+
...acc,
44+
[k]: (!Array.isArray(v) && v === Object(v))
45+
? clearObj(v)
46+
: v,
47+
}), {});
48+
};

components/pdforge/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/pdforge",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream pdforge Components",
55
"main": "pdforge.app.mjs",
66
"keywords": [
@@ -11,5 +11,10 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3",
17+
"stream": "^0.0.3",
18+
"util": "^0.12.5"
1419
}
1520
}

0 commit comments

Comments
 (0)