Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
Executing wkhtmltopdf on a changing HTML to generate multiple pdfs

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!