How can I filter only PDFs from multiple attachments?

Hi everyone,

I have a workflow where I receive multiple file attachments from emails, including PDFs and other file types (e.g., PNG, JPG). I want to filter out only the PDF files and process them further.

I tried using a Filter Node, but it didn’t work as expected. Ideally, I want to keep only files with mimeType: application/pdf.

My current approach:

{{ Object.values($json.binary || {}).some(file => file.mimeType === “application/pdf”) }}

However, this returns false, even though there are PDFs in the data.

What’s the best way to achieve this? Should I use a Code Node, or is there a way to do it directly with a Filter or Set Node?

Thanks in advance for your help! :rocket:


After listing the files, add an IF node to check if each file’s name ends with “.pdf”. For example, if your file name is stored in a field called name, you can set the condition using an expression like:

{{$json["name"].endsWith(".pdf")}}

This example uses a form upload and a Code Node

In the code node you can filter the filetype pretty easily.

let results = [];

for (item of items) {
    for (key of Object.keys(item.binary)) {
        if( item.binary[key].mimeType == 'application/pdf' ) {
          results.push({
              json: {
                  fileName: item.binary[key].fileName
              },
              binary: {
                  data: item.binary[key],
              }
          });
        }
    }
}
return results;

if you consider this to be the answer please click the ‘solution’ button

1 Like

i tried but than get this error message

Problem running workflow

Please execute the whole workflow, rather than just the node. (Existing execution data is too large.)

I will have another look, can you post your workflow also?

copy the wf and paste it between these “```”

I checked using a merge node, the result is the same.

So it seems your problem is something else not related to my answer for filtering your input.

If you can show you wf we can have a look, you might have to much data in your browser, so re loading could be an answer.

→ your execution data is stored in memory, If you don’t hit the bin/trash can icon at the bottom of the screen then often you can exceed the memory limits.

So, test the filter with fewer files and from there expand.

if you consider this to be the answer please click the ‘solution’ button

2 Likes

thank you very much

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.