Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

357
Visualizações
How to print ejs file data to pdf file using pdfkit in node js?

Here is my result.ejs file code:

<div style="width: 50%; margin: auto;">
            <table class="table">
                <thead>
                    <tr>
                        <th>SUBJECT</th>
                        <th>MARKS OBTAINED</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <th>pHYSICS</th>
                        <td>80</td>
                    </tr>
                    <tr>
                        <th>Chemistry</th>
                        <td>80</td>
                    </tr>
                    <tr>
                        <th>mathematics</th>
                        <td>80</td>
                    </tr>
                    <tr>
                        <th>computer</th>
                        <td>80</td>
                    </tr>
                </tbody>
            </table>
        </div>

In my user.js, I am rendring ejs file:

router.get("/result",async(req,res)=>{
  res.render("result");
})

I know basics about pdfkit. But I have used it for printing text only in pdf format like below:

const PDFDocument = require('pdfkit');
const fs = require('fs');
const doc = new PDFDocument();
doc.pipe(fs.createWriteStream('output.pdf'));
doc
  .fontSize(25)
  .text('Some text with an embedded font!', 100, 100);

But Now I need to print ejs file table data in pdf format. Please help me to find out. Thank you.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Yes, you can do this. First you need to render the file and then pass it to pdf. You simply need to render your ejs templated first and then pass the template string to filename xyz.pdf and you can see the ejs templated in pdf.

 // render the ejs file
  ejs.renderFile(path.join(__dirname, 'path-to-your-ejs'), data, {}, function(err, str) {
    if (err) return res.send(err);

    // str now contains your rendered html
   //your pdf object should be used here
    pdf.create(str).toFile("name of the file", function(err, data) {
      if (err) return res.send(err);

      res.send("File created successfully");
    });
  });

Referenced from this link, check the link for more info.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda