Error in PDF generation

I am trying to read Gmail and save the PDF attachments to a local file. Getting errors in that.


This is the config for - "Save file" -

A pdf is not a text file try saving as bianry.

You have not supplied any other info except an image, so no idea what your code does also.

1 Like

Thanks @E1cid . Actually this is the config which worked for me.

Now I am getting another problem regarding the pdf file sending as Multi-part FormData. I am trying to use - 'node-red-contrib-send-multipart-formdata'

Send where?
Again little to no info
please supply an example flow.

if you are trying to download from http endpoint you need to send correct headers
maybe this is what you are trying to do, at a guess

[{"id":"2a287cd793191db8","type":"http in","z":"b9860b4b9de8c8da","name":"","url":"/download","method":"get","upload":false,"swaggerDoc":"","x":540,"y":360,"wires":[["abdca12f81294686"]]},{"id":"abdca12f81294686","type":"file in","z":"b9860b4b9de8c8da","name":"","filename":"/path_tofile/.name.pdf","filenameType":"str","format":"","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":820,"y":320,"wires":[["0fc1dc5a7e1d36b9","9ab140b60dd0ac72"]]},{"id":"0fc1dc5a7e1d36b9","type":"debug","z":"b9860b4b9de8c8da","name":"debug 328","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1110,"y":200,"wires":[]},{"id":"9ab140b60dd0ac72","type":"function","z":"b9860b4b9de8c8da","name":"function 21","func":"msg.headers = {\n   \"Content-Type\": \"application/pdf\",\n   \"Content-Transfer-Encoding\": \"binary\",\n   \"Content-Disposition\": \"attachment; filename=\" + msg.filename,\n   \"Content-Length\": msg.payload.count\n}    \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1150,"y":300,"wires":[["6b73318a3f5a65b2"]]},{"id":"6b73318a3f5a65b2","type":"http response","z":"b9860b4b9de8c8da","name":"","statusCode":"","headers":{},"x":1030,"y":380,"wires":[]}]

Sure @E1cid ...
My requirement is to POST an API with Multi-part FormData with a body content with a PDF file.
This is my flow as of now, which I am working on.


This is the parameters used in the function before calling the HTTP POST ->

[
    {
        "id": "82a30f46ea3c7562",
        "type": "debug",
        "z": "385ddd017d4b7d79",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 710,
        "y": 500,
        "wires": []
    },
    {
        "id": "3670dc06d7feedab",
        "type": "inject",
        "z": "385ddd017d4b7d79",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 120,
        "y": 440,
        "wires": [
            [
                "f08e4ad30276d8b2"
            ]
        ]
    },
    {
        "id": "f08e4ad30276d8b2",
        "type": "function",
        "z": "385ddd017d4b7d79",
        "name": "Set File Name",
        "func": "var filepath = 'C:\\\\Users\\\\sabchatterjee\\\\Documents\\\\Enabling-Area\\\\Architecture\\\\Demos\\\\';\n\nmsg.filename = filepath + 'PO_19930217.pdf';\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 300,
        "y": 440,
        "wires": [
            [
                "d1c0f49ecd3cca33"
            ]
        ]
    },
    {
        "id": "364b80e52c1a29e5",
        "type": "http request",
        "z": "385ddd017d4b7d79",
        "name": "",
        "method": "POST",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "https://noderedonscpcf-XXXXXXX.cfapps.us10-001.hana.ondemand.com/read-multipart",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 530,
        "y": 500,
        "wires": [
            [
                "82a30f46ea3c7562"
            ]
        ]
    },
    {
        "id": "d1c0f49ecd3cca33",
        "type": "file in",
        "z": "385ddd017d4b7d79",
        "name": "Read PDF File",
        "filename": "filename",
        "filenameType": "msg",
        "format": "",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 500,
        "y": 440,
        "wires": [
            [
                "113c991273b81a32"
            ]
        ]
    },
    {
        "id": "113c991273b81a32",
        "type": "function",
        "z": "385ddd017d4b7d79",
        "name": "Set Request Parameters",
        "func": "msg.headers = {};\nmsg.headers['Content-Type'] = 'multipart/form-data';\nmsg.headers['Accept'] = 'application/json';\nmsg.headers['x-api-key'] = 'XXXXXXXXXXXXXXXXXXXXXXXX';\nmsg.payload = {\n    'file': msg.payload\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 310,
        "y": 500,
        "wires": [
            [
                "364b80e52c1a29e5"
            ]
        ]
    }
]

This should work untested.

[{"id":"3670dc06d7feedab","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":680,"y":60,"wires":[["d1c0f49ecd3cca33"]]},{"id":"d1c0f49ecd3cca33","type":"file in","z":"b9860b4b9de8c8da","name":"Read PDF File","filename":"/path_to_file/sample.pdf","filenameType":"str","format":"","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":920,"y":60,"wires":[["f59d8ff6f0fbe54e"]]},{"id":"f59d8ff6f0fbe54e","type":"function","z":"b9860b4b9de8c8da","name":"Create multipart/form-data","func":"msg.headers = {\n    \"content-type\": \"multipart/form-data\"\n}\n\nmsg.payload = {\n    \"file\": {\n        \"value\": msg.payload, //file buffer\n        \"options\": {\n            \"filename\": \"test.pdf\"  \n        }\n    }\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":120,"wires":[["364b80e52c1a29e5"]]},{"id":"364b80e52c1a29e5","type":"http request","z":"b9860b4b9de8c8da","name":"","method":"POST","ret":"obj","paytoqs":"ignore","url":"<add url>","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":950,"y":120,"wires":[["82a30f46ea3c7562"]]},{"id":"82a30f46ea3c7562","type":"debug","z":"b9860b4b9de8c8da","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1130,"y":120,"wires":[]}]

There are plenty of examples if you search the forum also.

Thanks a lot @E1cid .....

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