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!