pipedream icon indicating copy to clipboard operation
pipedream copied to clipboard

[ACTION] PandaDoc: Download Document

Open zalmanlew opened this issue 2 years ago • 1 comments

Download Document Required prop of document ID

Optional prop

  • watermark_text string
  • watermark_color string
  • watermark_font_size int
  • watermark_opacity float
  • separate_files bool

The API Call returns a PDF file - which could just be written to the /tmp directory.

The return value would be the path to the file.

A nice feature to add would be an optional prop to allow removing the "Certificate" page from the document (the last page of the PDF). There is a use case in just wanting the original filled document without the certificate. If the user toggled the prop to remove the certificate page it just needs to delete the last page of the PDF.


Would also be neat to have a Download Protected Document action which would work the same (slightly different API call) have the same required document ID prop and optional separate_files prop.

Please provide a link to the relevant API docs for the specific service / operation. https://developers.pandadoc.com/reference/download-document

zalmanlew avatar Aug 17 '23 19:08 zalmanlew

Here is some boilerplate code if anyone wants to give it a go.

If there were no signature fields on the document it would not include the certificate page. The first easy obvious check is to make sure there at least two pages before advancing with a deletion. It still needs a better way of confirming that the last page is in fact a signature certificate. An idea is to check if the final page contains the text Signature Certificate and Signed with PandaDoc and that.

import axios from "axios";
import fs from "fs";
import { PDFDocument } from "pdf-lib";

export default defineComponent({
  name: "Download Document",
  key: "zl-pandadoc-download-document",
  version: "0.0.1",
  type: "action",
  props: {
    pandadoc: {
      type: "app",
      app: "pandadoc",
    },
    documentId: {
      type: "string",
      label: "Document ID",
    },
    deleteCertPage: {
      type: "boolean",
      label: "Delete Certificate Page",
    },
  },
  async run({ steps, $ }) {
    const { data } = await axios({
      method: "GET",
      url: `https://api.pandadoc.com/public/v1/documents/${this.documentId}/download`,
      responseType: "arraybuffer",
      headers: {
        Authorization: `Bearer ${this.pandadoc.$auth.oauth_access_token}`,
      },
    });

    const filePath = `/tmp/${this.documentId}.pdf`;
    fs.writeFileSync(filePath, data);

    if (this.deleteCertPage) {
      const pdfDoc = await PDFDocument.load(fs.readFileSync(filePath));
      const pageCount = pdfDoc.getPageCount();

      if (pageCount > 1) {
        pdfDoc.removePage(pageCount - 1);
        fs.writeFileSync(filePath, await pdfDoc.save());
      } else {
        console.log("document only has one page, not deleting");
      }
    }

    return filePath;
  },
});

zalmanlew avatar Aug 20 '23 21:08 zalmanlew

Hello everyone, I have tested this PR and there're some test cases failed or needed improvement.

Please check the test report below for more information https://vunguyenhung.notion.site/ACTION-PandaDoc-Download-Document-d5c54828426a4b51b3a3b72f218ef6d0

vunguyenhung avatar Aug 28 '24 02:08 vunguyenhung

Hi everyone, all test cases are passed! Ready for release!

Test report https://vunguyenhung.notion.site/ACTION-PandaDoc-Download-Document-d5c54828426a4b51b3a3b72f218ef6d0

vunguyenhung avatar Aug 29 '24 02:08 vunguyenhung