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

**URL:** <https://community.postman.com/t/endpoint-downloads-pdf-how-to-do-this-via-collection-runner-or-newman/13405>\
**Category:** Help Hub\
**Created:** [June 15, 2020, 3:41am UTC](https://community.postman.com/t/endpoint-downloads-pdf-how-to-do-this-via-collection-runner-or-newman/13405 "2020-06-15T03:41:51Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![russellg](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/russellg/32/4729_2.png) [@russellg](https://community.postman.com/u/russellg)\
**Post date:** [June 15, 2020, 3:41am UTC](https://community.postman.com/t/endpoint-downloads-pdf-how-to-do-this-via-collection-runner-or-newman/13405/1 "2020-06-15T03:41:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jetison](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jetison/32/12_2.png) [@jetison](https://community.postman.com/u/jetison)\
**Post date:** [June 15, 2020, 9:45pm UTC](https://community.postman.com/t/endpoint-downloads-pdf-how-to-do-this-via-collection-runner-or-newman/13405/2 "2020-06-15T21:45:52Z")

</div>

Hi there @russellg -

Here’s [a tutorial about writing to your local file system by running a local server](http://blog.getpostman.com/2017/09/01/write-to-your-local-file-system-using-a-postman-collection/), or [how to use Newman to do something similar](https://github.com/postmanlabs/postman-app-support/issues/3033#issuecomment-301758179). Does that help?

---

<div class="post-metadata">

**Author:** ![russellg](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/russellg/32/4729_2.png) [@russellg](https://community.postman.com/u/russellg)\
**Post date:** [June 15, 2020, 11:31pm UTC](https://community.postman.com/t/endpoint-downloads-pdf-how-to-do-this-via-collection-runner-or-newman/13405/3 "2020-06-15T23:31:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![russellg](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/russellg/32/4729_2.png) [@russellg](https://community.postman.com/u/russellg)\
**Post date:** [June 17, 2020, 12:43am UTC](https://community.postman.com/t/endpoint-downloads-pdf-how-to-do-this-via-collection-runner-or-newman/13405/4 "2020-06-17T00:43:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![amit](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/amit/32/2056_2.png) [@amit](https://community.postman.com/u/amit)\
**Post date:** [June 17, 2020, 1:28am UTC](https://community.postman.com/t/endpoint-downloads-pdf-how-to-do-this-via-collection-runner-or-newman/13405/5 "2020-06-17T01:28:40Z")

</div>

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

**Created a request fetching a PDF file:**

 ![Screen Shot 2020-06-17 at 1.20.31 PM](https://us1.discourse-cdn.com/getpostman/original/3X/2/8/28b3cb6d3fa80c7cadac0f5328f51908db7fb7c7.png)  
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

```
<?php 
   
$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");
}

fclose($outp);
fclose($FP);

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 👍

---

<div class="post-metadata">

**Author:** ![russellg](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/russellg/32/4729_2.png) [@russellg](https://community.postman.com/u/russellg)\
**Post date:** [June 17, 2020, 5:33am UTC](https://community.postman.com/t/endpoint-downloads-pdf-how-to-do-this-via-collection-runner-or-newman/13405/6 "2020-06-17T05:33:26Z")

</div>

Nice work @amit 👌

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 🙂

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [June 17, 2020, 12:27pm UTC](https://community.postman.com/t/endpoint-downloads-pdf-how-to-do-this-via-collection-runner-or-newman/13405/7 "2020-06-17T12:27:25Z")

</div>

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');

newman.run({
  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:

> **[dummy.pdf](https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf)**
>
> 12.95 KB

---

<div class="post-metadata">

**Author:** ![fatihgunes](https://avatars.discourse-cdn.com/v4/letter/f/6f9a4e/32.png) [@fatihgunes](https://community.postman.com/u/fatihgunes)\
**Post date:** [June 24, 2020, 12:17pm UTC](https://community.postman.com/t/endpoint-downloads-pdf-how-to-do-this-via-collection-runner-or-newman/13405/8 "2020-06-24T12:17:43Z")

</div>

Hi @danny-dainton, 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.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [June 24, 2020, 12:22pm UTC](https://community.postman.com/t/endpoint-downloads-pdf-how-to-do-this-via-collection-runner-or-newman/13405/9 "2020-06-24T12:22:34Z")

</div>

Hey @fatihgunes,

You might need to expand that question a little bit 😃

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

---

<div class="post-metadata">

**Author:** ![fatihgunes](https://avatars.discourse-cdn.com/v4/letter/f/6f9a4e/32.png) [@fatihgunes](https://community.postman.com/u/fatihgunes)\
**Post date:** [June 27, 2020, 9:47pm UTC](https://community.postman.com/t/endpoint-downloads-pdf-how-to-do-this-via-collection-runner-or-newman/13405/10 "2020-06-27T21:47:51Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [June 28, 2020, 8:38am UTC](https://community.postman.com/t/endpoint-downloads-pdf-how-to-do-this-via-collection-runner-or-newman/13405/11 "2020-06-28T08:38:20Z")

</div>

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:

> **[GitHub - postmanlabs/newman: Newman is a command-line collection runner for...](https://github.com/postmanlabs/newman#file-uploads)**
>
> Newman is a command-line collection runner for Postman - GitHub - postmanlabs/newman: Newman is a command-line collection runner for Postman

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

---

<div class="post-metadata">

**Author:** ![artu-hnrq-denox](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/artu-hnrq-denox/32/7275_2.png) [@artu-hnrq-denox](https://community.postman.com/u/artu-hnrq-denox)\
**Post date:** [February 6, 2021, 4:50am UTC](https://community.postman.com/t/endpoint-downloads-pdf-how-to-do-this-via-collection-runner-or-newman/13405/12 "2021-02-06T04:50:40Z")

</div>

Should it be run through npm as a node package? i.e. from within a javascript file?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [February 6, 2021, 8:41am UTC](https://community.postman.com/t/endpoint-downloads-pdf-how-to-do-this-via-collection-runner-or-newman/13405/13 "2021-02-06T08:41:20Z")

</div>

That’s right, using Newman as a library, in a small nodejs project.

Creating a very basic nodejs project can be done like this:

- Create a new directory using `mkdir <new dir name>`
- Move to the new directory using `cd <new dir name>`
- Create a _package.json_ file using `npm init -y`
- Install the required node modules using `npm i -S newman`
- Create a new .js file and add the script
- Run using `node <filename>.js`

---

<div class="post-metadata">

**Author:** ![artu-hnrq-denox](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/artu-hnrq-denox/32/7275_2.png) [@artu-hnrq-denox](https://community.postman.com/u/artu-hnrq-denox)\
**Post date:** [February 6, 2021, 4:25pm UTC](https://community.postman.com/t/endpoint-downloads-pdf-how-to-do-this-via-collection-runner-or-newman/13405/14 "2021-02-06T16:25:03Z")

</div>

Pretty nice! Thanks for the fast reply, Danny!
