Comparing version 0.2.0 to 0.2.1
@@ -27,5 +27,6 @@ var PDFString = require('../objects/string') | ||
Text.prototype.text = function text(str, opts) { | ||
if (!str) return this.textFn | ||
opts = utils.extend(opts || {}, this.opts) | ||
var self = this, font = (opts.font ? this.doc.registerFont(opts.font) : this.doc.defaultFont).fromOpts(opts) | ||
var words = str.replace(/\r\n/, '\n').split(/ +|^|$/mg) | ||
var words = str.toString().replace(/\r\n/, '\n').split(/ +|^|$/mg) | ||
// if (words[0].match(/\.\!\?\,/) && this.contents.length) | ||
@@ -32,0 +33,0 @@ // this.contents[this.contents.length - 1].content += words.shift() |
@@ -27,3 +27,3 @@ var PDFObject = require('./objects/object') | ||
this.pages = new Pages(this) | ||
this.catalog.addProperty('Pages', this.pages.toReference()) | ||
this.catalog.prop('Pages', this.pages.toReference()) | ||
@@ -103,2 +103,6 @@ this.areas = { header: null, footer: null } | ||
var PDFDictionary = require('./objects/dictionary') | ||
, PDFArray = require('./objects/array') | ||
, PDFString = require('./objects/string') | ||
Document.prototype.toString = function() { | ||
@@ -142,7 +146,10 @@ var self = this | ||
// trailer | ||
var id = (new PDFString(uuid4())).toHexString() | ||
, trailer = new PDFDictionary({ | ||
Size: (this.objects.length + 1), | ||
Root: this.catalog.toReference(), | ||
ID: new PDFArray([id, id]) | ||
}) | ||
buf += 'trailer\n' | ||
buf += '<<\n' | ||
buf += '\t/Size ' + (this.objects.length + 1) + '\n' | ||
buf += '\t/Root ' + this.catalog.toReference().toString() + '\n' | ||
buf += '>>\n' | ||
buf += trailer.toString() + '\n' | ||
buf += 'startxref\n' | ||
@@ -230,2 +237,7 @@ buf += startxref + '\n' | ||
var c = string.charCodeAt(n); | ||
// workaround to not encode UTF8 characters | ||
// TODO: improve ... | ||
utftext += String.fromCharCode(Math.min(c, 0xff)) | ||
continue | ||
@@ -248,2 +260,26 @@ if (c < 128) { | ||
} | ||
} | ||
// UUID v4 | ||
// source: https://gist.github.com/jed/982883 | ||
function uuid4( | ||
a // placeholder | ||
){ | ||
return a // if the placeholder was passed, return | ||
? ( // a random number from 0 to 15 | ||
a ^ // unless b is 8, | ||
Math.random() // in which case | ||
* 16 // a random number from | ||
>> a/4 // 8 to 11 | ||
).toString(16) // in hexadecimal | ||
: ( // or otherwise a concatenated string: | ||
[1e7] + // 10000000 + | ||
-1e3 + // -1000 + | ||
-4e3 + // -4000 + | ||
-8e3 + // -80000000 + | ||
-1e11 // -100000000000, | ||
).replace( // replacing | ||
/[018]/g, // zeroes, ones, and eights with | ||
uuid4 // random hex digits | ||
) | ||
} |
@@ -5,11 +5,23 @@ var PDFName = module.exports = function(name) { | ||
if (name instanceof PDFName) return name | ||
name = name.toString() | ||
// white-space characters are not allowed | ||
if (name.match(/[\x00\x09\x0A\x0C\x0D\x20]/)) | ||
throw new Error('A Name mustn\'t contain white-space characters') | ||
if (name.match(/[\x00]/)) | ||
throw new Error('A Name mustn\'t contain the null characters') | ||
// delimiter characters are not allowed | ||
if (name.match(/[\(\)<>\[\]\{\}\/\%]/)) | ||
throw new Error('A Name mustn\'t contain delimiter characters') | ||
name = name.toString() | ||
// Beginning with PDF 1.2, any character except null (character code 0) | ||
// may be included in a name by writing its 2-digit hexadecimal code, | ||
// preceded by the number sign character (#) | ||
// ... it is recommended but not required for characters whose codes | ||
// are outside the range 33 (!) to 126 (~) | ||
name = name.replace(/[^\x21-\x7e]/g, function(c) { | ||
var code = c.charCodeAt(0) | ||
// replace unicode characters with `_` | ||
if (code > 0xff) code = 0x5f | ||
return '#' + code | ||
}) | ||
@@ -16,0 +28,0 @@ this.name = name |
@@ -23,3 +23,3 @@ var PDFString = module.exports = function(str) { | ||
PDFString.prototype.toString = function() { | ||
return this.toHexString() | ||
return this.toLiteralString() | ||
} |
@@ -7,4 +7,6 @@ { | ||
}, | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"homepage": "https://github.com/rkusa/pdfjs", | ||
"keywords": ["pdf", "generator"], | ||
"license": "MIT", | ||
"main": "lib/document", | ||
@@ -11,0 +13,0 @@ "scripts": { |
@@ -6,3 +6,3 @@ # pdfjs | ||
{ "name": "pdfjs", | ||
"version": "0.2.0" } | ||
"version": "0.2.1" } | ||
``` | ||
@@ -168,3 +168,3 @@ | ||
var fs = require('fs') | ||
fs.writeFile(__dirname + '/test.pdf', doc.toString()) | ||
fs.writeFile(__dirname + '/test.pdf', doc.toString(), 'ascii') | ||
``` | ||
@@ -171,0 +171,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
183485
3914