- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
实际上,我正在使用nodeJS中的phantom包生成pdf报告。我发现我们可以使用 phantom.callback 方法来设置它。但是我有一个问题,当这个回调返回简单文本时它工作正常,但是当我尝试使用使用闭包和jade引擎生成html的复杂函数时,我在幻像输出中出现错误,说明jade变量未定义,我认为这个问题发生的原因是在子幻影进程上下文中提到了有关工作的回调,因此在该回调中我的代码中定义的所有变量都不起作用。那么,我该如何解决这个问题呢?也许你知道更好的 phantomJS 包装器可以做这些事情?我使用这个包 phantom": "0.7.x"
//there I define all varaibles (jade, fs, etc., so I am sure that they are correct)
function generatePage(_page, reportConfig, phantom, html, report) {
_page.set('viewportSize', reportConfig.viewportSize);
var config = _.extend(reportConfig.paperSize, {
header: {
height: "2cm",
//contents: phantom.callback(headerCallback)
contents: phantom.callback(function (pageNum, numPages) {
var fn = jade.compile(headerTemplate); //Jade in undefined in phantom stdout
var templateData = _.extend({ currentPage: pageNum, numberPages: numPages }, report);
var generatedHtml = fn(templateData);
return "<h1>HEADER</h1><br />" /*+ generatedHtml*/;
})
}
, footer: {
height: "1cm",
contents: phantom.callback(function (pageNum, numPages) {
return "<p>Page " + pageNum + " of " + numPages + "</p>"; //WORKS fine
})
}
}
);
_page.set('paperSize', config);
_page.setContent(html);
return _page;
}
最佳答案
幻像回调不会以这种方式工作,您作为回调发送的函数将被字符串化并在幻像上下文中重新编译,您的依赖项将未知。
这是一个迟到的答案,但我没有在野外找到类似的东西。所以也许会帮助其他人。
您必须在定义 jade 和其他内容之后立即从页面上下文中生成 html,并将结果编译到您将作为回调发送的函数中:
//there I define all varaibles (jade, fs, etc., so I am sure that they are correct)
var fn = jade.compile(headerTemplate); //Jade is undefined in phantom context
var templateData = report;
var generatedHtml = fn(templateData);
//You can use some patterns in your template for pageNum and numPages like #pageNum# and replace them after compilation.
//here you are compiling the result into a function which you will send
//as callback(you have to remove all spaces and all breaklines to avoid compilation errors)
var headerCallbak = 'function(pageNum, numPages) { var x = \''+ generatedHtml .trim().replace(/(\r\n|\n|\r)/gm,"") +'\'; return x.relpace("#pageNumber#", pageNum).replace("#numPages#",numPages);}';
function generatePage(_page, reportConfig, phantom, html, report) {
_page.set('viewportSize', reportConfig.viewportSize);
var config = _.extend(reportConfig.paperSize, {
header: {
height: "2cm",
contents: phantom.callback(headerCallbak)
}
, footer: {
height: "1cm",
contents: phantom.callback(function (pageNum, numPages) {
return "<p>Page " + pageNum + " of " + numPages + "</p>"; //WORKS fine
})
}
}
);
_page.set('paperSize', config);
_page.setContent(html);
return _page;
}
关于javascript - 如何在nodeJS中使用phantom包设置自定义页眉和页脚内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27044459/
我想将页 footer 分放在文档的每一页上,除了第一页。 我通过扩展 TCPPDF 类并覆盖其页脚方法来创建我的自定义页脚。 然后根据文档并使用以下示例,我明白我应该使用 SetPrintHeade
我的页面分为 3 个部分。 页眉 主要 和页脚。 header 固定在顶部,大小为 109px,边框高 6px,因此 main 到顶部的边距为 109px。我希望 main 扩展到 heade
我正在构建一个网站,其中每个页面都有一个 , 和一个 . 据我了解,应用程序的入口点应包括这三个组件,并且应根据用户所在的路线呈现一个或多个附加组件。 我已经像这样构建了我的入口点: App.js
谁能告诉我为什么Footer出现在页面顶部,而我看不到Header或LayoutDiv9? 这是我添加的第二个 CSS 表,第一个是自动添加的 Fluid Grid Layout CSS 表,我根本没
我的应用程序中有这个页面: http://actibities-uniongr.rhcloud.com/pages/view-demo 如果您将浏览器的窗口缩短到小于 1000 像素并水平滚动,您会看
这个问题已经有答案了: Python command line interaction library? (3 个回答) 已关闭10 年前。 如何创建一个包含静态页眉和页脚的 Python 脚本,如下
我正在努力为我们的 Mediboard 元素提供完整的 HTML 页眉/页脚打印功能。 从长远来看,我知道 CSS3 页面媒体模块将满足我的需求,但这至少需要两三年时间。 所以我试着让它与 CSS2
我可以在每页上打印页眉,但我不熟悉打印页边距。我认为 @page css 可以工作,但它似乎不会影响页边距。如果我在正文上设置边距,它在第一页上有效,但后续页面默认从上边距开始,将页眉放在文本顶部。
有什么办法,就像 WordPress 一样,创建 header.php、theme-file.php 和 footer.php,然后使用钩子(Hook)将它们组合起来?对我来说仍然复制皮肤文件似乎
我正在努力解决以下问题。我有一个 UICollectionView,它的布局的页眉和页脚引用大小设置为一个值(比如 40.0)。当我删除/插入单元格并且集合 View 位于页眉和页脚不在屏幕上的位置时
我是 javascript 新手。我想使用 ajax 加载我的网站主模板文件(页眉、页脚、侧边栏)。我的布局文件如下所示。 布局.php header(); $ret .= $app->side
这是我的代码。内容图像曾短暂地工作,但现在消失了。 试图让Header只出现在页面顶部,图片高度为1000px。页面底部的页脚,图像高度为 1000px。内容图像需要重复以适合内容。这是一个博客,所以
我正在尝试将以下图像添加为网站内容的背景: http://webbos.co/vibration/wp-content/themes/vibration-child-theme/images/back
如何更改样式表中的页眉/页脚文本颜色? 我试过了,没有结果: footer: { parent: normal, alignment: TA_CENTER, textColor: red
我有以下 HTML,它有一个页眉、一个可滚动的内容和一个页脚,并且工作正常。 我想添加到此 HTML 的是一个包装所有三个 div 的表单,问题是一旦我用标签包装 div,布局就会中断。有什么建议吗?
我使用 jQuery 创建了具有不同状态的页眉和页脚导航。单击页脚导航时,我在更新页眉导航时遇到问题。例如,当用户单击页脚导航中的列表 1 时,我希望页眉导航也更新为相同的选择。我包含了一个 jsfi
我需要创建一个网页,其中包含正常页眉 + 粘性页脚 + 垂直拉伸(stretch)的流体内容。但是,这些位置不能是绝对的,因为我需要 780 像素的页面最小高度。 我设法使用表格完成了此操作,但我真的
我知道这可能是一件非常简单的事情,但除了很棒的 Stack Overflow 社区之外,我不知道有谁可以帮助我解决我的问题。我已经有一段时间没有在这种程度上使用 HTML/CSS 了。 我正在尝试为我
我前段时间做了一个wordpress网站。我今天刚刚看了一眼,由于某种原因,这个标志已经向东传播了。它已经厌倦了 clearfix 的 float 、文本对齐和边距:0 自动。谁能告诉我发生了什么,或
我需要帮助创建包含#header、#footer 和#main 的div 的代码。我只是不确定它在代码中的位置。我创建了一个带有 css 样式表的简单 html 页面。我并不是要任何人为我编写代码,因
我是一名优秀的程序员,十分优秀!