Skip to main content
Question

How do I create a PDF with Code and Attach It in a Later Step?

  • September 16, 2024
  • 1 reply
  • 57 views

Hi everyone,

I'm working on retrieving a PDF from an external API (in base64 format), decoding it in Python using a Zapier Code step, and generating the PDF successfully. However, I'm stuck on how to store this PDF in Zapier so that I can attach it in a later step.

The PDF filenames vary with each activation: "filename": f"{salesorder_id}.pdf".

Here’s the code I’m using:

 

import requests
import base64

# Get sales order ID
salesorder_id = input_data.get('salesorder_id', '')

# API URL for PDF retrieval
salesorder_pdf_url = f'https://api.example.com/documents/salesorder/{salesorder_id}/pdf'

headers = {
    "accept": "application/json",
    "key": "XXXXXX"  # API key is replaced for security
}

# API call to get the PDF
response = requests.get(salesorder_pdf_url, headers=headers)

if response.status_code == 200:
    json_response = response.json()
    data = json_response.get("data")
    if data:
        pdf_data = base64.b64decode(data)
        return {"pdf_data": base64.b64encode(pdf_data).decode('utf-8'), "filename": f"{salesorder_id}.pdf"}
    else:
        return {"error": "Data not found in the response."}
else:
    return {"error": f"Request failed with status code {response.status_code}."}

# Decode base64 to bytes and attempt to store in Zapier
pdf_bytes = base64.b64decode(pdf_data)


# StoreClient code for Zapier
store = StoreClient('XXXXXX')
sales_order_storage_key = store.store(f"{salesorder_id}.pdf", pdf_bytes)

Does anyone have experience with storing files in Zapier for later use as attachments? Any guidance on where I might be going wrong or how I can properly store the PDF?

Thanks in advance!

Did this topic help you find an answer to your question?

1 reply

cfarrugia
  • Beginner
  • 2 replies
  • September 17, 2024

What does the error look like in the Zap history?

Also be mindful that Zapier Storage values can only hold 25kb.

https://help.zapier.com/hc/en-us/articles/8496293271053-Save-and-retrieve-data-from-Zaps#h_01HMC68GD9SZPW6YHGQB8WYXHK