# Workflow step with JavaScript: result is a link to a generated a PDF file - how can I retrieve it?

**URL:** https://community.weweb.io/t/workflow-step-with-javascript-result-is-a-link-to-a-generated-a-pdf-file-how-can-i-retrieve-it/4590
**Category:** How do I?
**Created:** [September 26, 2023, 12:30pm UTC](https://community.weweb.io/t/workflow-step-with-javascript-result-is-a-link-to-a-generated-a-pdf-file-how-can-i-retrieve-it/4590 "2023-09-26T12:30:39Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![BertrandG](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/bertrandg/32/5129_2.png) [@BertrandG](https://community.weweb.io/u/BertrandG)
#### Post date: [September 26, 2023, 12:30pm UTC](https://community.weweb.io/t/workflow-step-with-javascript-result-is-a-link-to-a-generated-a-pdf-file-how-can-i-retrieve-it/4590/1 "2023-09-26T12:30:39Z")

</div>

Hello,  
I have a working JavaScript which generates a PDF file via an API call. The result of the first (and only) step is a link to the generated PDF file. Openbing the link in a brwoser displays the PDF file but how can I download the file or open in a new browser tab? There is no Open URL command as in AppGyver.

---

<div class="post-metadata">

### Author: ![sleeusis](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@sleeusis](https://community.weweb.io/u/sleeusis)
#### Post date: [September 26, 2023, 1:49pm UTC](https://community.weweb.io/t/workflow-step-with-javascript-result-is-a-link-to-a-generated-a-pdf-file-how-can-i-retrieve-it/4590/2 "2023-09-26T13:49:11Z")

</div>

Not sure if this is what you are looking for but you can direct and open the link to a new page in a tab in your jscript using window.open(_pdf\_url_, ‘\_blank’);

---

<div class="post-metadata">

### Author: ![BertrandG](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/bertrandg/32/5129_2.png) [@BertrandG](https://community.weweb.io/u/BertrandG)
#### Post date: [September 26, 2023, 2:05pm UTC](https://community.weweb.io/t/workflow-step-with-javascript-result-is-a-link-to-a-generated-a-pdf-file-how-can-i-retrieve-it/4590/3 "2023-09-26T14:05:06Z")

</div>

> [@sleeusis](#):
>
> window.open(_pdf\_url_, ‘\_blank’);

Thank you! This might help but I don’t know how to implement it. Here’s my JS so far:

const json = await response.json(); // Translate the request a json  
console.log(“step 7”, json)  
const {file, status, transaction\_ref} = json;  
return { **file** , status, transaction\_ref};

} catch (err) {  
const error = {  
code: ‘UnknownError’,  
message: ‘Something during fetching data’,  
rawErrr: JSON.stringify(err)  
}  
return [1, {error}]  
}

* * *

“file” is the URL to the generated PDF. But window.open( **file** , ‘\_blank’); causes an error. As I said, I’m not a JS expert.

---

<div class="post-metadata">

### Author: ![BertrandG](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/bertrandg/32/5129_2.png) [@BertrandG](https://community.weweb.io/u/BertrandG)
#### Post date: [September 26, 2023, 3:45pm UTC](https://community.weweb.io/t/workflow-step-with-javascript-result-is-a-link-to-a-generated-a-pdf-file-how-can-i-retrieve-it/4590/4 "2023-09-26T15:45:33Z")

</div>

> [@sleeusis](#):
>
> window.open

I’ve tried again and now there’s no error)?!). I just omitted “\_blank”  
window.open(file);  
} catch (err) {  
const error = {  
code: ‘UnknownError’,  
message: ‘Something happened during fetching data’,  
rawErrr: JSON.stringify(err)

However, nothing happens, no error reported; workflow test and actual button click seem to work but the PDF isn’t opened

---

<div class="post-metadata">

### Author: ![sleeusis](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@sleeusis](https://community.weweb.io/u/sleeusis)
#### Post date: [September 26, 2023, 6:32pm UTC](https://community.weweb.io/t/workflow-step-with-javascript-result-is-a-link-to-a-generated-a-pdf-file-how-can-i-retrieve-it/4590/5 "2023-09-26T18:32:31Z")

</div>

Check this [Workflow action to navigate to url](https://community.weweb.io/t/workflow-action-to-navigate-to-url/3654)

Make sure the variable _ **file** _ is a string containing a valid URL.

---

<div class="post-metadata">

### Author: ![sleeusis](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@sleeusis](https://community.weweb.io/u/sleeusis)
#### Post date: [September 26, 2023, 6:38pm UTC](https://community.weweb.io/t/workflow-step-with-javascript-result-is-a-link-to-a-generated-a-pdf-file-how-can-i-retrieve-it/4590/6 "2023-09-26T18:38:59Z")

</div>

You can isolate and test by just calling _window.open(“[http://www.google.com](http://www.google.com)”, “\_blank”);_

Most likely the variable is invalid or contains a malformed URL

---

<div class="post-metadata">

### Author: ![BertrandG](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/bertrandg/32/5129_2.png) [@BertrandG](https://community.weweb.io/u/BertrandG)
#### Post date: [September 27, 2023, 9:14am UTC](https://community.weweb.io/t/workflow-step-with-javascript-result-is-a-link-to-a-generated-a-pdf-file-how-can-i-retrieve-it/4590/7 "2023-09-27T09:14:46Z")

</div>

Good morning,

I’d already tested with a dummy button using only this On click command:  
window.open("[https://www.w3schools.com](https://www.w3schools.com)”);  
This opens the page in a new tab.

But this causes an **error** :  
window.open(“[https://www.w3schools.com](https://www.w3schools.com)” **, "\_blank”** );  
Anyway, the default behaviour is “open in a new tab” hence I can omit “\_blank”.

However, if I’m inserting window.open(“[https://www.w3schools.com](https://www.w3schools.com)”); into the full script, after “return {file, status, transaction\_ref};”, **nothing happens**.

This is the **file** variable and IMHO it’s a string:  
**file** : “[https://craftmypdf-gen-de.s3.eu-central-1.amazonaws.com/7d3b64a0-a4bd-4bdd-ba30-ab66103eccfe/INV38379.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIA6ENCBKJYLWJUD36X%2F20230927%2Feu-central-1%2Fs3%2Faws4\_request&X-Amz-Date=20230927T090001Z&X-Amz-Expires=3600&X-Amz-Signature=da26cd8a5a47eb532e9e452607d866c1b17b1446c5e31d131a603a47b5b91df4&X-Amz-SignedHeaders=host](https://craftmypdf-gen-de.s3.eu-central-1.amazonaws.com/7d3b64a0-a4bd-4bdd-ba30-ab66103eccfe/INV38379.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIA6ENCBKJYLWJUD36X%2F20230927%2Feu-central-1%2Fs3%2Faws4_request&X-Amz-Date=20230927T090001Z&X-Amz-Expires=3600&X-Amz-Signature=da26cd8a5a47eb532e9e452607d866c1b17b1446c5e31d131a603a47b5b91df4&X-Amz-SignedHeaders=host)”

Note: It’s only valid for some minutes.

I assume I didn’t place the window.open command at the **correct position**. Where should it been inserted in my code?

// Translate the request to json  
console.log(“step 7”, json)  
const {file, status, transaction\_ref} = json;  
return {file, status, transaction\_ref};  
_Nothing happens if placed here_  
} catch (err) {  
const error = {  
code: ‘UnknownError’,  
message: ‘Something happened during fetching data’,  
rawErrr: JSON.stringify(err)  
}  
return [1, {error}]  
}

If I insert it at the end of the script, it causes an error (unexpected token or similar).

As mentioned before, I’m not a JS expert ☹

---

<div class="post-metadata">

### Author: ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)
#### Post date: [September 27, 2023, 10:48am UTC](https://community.weweb.io/t/workflow-step-with-javascript-result-is-a-link-to-a-generated-a-pdf-file-how-can-i-retrieve-it/4590/8 "2023-09-27T10:48:40Z")

</div>

the js action is the body of a function. If you `return` the execution of the function stops and anything after that does not get executed.

> **[return - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return)**
>
> The return statement ends function execution and specifies a value to be returned to the function caller.

Keep in mind that in weweb you need to return something from the js action only if you want it available to be used in the next steps.

---

<div class="post-metadata">

### Author: ![BertrandG](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/bertrandg/32/5129_2.png) [@BertrandG](https://community.weweb.io/u/BertrandG)
#### Post date: [September 27, 2023, 11:02am UTC](https://community.weweb.io/t/workflow-step-with-javascript-result-is-a-link-to-a-generated-a-pdf-file-how-can-i-retrieve-it/4590/9 "2023-09-27T11:02:59Z")

</div>

OK, I understand. In my case, **file** is returned and it’s a string. In my previous app (AppGyver), there was actually following an **Open URL** step. But I don’t see any such step action in WeWeb. How can I use the **file** variable to open the page?

---

<div class="post-metadata">

### Author: ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)
#### Post date: [September 27, 2023, 11:36am UTC](https://community.weweb.io/t/workflow-step-with-javascript-result-is-a-link-to-a-generated-a-pdf-file-how-can-i-retrieve-it/4590/10 "2023-09-27T11:36:49Z")

</div>

Again, if you return in the js action your next action will be executed, so you can do what you want to do before returning or you can add another js action and use the returned data (it will be under the workflow section, the tab with the bolt icon).

---

<div class="post-metadata">

### Author: ![BertrandG](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/bertrandg/32/5129_2.png) [@BertrandG](https://community.weweb.io/u/BertrandG)
#### Post date: [September 27, 2023, 11:51am UTC](https://community.weweb.io/t/workflow-step-with-javascript-result-is-a-link-to-a-generated-a-pdf-file-how-can-i-retrieve-it/4590/11 "2023-09-27T11:51:44Z")

</div>

> [@BertrandG](#):
>
> window.open( **file** , ‘\_blank’);

Great, it worked!!! Thank you so much!  
Just a note: adding **, "\_blank** always causes an error.

---

<div class="post-metadata">

### Author: ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)
#### Post date: [September 27, 2023, 11:59am UTC](https://community.weweb.io/t/workflow-step-with-javascript-result-is-a-link-to-a-generated-a-pdf-file-how-can-i-retrieve-it/4590/12 "2023-09-27T11:59:17Z")

</div>

It’s probably because you are using the wrong character for quotes: only single/double quotes and backtick are valid to define a string. Anyway you should see the type of error in the browser’s console.  
`window.open('https://google.com','_blank')` is perfectly valid.

---

<div class="post-metadata">

### Author: ![BertrandG](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/bertrandg/32/5129_2.png) [@BertrandG](https://community.weweb.io/u/BertrandG)
#### Post date: [September 27, 2023, 12:03pm UTC](https://community.weweb.io/t/workflow-step-with-javascript-result-is-a-link-to-a-generated-a-pdf-file-how-can-i-retrieve-it/4590/13 "2023-09-27T12:03:43Z")

</div>

> [@BertrandG](#):
>
> window.open( **file** , ‘\_blank’); causes an error

I used single quotes around \_blank now and it worked. Thanks again!

---

<div class="post-metadata">

### Author: ![BertrandG](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/bertrandg/32/5129_2.png) [@BertrandG](https://community.weweb.io/u/BertrandG)
#### Post date: [September 27, 2023, 12:05pm UTC](https://community.weweb.io/t/workflow-step-with-javascript-result-is-a-link-to-a-generated-a-pdf-file-how-can-i-retrieve-it/4590/14 "2023-09-27T12:05:36Z")

</div>

I think I know where the issue came from: I probably used Word for typing and it convertedf the ’ to smart quotes!

---

<div class="post-metadata">

### Author: ![raydeck](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/raydeck/32/384_2.png) [@raydeck](https://community.weweb.io/u/raydeck)
#### Post date: [September 27, 2023, 11:34pm UTC](https://community.weweb.io/t/workflow-step-with-javascript-result-is-a-link-to-a-generated-a-pdf-file-how-can-i-retrieve-it/4590/15 "2023-09-27T23:34:43Z")

</div>

Nice catch!
