UNPKG

pdf-lib

Version:

Create and modify PDF files with JavaScript

95 lines 3.6 kB
import { __awaiter, __generator } from "tslib"; import PDFDocument from "./PDFDocument"; import { JpegEmbedder, PDFRef, PngEmbedder } from "../core"; import { assertIs } from "../utils"; /** * Represents an image that has been embedded in a [[PDFDocument]]. */ var PDFImage = /** @class */ (function () { function PDFImage(ref, doc, embedder) { this.alreadyEmbedded = false; assertIs(ref, 'ref', [[PDFRef, 'PDFRef']]); assertIs(doc, 'doc', [[PDFDocument, 'PDFDocument']]); assertIs(embedder, 'embedder', [ [JpegEmbedder, 'JpegEmbedder'], [PngEmbedder, 'PngEmbedder'], ]); this.ref = ref; this.doc = doc; this.width = embedder.width; this.height = embedder.height; this.embedder = embedder; } /** * Compute the width and height of this image after being scaled by the * given `factor`. For example: * ```js * image.width // => 500 * image.height // => 250 * * const scaled = image.scale(0.5) * scaled.width // => 250 * scaled.height // => 125 * ``` * This operation is often useful before drawing an image with * [[PDFPage.drawImage]] to compute the `width` and `height` options. * @param factor The factor by which this image should be scaled. * @returns The width and height of the image after being scaled. */ PDFImage.prototype.scale = function (factor) { assertIs(factor, 'factor', ['number']); return { width: this.width * factor, height: this.height * factor }; }; /** * Get the width and height of this image. For example: * ```js * const { width, height } = image.size() * ``` * @returns The width and height of the image. */ PDFImage.prototype.size = function () { return this.scale(1); }; /** * > **NOTE:** You probably don't need to call this method directly. The * > [[PDFDocument.save]] and [[PDFDocument.saveAsBase64]] methods will * > automatically ensure all images get embedded. * * Embed this image in its document. * * @returns Resolves when the embedding is complete. */ PDFImage.prototype.embed = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: if (!!this.alreadyEmbedded) return [3 /*break*/, 2]; return [4 /*yield*/, this.embedder.embedIntoContext(this.doc.context, this.ref)]; case 1: _a.sent(); this.alreadyEmbedded = true; _a.label = 2; case 2: return [2 /*return*/]; } }); }); }; /** * > **NOTE:** You probably don't want to call this method directly. Instead, * > consider using the [[PDFDocument.embedPng]] and [[PDFDocument.embedJpg]] * > methods, which will create instances of [[PDFImage]] for you. * * Create an instance of [[PDFImage]] from an existing ref and embedder * * @param ref The unique reference for this image. * @param doc The document to which the image will belong. * @param embedder The embedder that will be used to embed the image. */ PDFImage.of = function (ref, doc, embedder) { return new PDFImage(ref, doc, embedder); }; return PDFImage; }()); export default PDFImage; //# sourceMappingURL=PDFImage.js.map