|
| 1 | +import FormData from "form-data"; |
| 2 | +import fs from "fs"; |
| 3 | +import { |
| 4 | + checkTmp, |
| 5 | + parseObject, |
| 6 | +} from "../../common/utils.mjs"; |
| 7 | +import fileforge from "../../fileforge.app.mjs"; |
| 8 | + |
| 9 | +export default { |
| 10 | + key: "fileforge-generate-pdf", |
| 11 | + name: "Generate PDF", |
| 12 | + description: "Generate a PDF from provided HTML. [See the documentation](https://docs.fileforge.com/api-reference/api-reference/pdf/generate)", |
| 13 | + version: "0.0.1", |
| 14 | + type: "action", |
| 15 | + props: { |
| 16 | + fileforge, |
| 17 | + alert: { |
| 18 | + type: "alert", |
| 19 | + alertType: "warning", |
| 20 | + content: `An **\`index.html\`** file is required, and will be used as the main document. |
| 21 | + Other documents may also be attached, such as stylesheets or images. |
| 22 | + The path in the **\`filename\`** part of the multipart attachement will be respected during generation. |
| 23 | + **Important notice:** during generation, the **\`index.html\`** file will be processed to include the base URL of the document. |
| 24 | + This is required for assets to be loaded correctly. |
| 25 | + To link your assets from the HTML file, you should not use a leading slash in the URL. |
| 26 | + For example, use **\`<img src="image.jpg" />\`** instead of **\`<img src="/image.jpg" />\`**.`, |
| 27 | + }, |
| 28 | + files: { |
| 29 | + type: "string[]", |
| 30 | + label: "HTML Files", |
| 31 | + description: "The HTML files to convert to PDF. Each file should be a valid path to an HTML file saved to the `/tmp` directory (e.g. `/tmp/image.png`). [See the documentation](https://pipedream.com/docs/workflows/steps/code/nodejs/working-with-files/#the-tmp-directory)..", |
| 32 | + }, |
| 33 | + test: { |
| 34 | + type: "boolean", |
| 35 | + label: "Test", |
| 36 | + description: "Generate a test document instead of a production document. The generated document will contain a watermark. Defaults to true.", |
| 37 | + optional: true, |
| 38 | + }, |
| 39 | + expiresAt: { |
| 40 | + type: "string", |
| 41 | + label: "Expires At", |
| 42 | + description: "The expiration timestamp for the PDF in ISO 8601 format", |
| 43 | + optional: true, |
| 44 | + }, |
| 45 | + fileName: { |
| 46 | + type: "string", |
| 47 | + label: "Filename", |
| 48 | + description: "The desired filename for the generated PDF.", |
| 49 | + optional: true, |
| 50 | + }, |
| 51 | + allowViewing: { |
| 52 | + type: "boolean", |
| 53 | + label: "Allow Viewing", |
| 54 | + description: "Specifies whether viewing is allowed.", |
| 55 | + optional: true, |
| 56 | + }, |
| 57 | + }, |
| 58 | + async run({ $ }) { |
| 59 | + const { |
| 60 | + fileforge, |
| 61 | + files, |
| 62 | + ...data |
| 63 | + } = this; |
| 64 | + |
| 65 | + const formData = new FormData(); |
| 66 | + const parsedFiles = parseObject(files); |
| 67 | + |
| 68 | + for (const file of parsedFiles) { |
| 69 | + formData.append("files", fs.createReadStream(checkTmp(file))); |
| 70 | + } |
| 71 | + |
| 72 | + formData.append("options", JSON.stringify({ |
| 73 | + ...data, |
| 74 | + host: true, |
| 75 | + }), { |
| 76 | + contentType: "application/json", |
| 77 | + }); |
| 78 | + |
| 79 | + const response = await fileforge.generatePDF({ |
| 80 | + $, |
| 81 | + data: formData, |
| 82 | + headers: formData.getHeaders(), |
| 83 | + }); |
| 84 | + |
| 85 | + $.export("$summary", "Successfully generated the PDF file."); |
| 86 | + return response; |
| 87 | + }, |
| 88 | +}; |
0 commit comments