UNPKG

pdfkit

Version:

A PDF generation library for Node.js

99 lines (95 loc) 3.27 kB
(function() { var PDFImage; var __hasProp = Object.prototype.hasOwnProperty, __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (__hasProp.call(this, i) && this[i] === item) return i; } return -1; }; PDFImage = require('../image'); module.exports = { initImages: function() { this._imageRegistry = {}; return this._imageCount = 0; }, image: function(src, x, y, options) { var bh, bp, bw, h, hp, image, ip, label, pages, w, wp, _ref, _ref2, _ref3, _ref4, _ref5; if (options == null) options = {}; if (typeof x === 'object') { options = x; x = null; } x = (_ref = x != null ? x : options.x) != null ? _ref : this.x; y = (_ref2 = y != null ? y : options.y) != null ? _ref2 : this.y; if (this._imageRegistry[src]) { _ref3 = this._imageRegistry[src], image = _ref3[0], label = _ref3[1], pages = _ref3[2]; if (_ref4 = this.page, __indexOf.call(pages, _ref4) < 0) { pages.push(this.page); } } else { image = PDFImage.open(src); label = "I" + (++this._imageCount); this._imageRegistry[src] = [image, label, [this.page]]; } w = options.width || image.width; h = options.height || image.height; if (options.width && !options.height) { wp = w / image.width; w = image.width * wp; h = image.height * wp; } else if (options.height && !options.width) { hp = h / image.height; w = image.width * hp; h = image.height * hp; } else if (options.scale) { w = image.width * options.scale; h = image.height * options.scale; } else if (options.fit) { _ref5 = options.fit, bw = _ref5[0], bh = _ref5[1]; bp = bw / bh; ip = image.width / image.height; if (ip > bp) { w = bw; h = bw / ip; } else { h = bh; w = bh * ip; } } if (this.y === y) this.y += h; y = this.page.height - y - h; this.save(); this.addContent("" + w + " 0 0 " + h + " " + x + " " + y + " cm"); this.addContent("/" + label + " Do"); this.restore(); return this; }, embedImages: function(fn) { var images, item, proceed, src; var _this = this; images = (function() { var _ref, _results; _ref = this._imageRegistry; _results = []; for (src in _ref) { item = _ref[src]; _results.push(item); } return _results; }).call(this); return (proceed = function() { var image, label, pages, _ref; if (images.length) { _ref = images.shift(), image = _ref[0], label = _ref[1], pages = _ref[2]; return image.object(_this, function(obj) { var page, _base, _i, _len, _ref2; for (_i = 0, _len = pages.length; _i < _len; _i++) { page = pages[_i]; if ((_ref2 = (_base = page.xobjects)[label]) == null) { _base[label] = obj; } } return proceed(); }); } else { return fn(); } })(); } }; }).call(this);