Skip to content

Commit c459454

Browse files
committed
new components
1 parent 2727cd3 commit c459454

File tree

6 files changed

+244
-7
lines changed

6 files changed

+244
-7
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import fs from "fs";
2+
3+
function normalizeFilePath(path) {
4+
return path.includes("tmp/")
5+
? path
6+
: `/tmp/${path}`;
7+
}
8+
9+
function checkForExtension(filename, ext = "pdf") {
10+
return filename.includes(`.${ext}`)
11+
? filename
12+
: `${filename}.${ext}`;
13+
}
14+
15+
function downloadToTmp(response, filename) {
16+
const rawcontent = response.toString("base64");
17+
const buffer = Buffer.from(rawcontent, "base64");
18+
const filePath = normalizeFilePath(filename);
19+
fs.writeFileSync(filePath, buffer);
20+
21+
return [
22+
filename,
23+
filePath,
24+
];
25+
}
26+
27+
export default {
28+
normalizeFilePath,
29+
checkForExtension,
30+
downloadToTmp,
31+
};

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: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,57 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "pdf4me",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
filePath: {
8+
type: "string",
9+
label: "File Path",
10+
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)",
11+
},
12+
filename: {
13+
type: "string",
14+
label: "File Name",
15+
description: "The filename to save the resulting PDF in the /tmp directory",
16+
},
17+
},
518
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
19+
_baseUrl() {
20+
return "https://api.pdf4me.com/api/v2";
21+
},
22+
_makeRequest({
23+
$ = this,
24+
path = "/",
25+
...otherOpts
26+
}) {
27+
return axios($, {
28+
...otherOpts,
29+
url: `${this._baseUrl()}${path}`,
30+
headers: {
31+
authorization: this.$auth.api_key,
32+
},
33+
});
34+
},
35+
convertToPdf(opts = {}) {
36+
return this._makeRequest({
37+
method: "POST",
38+
path: "/ConvertToPdf",
39+
...opts,
40+
});
41+
},
42+
mergePdfs(opts = {}) {
43+
return this._makeRequest({
44+
method: "POST",
45+
path: "/Merge",
46+
...opts,
47+
});
48+
},
49+
compressPdf(opts = {}) {
50+
return this._makeRequest({
51+
method: "POST",
52+
path: "/Optimize",
53+
...opts,
54+
});
955
},
1056
},
11-
};
57+
};

0 commit comments

Comments
 (0)