Skip to content

Commit 1121f65

Browse files
New Components - pdf4me (#16174)
* new components * pnpm-lock.yaml * fix prop * Update components/pdf4me/common/utils.mjs Co-authored-by: Jorge Cortes <[email protected]> * error handling * pnpm-lock.yaml * updates --------- Co-authored-by: Jorge Cortes <[email protected]>
1 parent 8ce75ab commit 1121f65

File tree

7 files changed

+272
-8
lines changed

7 files changed

+272
-8
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import pdf4me from "../../pdf4me.app.mjs";
2+
import utils from "../../common/utils.mjs";
3+
import fs from "fs";
4+
5+
export default {
6+
key: "pdf4me-compress-pdf",
7+
name: "Compress PDF",
8+
description: "Compress a PDF file to reduce its size. [See the documentation](https://dev.pdf4me.com/apiv2/documentation/actions/compress-pdf/)",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
pdf4me,
13+
filePath: {
14+
propDefinition: [
15+
pdf4me,
16+
"filePath",
17+
],
18+
},
19+
optimizeProfile: {
20+
type: "string",
21+
label: "Optimize Profile",
22+
description: "The type of compression",
23+
options: [
24+
"Max",
25+
"Web",
26+
"Print",
27+
"Default",
28+
"WebMax",
29+
"PrintMax",
30+
"PrintGray",
31+
"Compress",
32+
"CompressMax",
33+
],
34+
},
35+
filename: {
36+
propDefinition: [
37+
pdf4me,
38+
"filename",
39+
],
40+
},
41+
},
42+
async run({ $ }) {
43+
const filename = utils.checkForExtension(this.filename, "pdf");
44+
const filePath = utils.normalizeFilePath(this.filePath);
45+
const fileContent = fs.readFileSync(filePath, {
46+
encoding: "base64",
47+
});
48+
49+
const response = await this.pdf4me.compressPdf({
50+
$,
51+
data: {
52+
docContent: fileContent,
53+
docName: filename,
54+
optimizeProfile: this.optimizeProfile,
55+
},
56+
responseType: "arraybuffer",
57+
});
58+
59+
const filedata = utils.downloadToTmp(response, filename);
60+
61+
$.export("$summary", "Successfully compressed PDF file");
62+
return filedata;
63+
},
64+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import pdf4me from "../../pdf4me.app.mjs";
2+
import utils from "../../common/utils.mjs";
3+
import fs from "fs";
4+
5+
export default {
6+
key: "pdf4me-convert-to-pdf",
7+
name: "Convert to PDF",
8+
description: "Convert a document (e.g., DOCX, XLSX, PPTX) to PDF. [See the documentation](https://dev.pdf4me.com/apiv2/documentation/actions/convert-to-pdf/)",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
pdf4me,
13+
filePath: {
14+
propDefinition: [
15+
pdf4me,
16+
"filePath",
17+
],
18+
description: "The path to a DOCX, XLSX, or PPTX file in the `/tmp` directory. [See the documentation on working with files](https://pipedream.com/docs/code/nodejs/working-with-files/#writing-a-file-to-tmp)",
19+
},
20+
filename: {
21+
propDefinition: [
22+
pdf4me,
23+
"filename",
24+
],
25+
},
26+
},
27+
async run({ $ }) {
28+
const filePath = utils.normalizeFilePath(this.filePath);
29+
const fileContent = fs.readFileSync(filePath, {
30+
encoding: "base64",
31+
});
32+
33+
const response = await this.pdf4me.convertToPdf({
34+
$,
35+
data: {
36+
docContent: fileContent,
37+
docName: this.filename,
38+
},
39+
responseType: "arraybuffer",
40+
});
41+
42+
const filedata = utils.downloadToTmp(response, this.filename);
43+
44+
$.export("$summary", "Successfully converted file to PDF");
45+
return filedata;
46+
},
47+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import pdf4me from "../../pdf4me.app.mjs";
2+
import utils from "../../common/utils.mjs";
3+
import fs from "fs";
4+
5+
export default {
6+
key: "pdf4me-merge-pdfs",
7+
name: "Merge PDF Files",
8+
description: "Merge multiple PDF files into a single PDF. [See the documentation](https://dev.pdf4me.com/apiv2/documentation/actions/merge/)",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
pdf4me,
13+
filePaths: {
14+
type: "string[]",
15+
label: "File Paths",
16+
description: "An array of paths to a PDF files in the `/tmp` directory. [See the documentation on working with files](https://pipedream.com/docs/code/nodejs/working-with-files/#writing-a-file-to-tmp)",
17+
},
18+
filename: {
19+
propDefinition: [
20+
pdf4me,
21+
"filename",
22+
],
23+
},
24+
},
25+
async run({ $ }) {
26+
const filename = utils.checkForExtension(this.filename, "pdf");
27+
const filePaths = this.filePaths.map((path) => utils.normalizeFilePath(path));
28+
const fileContents = filePaths.map((path) => fs.readFileSync(path, {
29+
encoding: "base64",
30+
}));
31+
32+
const response = await this.pdf4me.mergePdfs({
33+
$,
34+
data: {
35+
docContent: fileContents,
36+
docName: filename,
37+
},
38+
responseType: "arraybuffer",
39+
});
40+
41+
const filedata = utils.downloadToTmp(response, filename);
42+
43+
$.export("$summary", "Successfully merged PDF files");
44+
return filedata;
45+
},
46+
};

components/pdf4me/common/utils.mjs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import fs from "fs";
2+
import { ConfigurationError } from "@pipedream/platform";
3+
4+
function normalizeFilePath(path) {
5+
return path.startsWith("/tmp/")
6+
? path
7+
: `/tmp/${path}`;
8+
}
9+
10+
function checkForExtension(filename, ext = "pdf") {
11+
return filename.endsWith(`.${ext}`)
12+
? filename
13+
: `${filename}.${ext}`;
14+
}
15+
16+
function downloadToTmp(response, filename) {
17+
const rawcontent = response.toString("base64");
18+
const buffer = Buffer.from(rawcontent, "base64");
19+
const filePath = normalizeFilePath(filename);
20+
fs.writeFileSync(filePath, buffer);
21+
22+
return [
23+
filename,
24+
filePath,
25+
];
26+
}
27+
28+
function handleErrorMessage(error) {
29+
if (!error) {
30+
throw new ConfigurationError("Unknown error occurred");
31+
}
32+
let errorMessage = error.name;
33+
if (error.response?.data) {
34+
const text = Buffer.from(error.response.data).toString("utf-8");
35+
try {
36+
errorMessage = JSON.stringify(JSON.parse(text));
37+
} catch (parseErr) {
38+
errorMessage = text;
39+
}
40+
}
41+
throw new ConfigurationError(errorMessage);
42+
}
43+
44+
export default {
45+
normalizeFilePath,
46+
checkForExtension,
47+
downloadToTmp,
48+
handleErrorMessage,
49+
};

components/pdf4me/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/pdf4me",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream PDF4me Components",
55
"main": "pdf4me.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.3"
1417
}
15-
}
18+
}

components/pdf4me/pdf4me.app.mjs

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,62 @@
1+
import { axios } from "@pipedream/platform";
2+
import utils from "./common/utils.mjs";
3+
14
export default {
25
type: "app",
36
app: "pdf4me",
4-
propDefinitions: {},
7+
propDefinitions: {
8+
filePath: {
9+
type: "string",
10+
label: "File Path",
11+
description: "The path to a PDF file in the `/tmp` directory. [See the documentation on working with files](https://pipedream.com/docs/code/nodejs/working-with-files/#writing-a-file-to-tmp)",
12+
},
13+
filename: {
14+
type: "string",
15+
label: "File Name",
16+
description: "The filename to save the resulting PDF in the /tmp directory",
17+
},
18+
},
519
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
20+
_baseUrl() {
21+
return "https://api.pdf4me.com/api/v2";
22+
},
23+
async _makeRequest({
24+
$ = this,
25+
path = "/",
26+
...otherOpts
27+
}) {
28+
try {
29+
return await axios($, {
30+
...otherOpts,
31+
url: `${this._baseUrl()}${path}`,
32+
headers: {
33+
authorization: this.$auth.api_key,
34+
},
35+
});
36+
} catch (error) {
37+
utils.handleErrorMessage(error);
38+
}
39+
},
40+
convertToPdf(opts = {}) {
41+
return this._makeRequest({
42+
method: "POST",
43+
path: "/ConvertToPdf",
44+
...opts,
45+
});
46+
},
47+
mergePdfs(opts = {}) {
48+
return this._makeRequest({
49+
method: "POST",
50+
path: "/Merge",
51+
...opts,
52+
});
53+
},
54+
compressPdf(opts = {}) {
55+
return this._makeRequest({
56+
method: "POST",
57+
path: "/Optimize",
58+
...opts,
59+
});
960
},
1061
},
11-
};
62+
};

pnpm-lock.yaml

Lines changed: 5 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)