【问题标题】:how to transfer data object to html file using phantomjs?如何使用 phantomjs 将数据对象传输到 html 文件?
【发布时间】:2016-09-30 10:02:26
【问题描述】:

我在 phantom js 中运行这个程序

var ejs = require('ejs'),
    page = require('webpage').create();

page.open('./html/weekly.html', function (status) {
  if (status !== 'success') {
        phantom.exit();
    } else {
        window.setTimeout(function () {
            page.render('test4.pdf');
            phantom.exit();
        }, 1000); 
      }
});

此代码运行良好,它提供 test4.pdf 文件作为输出。

weekly.html 文件正在导入 data.js 文件

<script src="../data.js"></script>

data.js 文件提供了一个使用 jquery 在weekly.html 中导入的对象。

data.js

var data = {"hotel": {
  "info": {
    "hotelId": "h4456789",
    "levelId": "6",
    "locationId": "122",
    "aspireLevelId": "8",
    "hotelName": " xdfhjkhgfd",
    "hotelAddress": "delhi, India, 10035",
    "Rank": "#34 of 678 Hotels in New Delhi",
  }
}

编辑

我将数据作为对象获取,我想使用 phantomJs 将该数据放入 html 文件,然后将 html 文件转换为 pdf 文件。

现在我想通过phantomjs 导入该数据(data.js)。

如果不可能,还有什么办法?

【问题讨论】:

  • 你是什么意思dynamic data?你试试evaluate? [phantomjs.org/api/webpage/method/evaluate.html]
  • @stdob--动态数据意味着数据来自变量。
  • 不清楚。试着澄清你的问题。
  • 我编辑了问题
  • a) 这与 node.js 或 jQuery 有什么关系? PhantomJS 是一个独立的应用程序,您似乎正在使用没有桥接器的普通 PhantomJS。也许您应该删除这两个标签。 b)目前还不清楚您要做什么。对我来说,您似乎想以某种序列化形式在页面上包含 data JavaScript 对象,但您没有给出任何关于您期望哪种形式的指示。 page.evaluate(function(){ document.body.innerHTML = JSON.stringify(data); }); page.render('test5.pdf'); 的方向是否正确?

标签: javascript jquery node.js phantomjs


【解决方案1】:

你可以尝试在后台创建一个数据类,在weekly.html你可以使用@data DataClass,var data=@Html.Raw(Json.Encode(data));我不知道字符不符合要求,这是我理解的解决方案之一

【讨论】:

  • 你可以参考任何链接,我可以得到更多的细节
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-17
  • 2019-02-05
  • 2021-07-03
相关资源
最近更新 更多