Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda