Aquí está mi código de archivo result.ejs:
<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>En mi user.js, estoy renderizando el archivo ejs:
router.get("/result",async(req,res)=>{ res.render("result"); })Sé lo básico sobre pdfkit. Pero lo he usado para imprimir texto solo en formato pdf como se muestra a continuación:
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);Pero ahora necesito imprimir los datos de la tabla del archivo ejs en formato pdf. Por favor, ayúdame a averiguarlo. Gracias.
Sí, usted puede hacer esto. Primero necesitas renderizar el archivo y luego pasarlo a pdf. Simplemente necesita renderizar sus ejs con plantilla primero y luego pasar la cadena de plantilla al nombre de archivo xyz.pdf y podrá ver los ejs con plantilla en 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"); }); });Referenciado desde este enlace, consulte el enlace para obtener más información.