PDF file encoding when downloading from API

Viewed 314

I have the following code which downloads a PDF file from Xero API into Zapier. The code works fine, but the PDF file is always blank white page. I have inspected the contents of the PDF file returned (in a text file) and the top characters are returned as diamond shaped question marks (special characters). See screenshot below.

I have also compared with a working PDF file and the diamond shaped question marks are displayed as È on the good file.

Here's my code:

const pdfURL = {
  url: 'https://api.xero.com/api.xro/2.0/Quotes/' + bundle.inputData.QuoteID,
  method: 'GET',
  headers: {
    'Accept': 'application/pdf',
    'Authorization': `Bearer ${bundle.authData.access_token}`,
    'Xero-tenant-id': bundle.inputData.TenantID,
    'Content-type': 'application/pdf'
  }
};
const fileRequest = await z.request(pdfURL);
const url = await z.stashFile(fileRequest, fileRequest.length, 'Quote #123' + '.pdf', 'application/pdf;'); // knownLength and filename will be sniffed from the request. contentType will be binary/octet-stream
return {url};

Here's a screenshot of the contents in the corrupt PDF file:

enter image description here

Does anyone know what the problem could be?

I have tried setting the charset to 'utf-8' to no avail.

Any help would be greatly appreciated.

1 Answers

I just had the exactly same problem here.

To solve it, just set the parameter raw:true in the z.request(), it will avoid parsing and messing up with the encoding.

Like that:

const pdfURL = {
  ...
  raw: true
};

Your file will then render properly.

Related