diff --git a/demo/spotColor.pdf b/demo/spotColor.pdf new file mode 100644 index 00000000..3a1d217f Binary files /dev/null and b/demo/spotColor.pdf differ diff --git a/demo/testSpotColor.js b/demo/testSpotColor.js new file mode 100644 index 00000000..47824968 --- /dev/null +++ b/demo/testSpotColor.js @@ -0,0 +1,50 @@ +var PDFDocument = require('../'); + +var fs = require('fs'); + +// Create a new PDFDocument +var doc = new PDFDocument(); + +doc.pipe(fs.createWriteStream('spotColor.pdf')); + +// Set some meta data +doc.info['Title'] = 'Test Document for Sport Color'; + +doc.info['Author'] = 'Nicola A.'; + + +// Register a font name for use later +doc.registerFont('Palatino', 'fonts/PalatinoBold.ttf'); + +// Set the font, draw some text, and embed an image +doc + .font('Palatino') + .fontSize(18) + .text('Testing Spot Colors: MYFIRST_SPOT_COLOR, MYSECOND_SPOT_COLOR', 50, 50) + .fontSize(12) + .text('Check it out on Swatches palette', 50, 100) + +// Add another page +// doc +// .addPage() +// .fontSize(25) +// .text('Here is some vector graphics...', 100, 100); + +doc.addSpotColor('MYFIRST_SPOT_COLOR', 0, 50, 0, 0); +doc.addSpotColor('MYSECOND_SPOT_COLOR', 50, 0, 0, 0); + + +// Draw a triangle and a circle +doc + .moveTo(100, 150) + .lineTo(100, 250) + .lineTo(200, 250) + .stroke('MYFIRST_SPOT_COLOR'); + +doc + .moveTo(200, 250) + .lineTo(100, 150) + .lineTo(200, 150) + .lineTo(200, 250) + .fillAndStroke('MYFIRST_SPOT_COLOR', 'MYSECOND_SPOT_COLOR'); +doc.end(); diff --git a/lib/document.js b/lib/document.js index 310d3ebf..9671597e 100644 --- a/lib/document.js +++ b/lib/document.js @@ -328,6 +328,7 @@ Please pipe the document into a Node stream.\ _finalize() { // generate xref const xRefOffset = this._offset; + this._putSpotColors(); this._write('xref'); this._write(`0 ${this._offsets.length + 1}`); this._write('0000000000 65535 f '); diff --git a/lib/mixins/color.js b/lib/mixins/color.js index 318c8ab1..1eb02df3 100644 --- a/lib/mixins/color.js +++ b/lib/mixins/color.js @@ -5,6 +5,8 @@ const { PDFGradient, PDFLinearGradient, PDFRadialGradient } = Gradient; const { PDFTilingPattern } = pattern; export default { + spotColors: {}, + spotColorsCount: 0, initColor() { // The opacity dictionaries this._opacityRegistry = {}; @@ -38,6 +40,8 @@ export default { color = color.map(part => part / 100); } return color; + } else if (this.spotColors[color]) { + return color; } return null; @@ -48,6 +52,9 @@ export default { color.apply(stroke); return true; // see if tiling pattern, decode & apply it it + } else if (typeof color === 'string') { + this.addContent(`/CS${this.spotColors[color].index} ${stroke ? 'CS' : 'cs'} 1 ${op}`); + return false; } else if (Array.isArray(color) && color[0] instanceof PDFTilingPattern) { color[0].apply(stroke, color[1]); return true; @@ -163,6 +170,28 @@ export default { pattern(bbox, xStep, yStep, stream) { return new PDFTilingPattern(this, bbox, xStep, yStep, stream); + }, + + addSpotColor: function (name, C, M, Y, K) { + this._offsets.push(this._offset); + this.spotColors[name] = { + id: this._offsets.length, + index: ++this.spotColorsCount, + name: name, + values: [C, M, Y, K], + }; + }, + _putSpotColors: function () { + for (var item in this.spotColors) { + let output = ''; + item = this.spotColors[item]; + output += `${item.id} 0 obj \n`; + output += `[/Separation /${item.name} /DeviceCMYK << /Range [0 1 0 1 0 1 0 1] /C0 [0 0 0 0] /C1 [${ + item.values.map(value => value / 100.0).join(' ') + }] /FunctionType 2 /Domain [0 1] /N 1>>]`; + output += `\nendobj`; + this._write(output); + } } }; diff --git a/lib/object.js b/lib/object.js index e2ecc36f..327e3eed 100644 --- a/lib/object.js +++ b/lib/object.js @@ -40,7 +40,7 @@ class PDFObject { static convert(object, encryptFn = null) { // String literals are converted to the PDF name type if (typeof object === 'string') { - return `/${object}`; + return /^[\/\[\(\d].*/.test(object) ? object : '/' + object; // String objects are converted to PDF strings (UTF-16) } else if (object instanceof String) { diff --git a/lib/page.js b/lib/page.js index 32b8dc58..2d858425 100644 --- a/lib/page.js +++ b/lib/page.js @@ -157,6 +157,12 @@ class PDFPage { end() { this.dictionary.end(); + this.resources.data.ColorSpace = {}; + let i = 1; + for (let item in this.document.spotColors) { + item = this.document.spotColors[item]; + this.resources.data.ColorSpace[`CS${i++}`] = `${item.id} 0 R`; + } this.resources.end(); return this.content.end(); }