-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New Components - pdforge #16722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
New Components - pdforge #16722
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| }, | ||
| }; | ||
41 changes: 41 additions & 0 deletions
41
components/pdforge/actions/generate-pdf-from-html/generate-pdf-from-html.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.`; | ||
| }, | ||
| }, | ||
| }; |
40 changes: 40 additions & 0 deletions
40
components/pdforge/actions/generate-pdf-from-template/generate-pdf-from-template.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}`; | ||
| }, | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| }), {}); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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": [ | ||
|
|
@@ -11,5 +11,10 @@ | |
| "author": "Pipedream <[email protected]> (https://pipedream.com/)", | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "dependencies": { | ||
| "@pipedream/platform": "^3.0.3", | ||
| "stream": "^0.0.3", | ||
luancazarine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "util": "^0.12.5" | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| }); | ||
| }, | ||
| }, | ||
| }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.