<body>
<form method="post">
<label for="username">GFG Username</label>
<input type="text" name="username" />
<input type="submit" id="submit-form"
value="submit" hidden />
</form>
<button id="download-pdf">Submit</button>
<script>
const downloadPdf = document
.querySelector("#download-pdf");
const submitForm = document
.querySelector("#submit-form");
downloadPdf.addEventListener("click", () => {
// Creating the element anchor that
// will download pdf
let element = document.createElement("a");
element.href = "./GFG.pdf";
element.download = "GFG.pdf";
// Adding the element to body
document.documentElement.appendChild(element);
// Above code is equivalent to
// <a href="path of file" download="file name">
// onClick property, to trigger download
element.click();
// Removing the element from body
document.documentElement.removeChild(element);
// onClick property, to trigger submit form
submitForm.click();
});
</script>
</body>