I have a PDF generator that takes a full HTML document string as an argument, and then opens up the PDF created in a new tab/window, or downloads the file. I've been asked to wire up a form that will allow for making a certificate that takes semi-colon separated names and generates multiple PDFs from it. Here's the code that does it:
$("#certgensubmit").click(function (event) {
// Get the names to generate certificates for.
var names = $("#presented-to").html().split(';');
$.each(names, function(index, value) {
if(value.length > 0) {
var presentedBy = $("#presented-by").html();
var awardName = $("#award-name").html();
var presentedDate = $("#presented-date").html();
var signerName = $("#signer-name").html();
var html = getCertificateHTML(presentedBy, value, awardName, presentedDate, signerName);
//console.log(html);
$("#HtmlContent").val(html);
$("#pdfform").submit();
}
});
});
This works, except that it only creates the first certificate PDF. It's processing the code each time, though. If I uncomment the "console.log(html)" line I see the correct output in Chrome's console. I was able to get it sort of working by creating a new document in JavaScript in the loop, and running the form submit from within each one. The only problem is that it worked while only putting text inside the field sent to the PDF generator, but completely failed when sending HTML in the field, which the generator requires. So it seems that the problem has something to do with opening a new tab during the creation of the PDF. I need this to occur, so the user isn't taken away from the UI that accepts input for the PDF.
Don't get too caught up in the PDF generation aspect - I just need to know how to trigger the form submission multiple times within the jQuery each loop with different values. The rest is just for context.