Endpoint downloads PDF. How to do this via Collection Runner or Newman?

So I have this endpoint that is used to download a PDF. I can click Send and Download and if I save the response with the PDF extension, it writes to the file.

Is there anyway I can have the PDF created using the Collection runner or Newman? So that if instead of just sending the request it Sends and Downloads as well?

Hi there @russellg -

Here’s a tutorial about writing to your local file system by running a local server, or how to use Newman to do something similar. Does that help?

I can try. In this instance it’s a GET request to download the PDF. I will review and if successful mark this as resolved.

@joyce - Unfortunately those options do not work. When they write the body response it’s unable to be opened by a PDF viewer. Thanks anyway.

Hey @russellg, :wave: I was able to read and write a PDF file as follows:

Created a request fetching a PDF file:

In the “Test” script I’m sending the raw response stream to a locally running server.

localhost/save is the endpoint of the server that I’m running.
pm.response.stream is the data I sent to this server.

I’m running a PHP script at the local server to read the data and write it to a file.
This is how my server-side PHP script looks like

$FP = fopen('./saved.pdf','w+') or die("Error creating PDF");

$outp = fopen('php://input', 'r+') or die("Error reading data");

// pm.response.stream is a JSON object so we decode it first
$json = json_decode(stream_get_contents($outp));

// PDF data is inside the 'data' field
// All bytes are stored as an integer
$data = $json->data;
for($idx = 0; $idx < count($data); ++$idx) {
      // covert the bytes to characters (binary) and write to the file
      fwrite($FP,chr($data[$idx])) or die("Error writing data");


echo "Written!";

I was successfully able to open the saved.pdf file.

You will have to see how this can be done in the language of your choice!

Hope this helps :+1:

1 Like

Nice work @amit :ok_hand:

I’ll rewrite this in Javascript and see how I go. Might not be able to do it right now, but if I’m successful I’ll mark this resolved.

Many thanks for posting this :slight_smile:

1 Like

Hey @russellg,

I know it’s basic but would this help if you’re running it with Newman as a library?

const newman = require('newman'),
      fs = require('fs');

  collection: '<Collection File>'
}).on('request', (err, args) => {
  fs.writeFileSync(`./${args.item.name}.pdf`, args.response.stream, 'binary');

I was just using this Dummy request that returned a PDF and it seemed to work:

1 Like

Hi @dannydainton, can I do this on the fly? I don’t want to save the file. I will just get the stream for pdf from a service, and post it to another service.
I appreciate any help.

Hey @fatihgunes,

You might need to expand that question a little bit :smiley:

Could you give me an example of what you’re trying to do?

Sure. Let me explain step by step.
1 - I can GET a pdf file from a webservice using postman. When I click SaveResponse button on the UI the pdf is saved to local and opens without any problem.
2 - I can POST the local pdf file to another webservice using postman. When I select File option on Request/body/form-data Key field and select the file on value, post is done successfully and I can open the pdf file on the target without any problem.

I want to do these two steps in one collection. I don’t want to store the file on my local. I just want to get the file, save the content (stream) to an environment variable, and post this content to the other webservice. Is this possible either Postman or Newman?

When I try to do it, the pdf file posted is created empty!

It’s not something that I’ve tried but it might be possible to script something similar to the solution above, that saves the PDF and then reference that filename in the collection for the POST request and have newman pick it up during the run.

This is how file uploads work in Newman:

Like I said, I’ve never done this some I wouldnt know for sure if it would work :grin: