Homepage

Generating and Uploading a PDF

Last edit: Oct 26, 2022

This guide will help you generate and upload a PDF file.

Requirements

This is an advanced tutorial. To follow it, you should be familiar with Records, Tables, and uploads.

Steps

Step 1: Create Table

Start with a Table like this:

app/schema/model_with_upload.yml
name: model_with_upload
properties:
- name: the_upload
  type: upload

Step 2: Generate PDF and upload it to S3

Once you have the Table defined in step 1, you can generate a PDF and upload it to S3 from an HTML source by passing the HTML code in "content".
expires_in sets an expiration timeout for the download link, in seconds.

app/graphql/generate_pdf.graphql
mutation {
  record_create(
    record: {
      table: "model_with_upload"
      properties: [ { 
        name: "the_upload"
        value_upload: {
          type: pdf
          content: "<html><head><title>Test PDF</title></head><body><h1>This will be the content of the pdf</h1></body></html>"
        }
      } ]
    }
  ) {
    id
    url: property_upload(name:"the_upload", expires_in: 600) {
      extension
      file_name
      url
      versions
    }
  }
}

Step 3: Access PDF

If successful, the mutation will return something similar to this:

  "data": {
    "record_create": {
      "id": "118",
      "url": {
        "extension": "pdf",
        "file_name": "the_upload.pdf",
        "url": "https://s3-us-west-2.amazonaws.com/uploads.staging.oregon.platform-os.com/instances/xx/property_uploads/custom_uploads/file/customization/xxxx-xxxxxx/the_upload.pdf?xxxxx",
        "versions": {}
      }
    }
  }
}

The link in "url" will be available for the number of seconds previously specified in expires_in. You may still get the S3 link after the expiration time, by using the following query, but don't forget to set a new, non-zero expiration time, otherwise the file will not be accessible:

app/graphql/get_pdf_requests.graphql
query {
  records(
    per_page: 3
    filter: {
      id: { value: 57 } # use this if the id is known
      table: { value: "model_with_upload" }
    }
  ) {
    results {
      id
      url: property_upload(name:"the_upload", expires_in: 600) {
        extension
        file_name
        url
        versions
      }
    }
  }
}

Tip

To change the PDF's orientation, set the page size in your css to landscape: @page { size: landscape; }

Source code

The complete source code can be found on GitHub.

Questions?

We are always happy to help with any questions you may have.

contact us