├── .gitignore ├── .travis.yml ├── README.md ├── examples └── simple.js ├── package.json └── server.js /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.iml 3 | node_modules 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '7' 4 | dist: trusty 5 | sudo: false 6 | before_install: 7 | - npm i express 8 | script: 9 | - npm test 10 | deploy: 11 | provider: npm 12 | email: niklasvh@gmail.com 13 | api_key: 14 | secure: PvsSbjQuT+tnWKAjvCfAQ8bfpB9Yv65/k28UZZcu4h0vH4qb/QWO807jXJru8TXN45QvPjVzeGMgQdhsk7LGqBndq6ooorhOSRymk96zLWHaha/jDlKmBH7g47jgtB6GlAsaHnv2bSUsEshYsWkNFLn12/+vSKUZR1/vm46CnoI= 15 | on: 16 | tags: true 17 | branch: master 18 | repo: niklasvh/html2canvas-proxy-nodejs 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | html2canvas-proxy 2 | ================= 3 | 4 | [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/niklasvh/html2canvas?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) 5 | [![Build Status](https://travis-ci.org/niklasvh/html2canvas-proxy-nodejs.svg)](https://travis-ci.org/niklasvh/html2canvas-proxy-nodejs) 6 | [![NPM Downloads](https://img.shields.io/npm/dm/html2canvas-proxy.svg)](https://www.npmjs.org/package/html2canvas-proxy) 7 | [![NPM Version](https://img.shields.io/npm/v/html2canvas-proxy.svg)](https://www.npmjs.org/package/html2canvas-proxy) 8 | 9 | #### Express middleware proxy for html2canvas #### 10 | 11 | This library provides proxy middleware using express for [html2canvas](https://github.com/niklasvh/html2canvas). 12 | 13 | For html2canvas >=v1.0.0 use >= v1.0.0 of this library. 14 | 15 | #### Install #### 16 | 17 | npm install html2canvas-proxy --save 18 | 19 | #### Example #### 20 | 21 | var proxy = require('html2canvas-proxy'); 22 | var express = require('express'); 23 | 24 | var app = express(); 25 | app.use('/', proxy()); 26 | 27 | -------------------------------------------------------------------------------- /examples/simple.js: -------------------------------------------------------------------------------- 1 | var proxy = require("../server.js"); 2 | var express = require('express'); 3 | 4 | var app = express(); 5 | var port = (process.env.PORT || 3000); 6 | 7 | app.use("/", proxy()); 8 | 9 | console.log("Server running on port", port); 10 | app.listen(port); 11 | 12 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "html2canvas-proxy", 3 | "version": "1.0.1", 4 | "description": "Cross-origin content proxy for html2canvas", 5 | "main": "server.js", 6 | "scripts": { 7 | "test": "exit 0" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/niklasvh/html2canvas-proxy-nodejs.git" 12 | }, 13 | "keywords": [ 14 | "html2canvas", 15 | "proxy" 16 | ], 17 | "author": "Niklas von Hertzen", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/niklasvh/html2canvas-proxy-nodejs/issues" 21 | }, 22 | "homepage": "https://github.com/niklasvh/html2canvas-proxy-nodejs", 23 | "dependencies": { 24 | "cors": "2.8.4", 25 | "request": "2.87.0" 26 | }, 27 | "peerDependencies": { 28 | "express": "4.x" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const url = require('url'); 3 | const cors = require('cors'); 4 | const request = require('request'); 5 | 6 | function validUrl(req, res, next) { 7 | if (!req.query.url) { 8 | next(new Error('No url specified')); 9 | } else if (typeof req.query.url !== 'string' || url.parse(req.query.url).host === null) { 10 | next(new Error(`Invalid url specified: ${req.query.url}`)); 11 | } else { 12 | next(); 13 | } 14 | } 15 | 16 | module.exports = () => { 17 | const app = express.Router(); 18 | app.get('/', cors(), validUrl, (req, res, next) => { 19 | switch (req.query.responseType) { 20 | case 'blob': 21 | req.pipe(request(req.query.url).on('error', next)).pipe(res); 22 | break; 23 | case 'text': 24 | default: 25 | request({url: req.query.url, encoding: 'binary'}, (error, response, body) => { 26 | if (error) { 27 | return next(error); 28 | } 29 | res.send( 30 | `data:${response.headers['content-type']};base64,${Buffer.from( 31 | body, 32 | 'binary' 33 | ).toString('base64')}` 34 | ); 35 | }); 36 | } 37 | }); 38 | 39 | return app; 40 | }; 41 | --------------------------------------------------------------------------------