Permalink
Browse files

version 0.0.1

  • Loading branch information...
1 parent 9af59b0 commit a09126716e045da9796c5985c6bc93f95669f7dc @zweifisch committed Mar 4, 2013
Showing with 259 additions and 3 deletions.
  1. +4 −0 .gitignore
  2. +23 −3 README.md
  3. +12 −0 examples/matrix.coffee
  4. +23 −0 examples/matrix.js
  5. +17 −0 examples/matrix.tex
  6. +60 −0 index.coffee
  7. +99 −0 index.js
  8. +21 −0 package.json
View
@@ -0,0 +1,4 @@
+node_modules/
+*.pdf
+*.aux
+*.log
View
@@ -1,4 +1,24 @@
-node-pdf
-========
+# node-pdf
-pdf generation using mustache and xelatex
+pdf generation using mustache and xelatex
+
+## usage
+
+```sh
+npm install node-pdf
+```
+
+```coffeescript
+fs = require 'fs'
+{render} = require 'node-pdf'
+
+data =
+ title: 'Matrix'
+
+render './matrix.tex', data, (err,rs)->
+ if err
+ console.log err
+ else
+ ws = fs.createWriteStream './output.pdf'
+ rs.pipe ws
+```
View
@@ -0,0 +1,12 @@
+fs = require 'fs'
+{render} = require '../index'
+
+data =
+ title: 'Matrix'
+
+render './matrix.tex', data, (err,rs)->
+ if err
+ console.log err
+ else
+ ws = fs.createWriteStream './output.pdf'
+ rs.pipe ws
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -0,0 +1,17 @@
+\documentclass{article}
+\usepackage{amsmath}
+
+\begin{document}
+
+\title{ {{title}} }
+\maketitle
+
+$
+M = \begin{bmatrix}
+ \frac{5}{6} & \frac{1}{6} & 0 \\[0.3em]
+ \frac{5}{6} & 0 & \frac{1}{6} \\[0.3em]
+ 0 & \frac{5}{6} & \frac{1}{6}
+ \end{bmatrix}
+$
+
+\end{document}
View
@@ -0,0 +1,60 @@
+fs = require 'fs'
+path = require 'path'
+{spawn} = require 'child_process'
+{EventEmitter} = require 'events'
+temp = require 'temp'
+mu = require 'mu2'
+
+class XeLatex extends EventEmitter
+
+ constructor: (@outputDirectory)->
+
+ process :(file)->
+ xelatex = spawn 'xelatex', ['-interaction','nonstopmode','-output-directory',@outputDirectory,file]
+ xelatex.on 'exit', (code)=>
+ if code is 0
+ filename = path.basename file, '.tex'
+ @emit 'done',path.join @outputDirectory,"#{filename}.pdf"
+ else
+ @emit 'error',new Error "xelatex exits with #{code}"
+
+
+rmdir = (dir,callback) ->
+ fs.readdir dir,(err,files)->
+ if err
+ return callback err
+ removedFiles = 0
+ for filename in files
+ file = path.join dir, filename
+ fs.unlink file,->
+ removedFiles += 1
+ if removedFiles is files.length
+ fs.rmdir dir,callback
+
+
+render = (source,data,callback)->
+
+ rs = mu.compileAndRender source, data
+
+ temp.mkdir 'xelatex', (err, dirPath)->
+
+ ws = fs.createWriteStream path.join(dirPath, 'output.tex')
+ rs.pipe ws
+ ws.on 'close',->
+
+ xelatex = new XeLatex dirPath
+ xelatex.process path.join(dirPath, 'output.tex')
+
+ xelatex.on 'done', (path)->
+ readStream = fs.createReadStream path
+ readStream.on 'end',->
+ rmdir dirPath, ->
+ callback null,readStream
+
+ xelatex.on 'error', (err)->
+ callback err
+
+
+module.exports =
+ XeLatex: XeLatex
+ render: render
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -0,0 +1,21 @@
+{
+ "name": "node-pdf",
+ "version": "0.0.1",
+ "description": "pdf generation using mustache and xelatex",
+ "main": "index.js",
+ "scripts": {
+ "test": "echo \"Error: no test specified\" && exit 1"
+ },
+ "repository": "https://github.com/zweifisch/node-pdf.git",
+ "keywords": [
+ "pdf",
+ "latex",
+ "mustache"
+ ],
+ "author": "zf",
+ "license": "BSD",
+ "dependencies": {
+ "temp": "~0.5.0",
+ "mu2": "~0.5.17"
+ }
+}

0 comments on commit a091267

Please sign in to comment.