I have json file with info from multiple individuals. The JS file (imported into my HTML file) first reads the json file, and stores the info in an array of people objects. I want to iterate through this array, updating the HTML for one person at a time (essentially creating a unique form for each person). At the end of each update (iteration), I want to generate a pdf of the current HTML using wkhtmltopdf. Then the info from the HTML will be cleared, and updated with info from the next person, at which point a new pdf will be generated. Please point me in the right direction to go about doing this.
Actually wkhtmltopdf is able to run JavaScript pretty well (but not always perfectly). The key might be to use something like --javascript-delay 3000 - this thing will make wkhtmltopdf wait for 3000ms (3 seconds) for JavaScript execution to finish. For example I have this file
<html>
<body>
<h1>Users</h1>
<div id="users"></div>
</body>
<script>
function pushMessage(msg) {
const x = document.createElement("div");
x.innerText = msg;
document.getElementById("users").appendChild(x);
}
function handleUsers(users) {
users.forEach(user => pushMessage(user.name));
}
try {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState !== 4) return;
if (xhr.status >= 200 && xhr.status < 300) {
const users = JSON.parse(xhr.responseText);
handleUsers(users);
}
};
xhr.open('GET', 'https://jsonplaceholder.typicode.com/users');
xhr.send();
} catch(e) {
document.write(e.message);
}
</script>
</html>
And then I convert this with wkhtmltopdf --javascript-delay 3000 index.html index.pdf I will get the expected result in a PDF; meaning it looks the same as running this normally in the browser.