Use pdfkit instead of canvas

This commit is contained in:
ForbesLindesay 2015-07-21 11:31:27 +01:00
parent 09982436d5
commit e2fbb584c7
3 changed files with 17 additions and 15 deletions

View File

@ -2,18 +2,7 @@ var fs = require('fs');
var path = require('path');
var SVGO = require('svgo');
var dot = require('dot');
// Initialize what will be used for automatic text measurement.
var Canvas = require('canvas');
var canvasElement = new Canvas(0, 0); // Width and height are irrelevant.
var canvasContext = canvasElement.getContext('2d');
var CanvasFont = Canvas.Font;
try {
var opensans = new CanvasFont('Verdana',
path.join(__dirname, 'Verdana.ttf'));
canvasContext.addFont(opensans);
} catch(e) {}
canvasContext.font = '11px Verdana, "DejaVu Sans"';
var measureTextWidth = require('./measure-text');
// cache templates.
var templates = {};
@ -75,9 +64,9 @@ function makeImage(data, cb) {
data.logoPadding = 0;
}
data.widths = [
(canvasContext.measureText(data.text[0]).width|0) + 10
(measureTextWidth(data.text[0])|0) + 10
+ data.logoWidth + data.logoPadding,
(canvasContext.measureText(data.text[1]).width|0) + 10,
(measureTextWidth(data.text[1])|0) + 10,
];
if (data.links === undefined) {
data.links = ['', ''];

13
measure-text.js Normal file
View File

@ -0,0 +1,13 @@
'use strict';
var path = require('path');
var PDFDocument = require('pdfkit');
var doc = (new PDFDocument({size:'A4', layout:'landscape'}))
.font(path.join(__dirname, 'Verdana.ttf'))
.fontSize(11);
module.exports = measure;
function measure(str) {
return doc.widthOfString(str);
}

View File

@ -18,7 +18,7 @@
"dependencies": {
"dot": "~1.0.3",
"svgo": "~0.5.1",
"canvas": "~1.1.2",
"pdfkit": "~0.7.1",
"phantomjs": "~1.9.2-6",
"es6-promise": "~2.1.0",
"request": "~2.55.0",