渲染当前页面PhantomJS

问题描述 投票:0回答:1

我建立使用MERN堆栈(快递,节点)是重要的事情要突出一个分析仪表板。

作为一个破折号视图的一部分,我试图找出是否有可能引发PhantomJS调用创建使用页面本身上的按钮PDF格式的报告。

鉴于您需要登录才能看到自己的分析,我不能只在命令行中运行的幽灵,因为它需要登录和查询进行传递在仪表板的页面之一的URL。

是否有可能与phantomJS做到这一点?

node.js express phantomjs mern
1个回答
0
投票

如果我理解正确你的问题。

例:

[main.js]

  const dashboardToPdfCtrl = require("./controllers/phantom/pdf");  
  router.route("/api/dashboard/phantom").post(dashboardToPdfCtrl.createPdf);   
  router.route("/api/dashboard/phantom/html")
     .post(dashboardToPdfCtrl.createDashboard);

当用户点击“按钮”,您可以根据您的应用程序的架构验证用户。

“Pdf.js”

 exports.createPdf= async (req, res) => {
            if (!req.user || !req.user.sub) {
        return res
          .status(401)
          .send({ message: 'No authorization token was found' });
      }
          const instance = await phantom.create();
          const page = await instance.createPage();

         const settings = {
            operation: "POST",
            encoding: "utf8",
            headers: {
              "Content-Type": "application/json"
            },
            data: JSON.stringify({
              user: req.body.userId,
              anyDataYouNeedToRender: req.body.anyDataYouNeedToRender
            })
          };
    //POST request to /api/dashboard/phantom/html
            await page.open(
            `${process.env.HOST}:${
              process.env.PORT
            }/api/dashboard/phantom/html`,
            settings
          );
    //Save the content of /public/dashboard/dashboard.html with received data to pdf
          const pageSaved = await page.render(
            path.resolve(`./public/dashboard/file.pdf`)
          );

          if (pageSaved) await instance.exit();
        }

        exports.createDashboard = (req, res) => {
          res.render(
            path.resolve("./public/dashboard/dashboard.html"),
            { user: req.body.user,
              anyDataYouNeedToRender: req.body:anyDataYouNeedToRender
            }
          );
        };

难道这就是你要找的人?我想帮你,随便问detalization。

附:正如网友在评论前告知,如果你给我们更多的信息,understend你的目标将是巨大的。

© www.soinside.com 2019 - 2024. All rights reserved.