puppeteer render local HTML template bug
puppeteer render local HTML template bug
➜ url-to-poster git:(master) ✗ dev
^-v-^ app is running in development env!
yarn run v1.16.0
$ node index.js
(node:2725) UnhandledPromiseRejectionWarning: Error: Protocol error (Page.navigate): Cannot navigate to invalid URL
at Promise (/Users/xgqfrms-mbp/Documents/GitHub/url-to-poster/node_modules/puppeteer/lib/Connection.js:183:56)
at new Promise (<anonymous>)
at CDPSession.send (/Users/xgqfrms-mbp/Documents/GitHub/url-to-poster/node_modules/puppeteer/lib/Connection.js:182:12)
at navigate (/Users/xgqfrms-mbp/Documents/GitHub/url-to-poster/node_modules/puppeteer/lib/FrameManager.js:118:39)
at FrameManager.navigateFrame (/Users/xgqfrms-mbp/Documents/GitHub/url-to-poster/node_modules/puppeteer/lib/FrameManager.js:95:7)
at Frame.goto (/Users/xgqfrms-mbp/Documents/GitHub/url-to-poster/node_modules/puppeteer/lib/FrameManager.js:406:37)
at Frame.<anonymous> (/Users/xgqfrms-mbp/Documents/GitHub/url-to-poster/node_modules/puppeteer/lib/helper.js:112:23)
at Page.goto (/Users/xgqfrms-mbp/Documents/GitHub/url-to-poster/node_modules/puppeteer/lib/Page.js:672:49)
at Page.<anonymous> (/Users/xgqfrms-mbp/Documents/GitHub/url-to-poster/node_modules/puppeteer/lib/helper.js:112:23)
at /Users/xgqfrms-mbp/Documents/GitHub/url-to-poster/index.js:30:14
-- ASYNC --
at Frame.<anonymous> (/Users/xgqfrms-mbp/Documents/GitHub/url-to-poster/node_modules/puppeteer/lib/helper.js:111:15)
at Page.goto (/Users/xgqfrms-mbp/Documents/GitHub/url-to-poster/node_modules/puppeteer/lib/Page.js:672:49)
at Page.<anonymous> (/Users/xgqfrms-mbp/Documents/GitHub/url-to-poster/node_modules/puppeteer/lib/helper.js:112:23)
at /Users/xgqfrms-mbp/Documents/GitHub/url-to-poster/index.js:30:14
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:2725) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:2725) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

