├── .babelrc ├── LICENSE ├── README.md ├── config.js ├── demo.gif ├── middleware.js ├── package.json ├── printer.js ├── router.js ├── server.js └── start.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015"] 3 | } 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Zach Latta 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software is furnished to do so, 8 | subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Fastprint 2 | 3 | Email a file to `print@yourdomain.com` to print it. 4 | 5 | ![](demo.gif) 6 | 7 | ## Setup 8 | 9 | There are three steps to get Fastprint working: 10 | 11 | 1. [Set up an OS X or Linux machine with a printer (Fastprint will run on this 12 | machine)](#machine-setup) 13 | 2. [Configure a Mailgun account to handle mail sent to 14 | `print@yourdomain.com`](#mailgun-setup) 15 | 3. [Start Fastprint](#start-fastprint) 16 | 17 | #### Machine Setup 18 | 19 | Start by configuring your computer with the printer you'd like to use, if it's 20 | not set up already. If you're on OS X or a large Linux distro (like Debian), 21 | you'll want to use your standard system preferences for setting this up. 22 | 23 | #### Mailgun Setup 24 | 25 | Next create a [Mailgun](https://mailgun.com) account and set it up with your 26 | domain of choice. I've configured mine with `mg.zachlatta.com`. 27 | 28 | Once you've done that, run the following command, replacing 29 | `YOUR_API_KEY`,`YOUR_DOMAIN_NAME`, and `FASTPRINT_URL` with appropriate values 30 | from the Mailgun dashboard (ex. `key-c0c122841822ef7681bf191cd0090f14`, 31 | `mg.zachlatta.com`, and `https://fastprint.apps.zachlatta.com`, respectively). 32 | This tells Mailgun to send any emails received at `print@yourdomain.com` to 33 | Fastprint to process. `FASTPRINT_URL` is the URL to your hosted Fastprint 34 | instance. You can use https://ngrok.com/ to expose an instance of Fastprint 35 | running in your local network to the world. 36 | 37 | $ curl -s --user 'api:YOUR_API_KEY' \ 38 | https://api.mailgun.net/v3/routes \ 39 | -F priority=0 \ 40 | -F description='Fastprint' \ 41 | -F expression='match_recipient("print@YOUR_DOMAIN_NAME")' \ 42 | -F action='forward("FASTPRINT_URL/receive")' \ 43 | -F action='stop()' 44 | 45 | #### Start Fastprint 46 | 47 | The final step is to start Fastprint itself. Go ahead and clone Fastprint, 48 | install dependencies, and then run `npm start`. 49 | 50 | $ git clone https://github.com/zachlatta/fastprint 51 | $ cd fastprint 52 | $ npm install 53 | $ npm start 54 | 55 | It'll error on the first run and complain about `PRINTER_NAME` not being set. 56 | It'll print a list of the available printer names. Set `PRINTER_NAME` to one of 57 | those printer names and then run `npm start` again. After that, you should be 58 | good to go! 59 | 60 | ## License 61 | 62 | Fastprint is licensed under the MIT license. See [LICENSE](LICENSE) for the full 63 | license. 64 | -------------------------------------------------------------------------------- /config.js: -------------------------------------------------------------------------------- 1 | export const PRINTER_NAME = process.env.PRINTER_NAME; 2 | -------------------------------------------------------------------------------- /demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zachlatta/fastprint/4c47f2fb8d6647a6dce34674498914283244fe7f/demo.gif -------------------------------------------------------------------------------- /middleware.js: -------------------------------------------------------------------------------- 1 | export function* xResponseTime(next) { 2 | let start = new Date; 3 | yield next; 4 | var ms = new Date - start; 5 | this.set('X-Response-Time', ms + 'ms'); 6 | }; 7 | 8 | export function* logger(next) { 9 | let start = new Date; 10 | yield next; 11 | var ms = new Date - start; 12 | console.log('%s %s - %sms', this.method, this.url, ms); 13 | }; 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fastprint", 3 | "version": "1.0.0", 4 | "description": "Email your printer to print things", 5 | "main": "start.js", 6 | "scripts": { 7 | "start": "node start.js", 8 | "dev": "./node_modules/.bin/nodemon", 9 | "test": "echo \"Error: no test specified\" && exit 1" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/zachlatta/fastprint.git" 14 | }, 15 | "author": "Zach Latta", 16 | "license": "MIT", 17 | "bugs": { 18 | "url": "https://github.com/zachlatta/fastprint/issues" 19 | }, 20 | "homepage": "https://github.com/zachlatta/fastprint#readme", 21 | "dependencies": { 22 | "babel-core": "^6.3.26", 23 | "babel-polyfill": "^6.3.14", 24 | "babel-preset-es2015": "^6.3.13", 25 | "koa": "^1.1.2", 26 | "koa-body": "^1.4.0", 27 | "koa-router": "^5.3.0", 28 | "printer": "^0.2.1" 29 | }, 30 | "devDependencies": { 31 | "nodemon": "^1.8.1" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /printer.js: -------------------------------------------------------------------------------- 1 | import printer from 'printer'; 2 | 3 | export function printFile(filename, printerName) { 4 | return new Promise((resolve, reject) => { 5 | printer.printFile({ 6 | filename: filename, 7 | printer: printerName, 8 | success: resolve, 9 | error: reject 10 | }); 11 | }); 12 | }; 13 | -------------------------------------------------------------------------------- /router.js: -------------------------------------------------------------------------------- 1 | import koaRouter from 'koa-router'; 2 | import koaBody from 'koa-body'; 3 | import * as config from './config.js'; 4 | import { printFile } from './printer.js'; 5 | 6 | let bodyParser = koaBody({multipart: true}); 7 | let router = koaRouter(); 8 | 9 | router.post('/receive', bodyParser, function *() { 10 | let body = this.request.body; 11 | 12 | if (!body.hasOwnProperty('files')) { 13 | console.error("No attachment found."); 14 | this.response.body = "Error, no attachment found."; 15 | this.response.status = 400; 16 | return; 17 | } 18 | 19 | for (let attachmentName in body.files) { 20 | let attachment = body.files[attachmentName]; 21 | 22 | printFile(attachment.path, config.PRINTER_NAME) 23 | .then(jobID => { 24 | let msg = `Printed ${attachment.name} with job id ${jobID}`; 25 | this.response.body = msg; 26 | console.log(msg); 27 | }) 28 | .catch(err => { 29 | this.response.status = 500; 30 | this.response.body = err.toString(); 31 | console.error(err); 32 | }); 33 | } 34 | }); 35 | 36 | export default router; 37 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | import koa from 'koa'; 4 | import router from './router.js'; 5 | import * as middleware from './middleware.js'; 6 | 7 | let app = koa(); 8 | 9 | app.use(middleware.xResponseTime); 10 | app.use(middleware.logger); 11 | app.use(router.routes()); 12 | 13 | let server = app.listen(process.env.PORT || 3000, () => { 14 | console.log(`Started on port ${server.address().port}`); 15 | }); 16 | 17 | export default app; 18 | -------------------------------------------------------------------------------- /start.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('babel-polyfill'); 4 | require('babel-register'); 5 | 6 | let config = require('./config.js'); 7 | let printer = require('printer'); 8 | 9 | if (!config.PRINTER_NAME) { 10 | let printers = printer.getPrinters(); 11 | 12 | console.error('Oh no! The PRINTER_NAME environment variable is required, ' + 13 | 'but not set. Please set it to one of the following values:'); 14 | console.error(); 15 | console.error(printers.map(p => `- ${p.name}`).join('\n')); 16 | 17 | process.exit(1); 18 | } 19 | 20 | require('./server.js'); 21 | --------------------------------------------------------------------------------