【问题标题】:How to Generate a PDF using NodeJs, EJS and Mysql data如何使用 NodeJs、EJS 和 Mysql 数据生成 PDF
【发布时间】:2021-06-06 08:37:17
【问题描述】:

我想在我的 ejs PDF 上显示关于使用 nodejs 的我的 MYSQL 数据 所以我在显示 PDF 格式的数据时遇到了问题: 这是我的节点 js 代码,我想将数据(MYSQL 数据库)从 JS 发送到 EJS 文件并以 pdf 格式显示:




const mysql = require('mysql')
pdf = require('express-pdf');
const ejs =  require('ejs');
//Connection to database
const pool = mysql.createPool({
    connectionLimit :10,
    host :"localhost" ,
    user :"root",
    password :"",
    database :"backend",
    multipleStatements: true

})
module.exports={
//Creation Of pdf
    pdfversion : function(req,res){
        res.pdfFromHTML({
           filename: 'templatee.ejs',
           html: path.resolve(__dirname, './templatee.ejs')
           
           });  
     },
//Getting the data from MYSQL database
     getonetva : function(req , res){
        pool.getConnection((err,connection)=>{
            if(err) throw err        
            const id = req.params.id;
            const data =`SELECT *  FROM formulaire ` ;
            connection.query(data, (err,rows) =>{
                 connection.release()
                 console.log(rows)
                 if(!err){
                     res.send(rows)

//res.render To send the data to EJS file , and i think i have the Problem here :
                     res.render('./templatee.ejs', {
                        results: JSON.stringify(rows)
                    });
                 console.log(rows);
                 }
                 else{
                     console.log(err)
                 }
    
            })
        } )
     
    },

我们有 EJS 文件:

<div class="table-data">
    <h2>Display Data Node.js & MySQL</h2>
        <table border="1">
            <tr>            
                <th> Name</th>
                <th>Last Name </th>
                <th>Email</th>
                <th>Phone number</th>   
            </tr>   
            <tr>
                <td><%=results.nom %></td>
                <td><%=results.prenom %></td>
                <td><%=results.email %></td>
                <td><%=results.tel %></td>          
            </tr>           
        </table>
        </div>

和路由器:

const fac = require('../Controller/gestclient');
const route = require('express').Router();
route.get('/topdf',fac.pdfversion);
route.get('/show',fac.getalltva);
module.exports = route

这是我输入 http://localhost:5000/gestion/topdf 时得到的结果:它显示了模板,但没有替换。

【问题讨论】:

    标签: mysql node.js pdf ejs


    【解决方案1】:

    您必须先渲染您的 ejs 模板,然后再将其交给.pdfFromHTML()。尝试这样的事情,使用callback in your invocation of res.render()

    这不是调试的。

    pdfversion : function(req,res, next){
      /* get your data from SELECT */
      res.render(
        './templatee.ejs', 
        { results: JSON.stringify(rows) },
          function (err, html) {
            if (err) return next(err)
            res.pdfFromHTML( {
              htmlContent: html,
              directory: '/tmp' })
           } )
         }
       )
    }
    

    【讨论】:

    • 我遇到了这个问题(错误 [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client) 感谢您的回答
    猜你喜欢
    • 1970-01-01
    • 2017-09-26
    • 2015-05-12
    • 2016-02-11
    • 1970-01-01
    • 2010-12-24
    • 2021-10-30
    • 2020-05-16
    • 2016-07-29
    相关资源
    最近更新 更多