使用 PhantomJS 渲染当前页面

Rendering Current Page With PhantomJS

我正在使用 MERN 堆栈(Express、Node)构建一个分析仪表板,需要强调的是重要事项。

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

鉴于您需要登录才能查看自己的分析,我不能只是从命令行 运行 幻影并将其传递到其中一个仪表板页面的 URL,因为它需要登录并进行查询。

是否可以用 phantomJS 做到这一点?

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

示例:

[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);

当用户点击 "button" 时,您可以根据应用程序的架构验证用户。

[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。

P.S。正如朋友之前在评论中所说,如果您能给我们更多信息以了解您的目标,那就太好了。