How to retrieve PDF files using JavaScript in Zapier Code block

I’m working on a Zapier automation where I need to fetch a PDF document from a remote URL using the JavaScript Code step. My current implementation returns corrupted data instead of a proper file. I suspect there might be an encoding issue with how I’m handling the response.

Here’s what I have so far:

const downloadUrl = 'https://example.com/document.pdf';

fetch(downloadUrl)
  .then(response => {
    return response.text();
  })
  .then(data => {
    const result = {
      documentId: 5678,
      pdfContent: data
    };
    callback(null, result);
  })
  .catch(error => {
    callback(error);
  });

The output I get contains unreadable characters. What’s the correct way to handle binary PDF data in Zapier’s Code step? Also, if I need to access a protected PDF that requires basic auth credentials, how would I modify this approach?

Your problem is using response.text() for binary data. PDFs are binary files, so you can’t handle them like text in Zapier. Use .arrayBuffer() instead and convert to base64 - that’s what Zapier expects.

Here’s the fix:

const downloadUrl = 'https://example.com/document.pdf';

fetch(downloadUrl)
  .then(response => response.arrayBuffer())
  .then(buffer => {
    const base64String = Buffer.from(buffer).toString('base64');
    const result = {
      documentId: 5678,
      pdfContent: base64String,
      contentType: 'application/pdf'
    };
    callback(null, result);
  })
  .catch(error => {
    callback(error);
  });

If you need basic auth, just add headers:

const credentials = Buffer.from('username:password').toString('base64');
fetch(downloadUrl, {
  headers: {
    'Authorization': `Basic ${credentials}`
  }
})

Once you’ve got the base64 PDF, you can use it in other Zapier steps or convert it back to binary when needed.

You’re hitting the classic binary data encoding problem. Your code treats PDF bytes as text, which completely mangles the data. I’ve hit this exact issue multiple times in production Zapier workflows. Skip response.text() and use response.buffer() instead - it works way better in Zapier’s Node environment. Here’s what actually works:

const downloadUrl = 'https://example.com/document.pdf';

fetch(downloadUrl)
  .then(response => response.buffer())
  .then(buffer => {
    const result = {
      documentId: 5678,
      pdfContent: buffer.toString('base64'),
      mimeType: 'application/pdf'
    };
    callback(null, result);
  })
  .catch(error => callback(error));

For protected resources, just add auth headers before the request. The key difference? Using .buffer() directly - way more reliable than .arrayBuffer() in Zapier’s runtime. This approach handles larger PDFs better without the memory issues you sometimes get with array buffers.

yeah, that’s an encoding issue. use response.blob() instead - it works great for PDFs in zapier. grab the blob and convert to base64 with blob.arrayBuffer().then(buf => Buffer.from(buf).toString('base64')). much simpler and handles large files better.