Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions components/pdf4me/actions/compress-pdf/compress-pdf.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import pdf4me from "../../pdf4me.app.mjs";
import utils from "../../common/utils.mjs";
import fs from "fs";

export default {
key: "pdf4me-compress-pdf",
name: "Compress PDF",
description: "Compress a PDF file to reduce its size. [See the documentation](https://dev.pdf4me.com/apiv2/documentation/actions/compress-pdf/)",
version: "0.0.1",
type: "action",
props: {
pdf4me,
filePath: {
propDefinition: [
pdf4me,
"filePath",
],
},
optimizeProfile: {
type: "string",
label: "Optimize Profile",
description: "The type of compression",
options: [
"Max",
"Web",
"Print",
"Default",
"WebMax",
"PrintMax",
"PrintGray",
"Compress",
"CompressMax",
],
},
filename: {
propDefinition: [
pdf4me,
"filename",
],
},
},
async run({ $ }) {
const filename = utils.checkForExtension(this.filename, "pdf");
const filePath = utils.normalizeFilePath(this.filePath);
const fileContent = fs.readFileSync(filePath, {
encoding: "base64",
});

const response = await this.pdf4me.compressPdf({
$,
data: {
docContent: fileContent,
docName: filename,
optimizeProfile: this.optimizeProfile,
},
responseType: "arraybuffer",
});

const filedata = utils.downloadToTmp(response, filename);

$.export("$summary", "Successfully compressed PDF file");
return filedata;
},
};
47 changes: 47 additions & 0 deletions components/pdf4me/actions/convert-to-pdf/convert-to-pdf.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import pdf4me from "../../pdf4me.app.mjs";
import utils from "../../common/utils.mjs";
import fs from "fs";

export default {
key: "pdf4me-convert-to-pdf",
name: "Convert to PDF",
description: "Convert a document (e.g., DOCX, XLSX, PPTX) to PDF. [See the documentation](https://dev.pdf4me.com/apiv2/documentation/actions/convert-to-pdf/)",
version: "0.0.1",
type: "action",
props: {
pdf4me,
filePath: {
propDefinition: [
pdf4me,
"filePath",
],
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)",
},
filename: {
propDefinition: [
pdf4me,
"filename",
],
},
},
async run({ $ }) {
const filePath = utils.normalizeFilePath(this.filePath);
const fileContent = fs.readFileSync(filePath, {
encoding: "base64",
});

const response = await this.pdf4me.convertToPdf({
$,
data: {
docContent: fileContent,
docName: this.filename,
},
responseType: "arraybuffer",
});

const filedata = utils.downloadToTmp(response, this.filename);

$.export("$summary", "Successfully converted file to PDF");
return filedata;
},
};
46 changes: 46 additions & 0 deletions components/pdf4me/actions/merge-pdfs/merge-pdfs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pdf4me from "../../pdf4me.app.mjs";
import utils from "../../common/utils.mjs";
import fs from "fs";

export default {
key: "pdf4me-merge-pdfs",
name: "Merge PDF Files",
description: "Merge multiple PDF files into a single PDF. [See the documentation](https://dev.pdf4me.com/apiv2/documentation/actions/merge/)",
version: "0.0.1",
type: "action",
props: {
pdf4me,
filePaths: {
type: "string[]",
label: "File Paths",
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)",
},
filename: {
propDefinition: [
pdf4me,
"filename",
],
},
},
async run({ $ }) {
const filename = utils.checkForExtension(this.filename, "pdf");
const filePaths = this.filePaths.map((path) => utils.normalizeFilePath(path));
const fileContents = filePaths.map((path) => fs.readFileSync(path, {
encoding: "base64",
}));

const response = await this.pdf4me.mergePdfs({
$,
data: {
docContent: fileContents,
docName: filename,
},
responseType: "arraybuffer",
});

const filedata = utils.downloadToTmp(response, filename);

$.export("$summary", "Successfully merged PDF files");
return filedata;
},
};
46 changes: 46 additions & 0 deletions components/pdf4me/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import fs from "fs";
import { ConfigurationError } from "@pipedream/platform";

function normalizeFilePath(path) {
return path.startsWith("/tmp/")
? path
: `/tmp/${path}`;
}

function checkForExtension(filename, ext = "pdf") {
return filename.includes(`.${ext}`)
? filename
: `${filename}.${ext}`;
}

function downloadToTmp(response, filename) {
const rawcontent = response.toString("base64");
const buffer = Buffer.from(rawcontent, "base64");
const filePath = normalizeFilePath(filename);
fs.writeFileSync(filePath, buffer);

return [
filename,
filePath,
];
}

function handleErrorMessage(error) {
let errorMessage = error.name;
if (error.response && error.response.data) {
const text = Buffer.from(error.response.data).toString("utf-8");
try {
errorMessage = JSON.stringify(JSON.parse(text));
} catch (parseErr) {
errorMessage = text;
}
}
throw new ConfigurationError(`${errorMessage}`);
}

export default {
normalizeFilePath,
checkForExtension,
downloadToTmp,
handleErrorMessage,
};
7 changes: 5 additions & 2 deletions components/pdf4me/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/pdf4me",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream PDF4me Components",
"main": "pdf4me.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,8 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.3"
}
}
}
61 changes: 56 additions & 5 deletions components/pdf4me/pdf4me.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,62 @@
import { axios } from "@pipedream/platform";
import utils from "./common/utils.mjs";

export default {
type: "app",
app: "pdf4me",
propDefinitions: {},
propDefinitions: {
filePath: {
type: "string",
label: "File Path",
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)",
},
filename: {
type: "string",
label: "File Name",
description: "The filename to save the resulting PDF in the /tmp directory",
},
},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_baseUrl() {
return "https://api.pdf4me.com/api/v2";
},
async _makeRequest({
$ = this,
path = "/",
...otherOpts
}) {
try {
return await axios($, {
...otherOpts,
url: `${this._baseUrl()}${path}`,
headers: {
authorization: this.$auth.api_key,
},
});
} catch (error) {
utils.handleErrorMessage(error);
}
},
convertToPdf(opts = {}) {
return this._makeRequest({
method: "POST",
path: "/ConvertToPdf",
...opts,
});
},
mergePdfs(opts = {}) {
return this._makeRequest({
method: "POST",
path: "/Merge",
...opts,
});
},
compressPdf(opts = {}) {
return this._makeRequest({
method: "POST",
path: "/Optimize",
...opts,
});
},
},
};
};
16 changes: 10 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading