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 ![]()
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.
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.
I used single quotes around _blank now and it worked. Thanks again!
I think I know where the issue came from: I probably used Word for typing and it convertedf the ’ to smart quotes!
Nice catch!