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的更多相关文章

  1. 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 ...

  2. react-router3.x hashHistory render两次的bug,及解决方案

    先写一个简单App页面,其实就是简单修改了react-router的官方例子中的animations例子,修改了两个地方: 1.路由方式由browserHistory修改为hashHistory 2. ...

  3. [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 ...

  4. 聊聊Vue.js的template编译

    写在前面 因为对Vue.js很感兴趣,而且平时工作的技术栈也是Vue.js,这几个月花了些时间研究学习了一下Vue.js源码,并做了总结与输出. 文章的原地址:https://github.com/a ...

  5. Django升级1.9.6出现的中文本地化bug

    Error日志: Error opening file for reading: Permission denied ERROR Internal Server Error: / Traceback  ...

  6. Django之template操作

    一.模板渲染的原理 (一)使用 模板渲染首先有一个模板对象Template,然后有一个上下文对象Context,通过render方法进行渲染,最后返回字符串,render方法的本质还是调用了HttpR ...

  7. JavaScript模板引擎artTemplate.js——template.compile()方法

    template.compile(source, options) source:必传,渲染模板的内容. options:可选,通常不传.(其实是我还没研究明白) return:一个渲染函数. 示例如 ...

  8. 使用Underscore.js的template将Backbone.js的js代码和html代码分离

    这段时间在学习Require.js和Backbone.js的过程中,发现有些项目里的HTML代码都是写在View的js代码里面的,渲染的时候需要对Collection进行循环,再将HTML代码拼接上去 ...

  9. template.compile()方法

    template.compile(source, options) source:必传,渲染模板的内容. options:可选,通常不传.(其实是我还没研究明白) return:一个渲染函数. 示例如 ...

随机推荐

  1. loj10003加工生产调度

    题目描述 某工厂收到了 n个产品的订单,这  个产品分别在 A.B 两个车间加工,并且必须先在 A 车间加工后才可以到 B 车间加工. 某个产品 i 在 A,B 两车间加工的时间分别为 A_i,B_i ...

  2. 最全面的图卷积网络GCN的理解和详细推导,都在这里了!

    目录 目录 1. 为什么会出现图卷积神经网络? 2. 图卷积网络的两种理解方式 2.1 vertex domain(spatial domain):顶点域(空间域) 2.2 spectral doma ...

  3. spark整合Phoenix相关案例

    spark 读取Phoenix hbase table表到 DataFrame的方式 Demo1: 方式一:spark read读取各数据库的通用方式 方式二:spark.load 方式三:phoen ...

  4. GJ项目技术代码相关总结

    第一次实习公司的GJ项目快要结束,自己总结了一些工作中的代码,留到记录学习. 根据下拉条件,进行查询,展示出不同的表单选项:并在鼠标进入到指定区域时显示部分内容,鼠标移出内容区域时,隐藏内容. 焦点移 ...

  5. linux下mysql基于mycat做主从复制和读写分离之基础篇

    Linux下mysql基于mycat实现主从复制和读写分离1.基础设施 两台虚拟机:172.20.79.232(主) 172.20.79.233(从) 1.1软件设施 mysql5.6.39 , my ...

  6. Flink-v1.12官方网站翻译-P006-Intro to the DataStream API

    DataStream API介绍 本次培训的重点是广泛地介绍DataStream API,使你能够开始编写流媒体应用程序. 哪些数据可以流化? Flink的DataStream APIs for Ja ...

  7. Flink-v1.12官方网站翻译-P004-Flink Operations Playground

    Flink操作训练场 在各种环境中部署和操作Apache Flink的方法有很多.无论这种多样性如何,Flink集群的基本构件保持不变,类似的操作原则也适用. 在这个操场上,你将学习如何管理和运行Fl ...

  8. Java链表(英雄增删查改)

    链表(Linked List)介绍 链表是有序的列表,但是它在内存中是存储如下 小结: 1.链表是以节点的方式来存储,是链式存储. 2.每个节点包含 data 域, next 域:指向下一个节点. 3 ...

  9. linux开发各种I/O操作简析,以及select、poll、epoll机制的对比

    作者:良知犹存 转载授权以及围观:欢迎添加微信公众号:羽林君 IO 概念区分 四个相关概念: 同步(Synchronous) 异步( Asynchronous) 阻塞( Blocking ) 非阻塞( ...

  10. Hiho1422 Harmonic Matrix Counter (高斯消元)

    16年北京站A题 真的难啊.. 题意: 定义和谐矩阵 就是每个元素和上下左右的xor值=0 输出一个超大数 然后最多800个询问 求字典序第k小的和谐矩阵 x y位置上的数 题解: 首先这个超大数的范 ...