solution
const Fs = require('fs')
const Path = require('path')
const Util = require('util')
const Puppeteer = require('puppeteer')
const Handlebars = require('handlebars')
const ReadFile = Util.promisify(Fs.readFile)
class Invoice {
async html() {
try {
const data = {
your: 'data'
}
const templatePath = Path.resolve('path', 'to', 'invoice.html')
const content = await ReadFile(templatePath, 'utf8')
// compile and render the template with handlebars
const template = Handlebars.compile(content)
return template(data)
} catch (error) {
throw new Error('Cannot create invoice HTML template.')
}
}
async pdf() {
const html = await this.html()
const browser = await Puppeteer.launch()
const page = await browser.newPage()
await page.setContent(html)
return page.pdf()
}
}
https://github.com/wildbit/postmark-templates/tree/master/templates/basic/invoice
handlebars.js
HTML template
https://github.com/wycats/handlebars.js
https://handlebarsjs.com/zh/guide/
var source = "<p>Hello, my name is {{name}}. I am from {{hometown}}. I have " +
"{{kids.length}} kids:</p>" +
"<ul>{{#kids}}<li>{{name}} is {{age}}</li>{{/kids}}</ul>";
var template = Handlebars.compile(source);
var data = { "name": "Alan", "hometown": "Somewhere, TX",
"kids": [{"name": "Jimmy", "age": "12"}, {"name": "Sally", "age": "4"}]};
var result = template(data);
// Would render:
// <p>Hello, my name is Alan. I am from Somewhere, TX. I have 2 kids:</p>
// <ul>
// <li>Jimmy is 12</li>
// <li>Sally is 4</li>
// </ul>
puppeteer render local HTML template bug的更多相关文章
- vue h render function & render select with options bug
vue h render function & render select with options bug https://github.com/xgqfrms/vue/issues/41 ...
- react-router3.x hashHistory render两次的bug,及解决方案
先写一个简单App页面,其实就是简单修改了react-router的官方例子中的animations例子,修改了两个地方: 1.路由方式由browserHistory修改为hashHistory 2. ...
- [Angular 2] Generate and Render Angular 2 Template Elements in a Component
Angular 2 Components have templates, but you can also create templates inside of your templates usin ...
- 聊聊Vue.js的template编译
写在前面 因为对Vue.js很感兴趣,而且平时工作的技术栈也是Vue.js,这几个月花了些时间研究学习了一下Vue.js源码,并做了总结与输出. 文章的原地址:https://github.com/a ...
- Django升级1.9.6出现的中文本地化bug
Error日志: Error opening file for reading: Permission denied ERROR Internal Server Error: / Traceback ...
- Django之template操作
一.模板渲染的原理 (一)使用 模板渲染首先有一个模板对象Template,然后有一个上下文对象Context,通过render方法进行渲染,最后返回字符串,render方法的本质还是调用了HttpR ...
- JavaScript模板引擎artTemplate.js——template.compile()方法
template.compile(source, options) source:必传,渲染模板的内容. options:可选,通常不传.(其实是我还没研究明白) return:一个渲染函数. 示例如 ...
- 使用Underscore.js的template将Backbone.js的js代码和html代码分离
这段时间在学习Require.js和Backbone.js的过程中,发现有些项目里的HTML代码都是写在View的js代码里面的,渲染的时候需要对Collection进行循环,再将HTML代码拼接上去 ...
- template.compile()方法
template.compile(source, options) source:必传,渲染模板的内容. options:可选,通常不传.(其实是我还没研究明白) return:一个渲染函数. 示例如 ...
随机推荐
- 【笔记】学习markdown
经过来自学长(姐?)的 嘲讽 善意提醒后,我才知道这个博客园好像 资瓷 markdown 于是我决定要认真学习markdown(绝不是因为洛谷题解又过不去了) 正常点: 由于没人教,我上网查了一下 一 ...
- Java三种IO模型和LinuxIO五种IO模型
Java: https://github.com/Snailclimb/JavaGuide/blob/master/docs/java/BIO-NIO-AIO.md https://github.co ...
- js基础(使用Canvas画图)
HTML5的元素提供了一组JavaScript API,让我们可以动态地创建图形和图像. 图形是在一个特定的上下文中创建的,而上下文对象目前有两种.第一种是2D上下文,可以执行原始的绘图操作, 比如: ...
- BT宝塔FTP连接不上用不了的解决办法,BT宝塔FTP轻松几步设置即可连接成功
BT宝塔这款linux服务器管理面板,相信现在很多站长都有使用,操作简便,傻瓜化,而且功能全面,什么301设置,SSL证书申请,网站源码一键安装等等,反正就是一句话,BT宝塔非常的好用,但是偶尔也会遇 ...
- cloudera manager server迁移
一.迁移背景 服务器出了问题,导致整个cm server界面呈现出不可用的状态,也就是获取不到各个大数据组件以及主机相关的状态的信息,整个cm server的前端界面处于瘫痪的状态,不可用,刚开始怀疑 ...
- centos 7 部署nginx及libfastcommon
1.编译环境 (centos)yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib- ...
- MySQL数据库迁移与MySQL数据库批量恢复
目录 一.MySQL数据库迁移或备份 1. 了解使用InnoDB引擎创建数据库所产生的文件 2. 迁移数据库步骤 1. 从A服务器迁移至B服务器 2. MySQL重装并导入之前数据库 二.MySQL数 ...
- AtCoder Beginner Contest 182 D - Wandering (前缀和)
题意:在\(x\)轴上,你刚开始在\(0\)的位置,第\(i\)次操作需要走\(A_1,...,A_i\)个单位,如果\(A_i\)为正向右走,否则向左走,求你所能走到的最大坐标. 题解:我们一步一步 ...
- HDU - 3282 优先队列的使用
题意: 按照顺序给你n个数,当数的数量是奇数的时候就输出它们的中位数 题解: 优先队列默认是大顶堆,即priority_queue.top()是这个队列中的最大值 那么我们就可以先创造一个大顶堆优先队 ...
- 数位dp整理 && 例题HDU - 2089 不要62 && 例题 HDU - 3555 Bomb
数位dp: 数位dp是一种计数用的dp,一般就是要统计一个区间[li,ri]内满足一些条件数的个数.所谓数位dp,字面意思就是在数位上进行dp.数位的含义:一个数有个位.十位.百位.千位......数 ...