medium.com 3 min read Original Source

How to Convert n8n Binary Files from Octet-Stream to PDF

A little while ago I was helping out a client with a project for their business. We were building an n8n automation workflow to download invoices from email attachments, right into a QuickBooks bill. The objective was simple. Create a bill, and add its corresponding invoice attachment along with it.

Well, as it always goes when you say “Should be a quick configuration,” it was not as simple as it sounded. Mainly because of how picky QuickBooks Online is with how it receives data via HTTP requests, but that we were able to figure out quickly.

The one thing that stumped us was the specific file type that the client was receiving. They appeared as PDFs when they got into their email, but when it was downloaded in n8n, the MIME type was showing up as application/pdf, but rather application/octet-stream. This, was the most puzzling part.

The Problem

Now, the reason why this needed to be converted was because QuickBooks online was not accepting this file type.

One thing I noticed was that when I downloaded the file on my computer, and uploaded the file, the Mime type showed up as PDF. It somehow got converted upon downloading, but this is not a solution in terms of a complete, hands-free automated process.

So, I figured, what if I downloaded it, used an Upload node to OneDrive and download it back down again? Well, that didn’t work either! Why? OneDrive didn’t want to except this either. It was basically coming up as unrecognizable. I didn’t try Google Drive but even if it did work, we didn’t feel like this was a solid solution.

The Fix

Alright, now I want to go into detail about octet-stream MIME types and what they are. I’m sure you’re as frustrated as I was when I was trying to upload this to anywhere. Which is why you’re here in the first place. So, let’s get right to it.

Fortunately for you, you won’t have to spend a lot of time searching through unanswered questions in forums about this or spend time testing other nodes that never worked (I’m sorry if you already have). Instead, I’ll give you the solution to this which is thankfully only one node.

Since this was a bit of a complex issue, it a custom built solution. Thankfully, n8n allows for you to do this with their Code node to write custom functions in JavaScript, which is what we’ll do here.

Right after your node that downloads the file as an application/octet-stream, you will plug in the node to write the code. For me, it sat in between the node that downloaded the file, and the one that was supposed to upload the file.

Just like this:

Great! Now open up that node. Set the following settings:

Mode: Run Once for All Items

Language: JavaScript

Next, we’re going to do some coding. Don’t know how to code? No worries! I’ll tell you exactly what to put in. The first thing you’re going to want to do is copy the code below:

{
if (item.binary && item.binary.data) {
item.binary.data.mimeType = 'application/pdf';
item.binary.data.fileName = item.json.name || 'attachment.pdf';
}
return item;
});” data-toggle-ms=”1200″ aria-label=”Copy code” data-nosnippet=”” class=”code-copy-btn absolute right-3 top-3 size-8 p-1.5 flex items-center justify-center bg-black/50 text-white rounded-md transition-colors duration-200 cursor-pointer hover:bg-black/70″>

return items.map(item => {
  if (item.binary && item.binary.data) {
    item.binary.data.mimeType = 'application/pdf';
    item.binary.data.fileName = item.json.name || 'attachment.pdf';
  }
  return item;
});
return items.map(item => {
  if (item.binary && item.binary.data) {
    item.binary.data.mimeType = 'application/pdf';
    item.binary.data.fileName = item.json.name || 'attachment.pdf';
  }
  return item;
});

Now, go ahead and paste it in the code editor box inside the node. It might be infuriating, but that’s all it took. Now, when you run your flow, you will see that this bit of code takes the binary file, and converts it into PDF.

Now, the cool thing about this bit of code is that it works for all file types. You can have any n8n binary file as the input, and it will convert it to PDFs. That’s the theory, I haven’t tested absolutely every file type yet, but that’s what this code is supposed to do.

Once your code has converted the MIME type to application/pdf, try uploading the file now and see what you get.

As you can see here, I have an HTTP request node that is supposed to upload the attachment to a specific bill on QuickBooks Online. Now that the MIME type of the file has been converted to application/pdf, it can now read this file and sends the attachment to the bill in QuickBooks.

That’s It

That is all! If you’ve been stuck with trying to upload n8n binary files that show up as octet-stream, I hope this guide was helpful to you. Feel free to give me a follow for more articles about tech, AI, and automations.

Feel free to reach out if you get stuck somewhere with this node.

Happy automating!