Skip to content

Commit

Permalink
tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-last committed Sep 2, 2016
1 parent 08c6ec8 commit 7ca724b
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 45 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Convert SVG `rects` to a single path string.

## Motivation

I needed to generate PDF documents for printing onto labels media. Some of the labels need barcodes, and I wanted everything to be vector, no bitmaps. I decided to use [PDF Kit](http://pdfkit.org/docs/vector.html#svg_paths) which can use SVG path strings to draw vector shapes. So I needed a way to generate an SVG barcode and get an SVG path string to feed to `.path()`.
I needed to generate PDF documents for printing onto labels media. Some of the labels need barcodes, and I wanted everything to be vector, no bitmaps. I decided to use [PDF Kit](http://pdfkit.org/docs/vector.html#svg_paths) which can use SVG path strings to draw vector shapes. So I needed a way to generate an SVG barcode and get an SVG path string to feed to PDF Kit's `.path()` method.

Only one dependency: [xmldom](https://github.com/jindw/xmldom).

Expand All @@ -33,6 +33,7 @@ console.log(path) // M 10.000 10.000 L 14.000 10.000 L 14.000 38.000 L 10.000
### With Bardcode & PDFKit

```javascript
import fs from 'fs'
import bardcode from 'bardcode'
import PDFKit from 'pdfkit'
import srtps from 'srtps'
Expand Down
Binary file removed barcodes.pdf
Binary file not shown.
4 changes: 2 additions & 2 deletions dist/srtps.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function getRectPaths(rects) {
}

/**
* rects converted to string paths (exported/public)
* rects converted to string paths (public)
*/
function rectsToPath(svgString) {
var doc = new _xmldom2.default.DOMParser().parseFromString(svgString, 'text/xml');
Expand All @@ -89,4 +89,4 @@ function rectsToPath(svgString) {
return getRectPaths(rects);
}

exports.default = { rectsToPath: rectsToPath };
exports.default = { rectsToPath: rectsToPath };
23 changes: 0 additions & 23 deletions gruntfile.js

This file was deleted.

12 changes: 12 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

var gulp = require('gulp')
var babel = require('gulp-babel')

// es6
gulp.task('compile', function () {
return gulp.src('src/srtps.js')
.pipe(babel({ presets: ['es2015'] }))
.pipe(gulp.dest('dist'))
})

gulp.task('default', ['compile'])
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"main": "./dist/srtps.js",
"author": "Stephen Last <[email protected]>",
"scripts": {
"test": "node test.js",
"testbasic": "node test-basic.js"
"test": "node test/test.js",
"testbasic": "node test/test-basic.js"
},
"contributors": [
{
Expand All @@ -26,9 +26,8 @@
"devDependencies": {
"babel-preset-es2015": "^6.14.0",
"bardcode": "^1.0.0",
"grunt": "^1.0.1",
"grunt-babel": "^6.0.0",
"load-grunt-tasks": "^3.5.2",
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"pdfkit": "^0.8.0"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/srtps.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function getRectPaths (rects) {
}

/**
* rects converted to string paths (exported/public)
* rects converted to string paths (public)
*/
export function rectsToPath (svgString) {
const doc = new dom.DOMParser().parseFromString(svgString, 'text/xml')
Expand Down
Binary file added test/barcodes.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion test-basic.js → test/test-basic.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

var srtps = require('./dist/srtps')
var srtps = require('../dist/srtps')

var svg = '<svg><rect width=\'4\' height=\'28\' x=\'10\' y=\'10\' /></svg>'
var path = srtps.rectsToPath(svg)
Expand Down
19 changes: 7 additions & 12 deletions test.js → test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ var fs = require('fs')
var bardcode = require('bardcode')
var PDFKit = require('pdfkit')

var srtps = require('./dist/srtps')
var srtps = require('../dist/srtps')

/**
* multiplier for converting mm to 72 ppi (72 / 25.4) (1 inch = 25.4 mm)
* pdf kit creates pdf docs at 72 ppi
*/
function mm (num) {
var multiplier = 2.834645669291339
return num * multiplier
return num * 2.834645669291339
}

/**
Expand All @@ -25,27 +24,23 @@ function svgPath (str, h) {
quietZoneSize: 0 // number of moduleWidths in quiet zone on either side. defaults to 10.
}
var svg = bardcode.drawBarcode('svg', str, options)
var path = srtps.rectsToPath(svg)
return path
return srtps.rectsToPath(svg)
}

/**
* write a barcode to the pdf doc
*/
function barcode (pdf, barcode, h, x, y) {
pdf.save()
pdf.translate(mm(x), mm(y))
pdf.path(svgPath(barcode, h)).fill('#000')
pdf.translate(mm(x), mm(y)).path(svgPath(barcode, h)).fill('#000')
pdf.text(barcode, mm(0), (mm(h) + mm(2)), { align: 'left' })
pdf.restore()
}

// generate pdf
var pdf = new PDFKit()
pdf.pipe(fs.createWriteStream('./barcodes.pdf'))
pdf.pipe(fs.createWriteStream('./test/barcodes.pdf'))
barcode(pdf, '12345', 10, 10, 10)
barcode(pdf, '3946', 10, 10, 30)
barcode(pdf, 'hello', 10, 10, 50)
barcode(pdf, 'scan-me', 10, 10, 70)
barcode(pdf, '.PL46521', 10, 10, 90)
barcode(pdf, 'hello', 10, 10, 30)
barcode(pdf, 'scan-me', 10, 10, 50)
pdf.end()

0 comments on commit 7ca724b

Please sign in to comment.