Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
134 changes: 134 additions & 0 deletions components/pdforge/actions/common/base.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import fs from "fs";
import stream from "stream";
import { promisify } from "util";
import {
checkTmp,
clearObj,
} from "../../common/utils.mjs";
import pdforge from "../../pdforge.app.mjs";

export default {
props: {
pdforge,
convertToImage: {
type: "boolean",
label: "Convert to Image",
description: "If true, will return a .PNG file instead of a .PDF file",
default: false,
},
asyncMode: {
type: "boolean",
label: "Async Mode",
description: "If `true`, the request will be executed in the background and the response will be sent to the webhook URL.",
default: false,
reloadProps: true,
},
saveS3: {
type: "boolean",
label: "Save to S3",
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.",
default: true,
reloadProps: true,
hidden: true,
},
s3bucket: {
propDefinition: [
pdforge,
"s3bucket",
],
hidden: true,
},
s3key: {
propDefinition: [
pdforge,
"s3key",
],
hidden: true,
},
fileName: {
propDefinition: [
pdforge,
"fileName",
],
hidden: true,
},
webhook: {
propDefinition: [
pdforge,
"webhook",
],
hidden: true,
},
},
async additionalProps(props) {
const isAsync = this.asyncMode;
const saveAtS3 = this.saveS3;

props.webhook.hidden = !isAsync;
props.saveS3.hidden = isAsync;

const showS3 = !isAsync && saveAtS3;
props.s3bucket.hidden = !showS3;
props.s3key.hidden = !showS3;
props.fileName.hidden = showS3;
return {};
},
async run({ $ }) {
let response;

const data = {
...this.getAdditionalData(),
convertToImage: this.convertToImage,
webhook: this.webhook,
};

if (this.saveS3) {
data.s3Bucked = this.s3Bucked;
data.s3Key = this.s3Key;
}

if (this.asyncMode) {
data.webhook = this.webhook;
}

const fn = this.getFunction();

const asyncResponse = await fn({
$,
asyncMode: this.asyncMode,
data: clearObj(data),
});

response = asyncResponse;

if (!this.saveS3 && !this.asyncMode) {
const fileStream = await this.pdforge._makeRequest({
baseUrl: response.signedUrl,
responseType: "stream",
removeHeader: true,
});

const filePath = checkTmp(
`${this.fileName}.${this.convertToImage
? "PNG"
: "PDF"}`,
);

const pipeline = promisify(stream.pipeline);
await pipeline(fileStream, fs.createWriteStream(filePath));

response = {
...asyncResponse,
filePath,
};
}

if (this.asyncMode) {
$.export("$summary", "Asynchronous request initiated. Await the webhook callback for completion.");
} else {
$.export("$summary", this.getSummary(this));
}

return response;
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { parseObject } from "../../common/utils.mjs";
import common from "../common/base.mjs";

export default {
...common,
key: "pdforge-generate-pdf-from-html",
name: "Generate PDF from HTML",
description: "Generate a PDF document from HTML content. [See the documentation](https://docs.pdforge.com/html-to-pdf-conversion/synchronous-request)",
version: "0.0.1",
type: "action",
props: {
...common.props,
html: {
type: "string",
label: "HTML",
description: "The HTML content you want to render",
},
pdfParams: {
type: "object",
label: "PDF Params",
description: "The object containing the parameters for your PDF. [See all the options here](https://docs.pdforge.com/options/pdf-params).",
optional: true,
},
},
methods: {
getAdditionalData() {
return {
html: this.html,
pdfParams: parseObject(this.pdfParams),
};
},
getFunction() {
return this.pdforge.generatePDFfromHTML;
},
getSummary({ convertToImage }) {
return `${convertToImage
? "PNG"
: "PDF"} successfully generated from provided HTML content.`;
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { parseObject } from "../../common/utils.mjs";
import common from "../common/base.mjs";

export default {
...common,
key: "pdforge-generate-pdf-from-template",
name: "Generate PDF from Template",
description: "Generate a document from a selected template. [See the documentation](https://docs.pdforge.com/pdfs-from-templates/synchronous-request)",
version: "0.0.1",
type: "action",
props: {
...common.props,
templateId: {
type: "string",
label: "Template ID",
description: "The ID of the template from which to generate the document",
},
data: {
type: "object",
label: "Data",
description: "The object containing the variables for your PDF template",
},
},
methods: {
getAdditionalData() {
return {
templateId: this.templateId,
data: parseObject(this.data),
};
},
getFunction() {
return this.pdforge.generateDocumentFromTemplate;
},
getSummary({ convertToImage }) {
return `${convertToImage
? "PNG"
: "PDF"} generated successfully from template ID: ${this.templateId}`;
},
},
};
48 changes: 48 additions & 0 deletions components/pdforge/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export const checkTmp = (filename) => {
if (!filename.startsWith("/tmp")) {
return `/tmp/${filename}`;
}
return filename;
};

export const parseObject = (obj) => {
if (!obj) return undefined;

if (Array.isArray(obj)) {
return obj.map((item) => {
if (typeof item === "string") {
try {
return JSON.parse(item);
} catch (e) {
return item;
}
}
return item;
});
}
if (typeof obj === "string") {
try {
return JSON.parse(obj);
} catch (e) {
return obj;
}
}
return obj;
};

export const clearObj = (obj) => {
return Object.entries(obj)
.filter(([
,
v,
]) => (v != null && v != "" && JSON.stringify(v) != "{}"))
.reduce((acc, [
k,
v,
]) => ({
...acc,
[k]: (!Array.isArray(v) && v === Object(v))
? clearObj(v)
: v,
}), {});
};
9 changes: 7 additions & 2 deletions components/pdforge/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/pdforge",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream pdforge Components",
"main": "pdforge.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,10 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.3",
"stream": "^0.0.3",
"util": "^0.12.5"
}
}
}
69 changes: 65 additions & 4 deletions components/pdforge/pdforge.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,72 @@
import { axios } from "@pipedream/platform";

export default {
type: "app",
app: "pdforge",
propDefinitions: {},
propDefinitions: {
s3bucket: {
type: "string",
label: "S3 Bucket",
description: "The ID of the active S3 connection to store the generated file on",
},
s3key: {
type: "string",
label: "S3 Key",
description: "The path and filename without extension for saving the file in the S3 bucket",
secret: true,
},
fileName: {
type: "string",
label: "File Name",
description: "The filename without extension for saving the file in the `/tmp` direcrtory",
},
webhook: {
type: "string",
label: "Webhook URL",
description: "The URL of your webhook endpoint",
},
},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_baseUrl(baseUrl) {
return baseUrl || "https://api.pdforge.com/v1";
},
_headers(removeHeader = false) {
return removeHeader
? {}
: {
Authorization: `Bearer ${this.$auth.api_key}`,
};
},
_makeRequest({
$ = this, baseUrl, removeHeader, path = "", ...opts
}) {
return axios($, {
url: this._baseUrl(baseUrl) + path,
headers: this._headers(removeHeader),
...opts,
});
},
generateDocumentFromTemplate({
asyncMode, ...opts
}) {
return this._makeRequest({
method: "POST",
path: `/pdf/${asyncMode
? "async"
: "sync"}`,
...opts,
});
},
generatePDFfromHTML({
asyncMode, ...opts
}) {
return this._makeRequest({
method: "POST",
path: `/html-to-pdf/${asyncMode
? "async"
: "sync"}`,
...opts,
});
},
},
};
20 changes: 11 additions & 9 deletions pnpm-lock.yaml

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

Loading