Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

node.js - how to integrate node-jasper with expressjs, knexjs and MySQL?

I am using Express JS and Knexjs to read data from MySQL database, I can load the data from MySQL but I cannot see the result, it gives an error:

Error: Failed to load PDF document

my code as follow:

const express = require("express");
const router = express.Router();
const Lodash = require("lodash");
const jasper = require("node-jasper")({
  path: "../lib/jasperreports-5.6.0",
  reports: {
    hw: {
      jasper: "../reports/Customer_report.jasper",
      jrxml: "../reports/Customer_report.jrxml",
      conn: "in_memory_json",
    },
  },
});

const model = require("../model/customersModel");

router.get("/", async (req, res, next) => {
  try {
    model.getAll().then((customers) => {
      // CustomerName, Address, CustomerContactName, Email, Telephone
      let myDataSet = [];
      Lodash.forEach(customers, (customers) => {
        myDataSet.push({
          CustomerName: customers.CustomerName,
          Address: customers.Address,
          CustomerContactName: customers.CustomerContactName,
          Email: customers.Email,
          Telephone: customers.Telephone,
        });
      });
      var report = {
        reports: "hw",
        dataset: myDataSet,
      };
      console.log(report); // here I can see the data
      var pdf = jasper.pdf(report);
      res.set({
        "Content-type": "application/pdf",
        "Content-Length": pdf.length,
      });
      res.send(pdf);
    });
  } catch (error) {
    next(error);
  }
});

module.exports = router;

I can console.log the dataset but on the browser it gives me an error.

Error: Failed to load PDF document

question from:https://stackoverflow.com/questions/65650349/how-to-integrate-node-jasper-with-expressjs-knexjs-and-mysql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...