Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 16 additions & 25 deletions src/api/PDFDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -975,10 +975,10 @@ export default class PDFDocument {
* @param options The options to be used when embedding the font.
* @returns Resolves with the embedded font.
*/
async embedFont(
embedFont(
font: StandardFonts | string | Uint8Array | ArrayBuffer,
options: EmbedFontOptions = {},
): Promise<PDFFont> {
): PDFFont {
const { subset = false, customName, features } = options;

assertIs(font, 'font', ['string', Uint8Array, ArrayBuffer]);
Expand All @@ -991,13 +991,8 @@ export default class PDFDocument {
const bytes = toUint8Array(font);
const fontkit = this.assertFontkit();
embedder = subset
? await CustomFontSubsetEmbedder.for(
fontkit,
bytes,
customName,
features,
)
: await CustomFontEmbedder.for(fontkit, bytes, customName, features);
? CustomFontSubsetEmbedder.for(fontkit, bytes, customName, features)
: CustomFontEmbedder.for(fontkit, bytes, customName, features);
} else {
throw new TypeError(
'`font` must be one of `StandardFonts | string | Uint8Array | ArrayBuffer`',
Expand Down Expand Up @@ -1067,10 +1062,10 @@ export default class PDFDocument {
* @param jpg The input data for a JPEG image.
* @returns Resolves with the embedded image.
*/
async embedJpg(jpg: string | Uint8Array | ArrayBuffer): Promise<PDFImage> {
embedJpg(jpg: string | Uint8Array | ArrayBuffer): PDFImage {
assertIs(jpg, 'jpg', ['string', Uint8Array, ArrayBuffer]);
const bytes = toUint8Array(jpg);
const embedder = await JpegEmbedder.for(bytes);
const embedder = JpegEmbedder.for(bytes);
const ref = this.context.nextRef();
const pdfImage = PDFImage.of(ref, this, embedder);
this.images.push(pdfImage);
Expand Down Expand Up @@ -1107,17 +1102,17 @@ export default class PDFDocument {
* @param png The input data for a PNG image.
* @returns Resolves with the embedded image.
*/
async embedPng(png: string | Uint8Array | ArrayBuffer): Promise<PDFImage> {
embedPng(png: string | Uint8Array | ArrayBuffer): PDFImage {
assertIs(png, 'png', ['string', Uint8Array, ArrayBuffer]);
const bytes = toUint8Array(png);
const embedder = await PngEmbedder.for(bytes);
const embedder = PngEmbedder.for(bytes);
const ref = this.context.nextRef();
const pdfImage = PDFImage.of(ref, this, embedder);
this.images.push(pdfImage);
return pdfImage;
}

async embedSvg(svg: string): Promise<PDFSvg> {
embedSvg(svg: string): PDFSvg {
if (!svg) return new PDFSvg(svg);
const parsedSvg = parseHtml(svg);
const findImages = (element: HTMLElement): HTMLElement[] => {
Expand All @@ -1133,17 +1128,13 @@ export default class PDFDocument {
const images = findImages(parsedSvg);
const imagesDict = {} as Record<string, PDFImage>;

await Promise.all(
images.map(async (image) => {
const href = image.attributes.href ?? image.attributes['xlink:href'];
if (!href || imagesDict[href]) return;
const isPng = href.match(/\.png(\?|$)|^data:image\/png;base64/gim);
const pdfImage = isPng
? await this.embedPng(href)
: await this.embedJpg(href);
imagesDict[href] = pdfImage;
}),
);
images.map(async (image) => {
const href = image.attributes.href ?? image.attributes['xlink:href'];
if (!href || imagesDict[href]) return;
const isPng = href.match(/\.png(\?|$)|^data:image\/png;base64/gim);
const pdfImage = isPng ? this.embedPng(href) : this.embedJpg(href);
imagesDict[href] = pdfImage;
});

return new PDFSvg(svg, imagesDict);
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/embedders/CustomFontEmbedder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import {
* https://github.com/devongovett/pdfkit/blob/e71edab0dd4657b5a767804ba86c94c58d01fbca/lib/image/jpeg.coffee
*/
class CustomFontEmbedder {
static async for(
static for(
fontkit: Fontkit,
fontData: Uint8Array,
customName?: string,
fontFeatures?: TypeFeatures,
) {
const font = await fontkit.create(fontData);
const font = fontkit.create(fontData);
return new CustomFontEmbedder(font, fontData, customName, fontFeatures);
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/embedders/CustomFontSubsetEmbedder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import { Cache, mergeUint8Arrays, toHexStringOfMinLength } from '../../utils';
* https://github.com/devongovett/pdfkit/blob/e71edab0dd4657b5a767804ba86c94c58d01fbca/lib/image/jpeg.coffee
*/
class CustomFontSubsetEmbedder extends CustomFontEmbedder {
static async for(
static for(
fontkit: Fontkit,
fontData: Uint8Array,
customFontName?: string,
fontFeatures?: TypeFeatures,
) {
const font = await fontkit.create(fontData);
const font = fontkit.create(fontData);
return new CustomFontSubsetEmbedder(
font,
fontData,
Expand Down
2 changes: 1 addition & 1 deletion src/core/embedders/JpegEmbedder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const ChannelToColorSpace: { [idx: number]: ColorSpace | undefined } = {
* https://github.com/foliojs/pdfkit/blob/a6af76467ce06bd6a2af4aa7271ccac9ff152a7d/lib/image/jpeg.js
*/
class JpegEmbedder {
static async for(imageData: Uint8Array) {
static for(imageData: Uint8Array) {
const dataView = new DataView(imageData.buffer);

const soi = dataView.getUint16(0);
Expand Down
2 changes: 1 addition & 1 deletion src/core/embedders/PngEmbedder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { PNG } from '../../utils/png';
* https://github.com/devongovett/pdfkit/blob/e71edab0dd4657b5a767804ba86c94c58d01fbca/lib/image/png.coffee
*/
class PngEmbedder {
static async for(imageData: Uint8Array) {
static for(imageData: Uint8Array) {
const png = PNG.load(imageData);
return new PngEmbedder(png);
}
Expand Down
Loading