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

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.

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ā€™);

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.

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

Check this Workflow action to navigate to url

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

You can isolate and test by just calling window.open(ā€œhttp://www.google.comā€, ā€œ_blankā€);

Most likely the variable is invalid or contains a malformed URL

Good morning,

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

But this causes an error:
window.open(ā€œ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ā€); 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ā€

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 :frowning:

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.

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.

1 Like

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?

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).

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

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.

1 Like

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

2 Likes

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

1 Like

Nice catch!

1 Like