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
3 changes: 2 additions & 1 deletion apps/node/tests/test1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
StandardFonts,
typedArrayFor,
AFRelationship,
Fontkit,
} from '../../..';

const ipsumLines = [
Expand All @@ -43,7 +44,7 @@ export default async (assets: Assets) => {
pdfDoc.setCreationDate(new Date('2018-06-24T01:58:37.228Z'));
pdfDoc.setModificationDate(new Date('2018-12-21T07:00:11.000Z'));

pdfDoc.registerFontkit(fontkit);
pdfDoc.registerFontkit(fontkit as Fontkit);

await pdfDoc.attach(assets.images.png.greyscale_bird, 'bird.png', {
mimeType: 'image/png',
Expand Down
3 changes: 2 additions & 1 deletion apps/node/tests/test11.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
PDFFont,
StandardFonts,
charAtIndex,
Fontkit,
} from '../../..';

const breakTextIntoLines = (
Expand Down Expand Up @@ -58,7 +59,7 @@ export default async (assets: Assets) => {

const pdfDoc = await PDFDocument.create();

pdfDoc.registerFontkit(fontkit);
pdfDoc.registerFontkit(fontkit as Fontkit);

const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);
const helveticaBoldFont = await pdfDoc.embedFont(StandardFonts.HelveticaBold);
Expand Down
3 changes: 2 additions & 1 deletion apps/node/tests/test17.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import {
PDFFont,
drawEllipse,
PDFWidgetAnnotation,
Fontkit,
} from '../../..';

import fontkit from '@pdf-lib/fontkit';

export default async (assets: Assets) => {
const pdfDoc = await PDFDocument.load(assets.pdfs.fancy_fields);

pdfDoc.registerFontkit(fontkit);
pdfDoc.registerFontkit(fontkit as Fontkit);
const ubuntuFont = await pdfDoc.embedFont(assets.fonts.ttf.ubuntu_r);

const form = pdfDoc.getForm();
Expand Down
4 changes: 2 additions & 2 deletions apps/node/tests/test18.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Assets } from '..';
import { PDFDocument } from '../../..';
import { Fontkit, PDFDocument } from '../../..';

import fontkit from '@pdf-lib/fontkit';

Expand Down Expand Up @@ -128,7 +128,7 @@ const loadC = async (assets: Assets) => {
const loadD = async (assets: Assets) => {
const pdfDoc = await PDFDocument.load(assets.pdfs.fancy_fields);

pdfDoc.registerFontkit(fontkit);
pdfDoc.registerFontkit(fontkit as Fontkit);
const ubuntuFont = await pdfDoc.embedFont(assets.fonts.ttf.ubuntu_r);

const form = pdfDoc.getForm();
Expand Down
4 changes: 2 additions & 2 deletions apps/node/tests/test2.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fontkit from '@pdf-lib/fontkit';
import { Assets } from '..';
import { ParseSpeeds, PDFDocument, rgb } from '../../..';
import { Fontkit, ParseSpeeds, PDFDocument, rgb } from '../../..';

// This test loads an existing PDF document with many pages.
// It inserts data for every page (images, rectangles, texts, embedded PDFs).
Expand All @@ -13,7 +13,7 @@ export default async (assets: Assets) => {
updateMetadata: false,
});

pdfDoc.registerFontkit(fontkit);
pdfDoc.registerFontkit(fontkit as Fontkit);

const ubuntuFont = await pdfDoc.embedFont(fonts.ttf.ubuntu_r, {
subset: true,
Expand Down
4 changes: 2 additions & 2 deletions apps/node/tests/test6.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fontkit from '@pdf-lib/fontkit';
import { Assets } from '..';
import { degrees, ParseSpeeds, PDFDocument, rgb } from '../../..';
import { degrees, Fontkit, ParseSpeeds, PDFDocument, rgb } from '../../..';

export default async (assets: Assets) => {
const { pdfs, images, fonts } = assets;
Expand All @@ -10,7 +10,7 @@ export default async (assets: Assets) => {
{ parseSpeed: ParseSpeeds.Fastest },
);

pdfDoc.registerFontkit(fontkit);
pdfDoc.registerFontkit(fontkit as Fontkit);

await pdfDoc.attach(pdfs.us_constitution, 'us_constitution.pdf', {
mimeType: 'application/pdf',
Expand Down
2 changes: 1 addition & 1 deletion apps/node/tests/test7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default async (assets: Assets) => {
{ parseSpeed: ParseSpeeds.Fastest },
);

const allDonorPdfBytes: Uint8Array[] = [
const allDonorPdfBytes = [
assets.pdfs.normal,
assets.pdfs.with_update_sections,
assets.pdfs.linearized_with_object_streams,
Expand Down
4 changes: 2 additions & 2 deletions apps/node/tests/test9.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fontkit from '@pdf-lib/fontkit';
import { Assets } from '..';
import { ParseSpeeds, PDFDocument, rgb } from '../../..';
import { Fontkit, ParseSpeeds, PDFDocument, rgb } from '../../..';

export default async (assets: Assets) => {
const { pdfs, fonts, images } = assets;
Expand All @@ -9,7 +9,7 @@ export default async (assets: Assets) => {
parseSpeed: ParseSpeeds.Fastest,
});

pdfDoc.registerFontkit(fontkit);
pdfDoc.registerFontkit(fontkit as Fontkit);

const ubuntuFont = await pdfDoc.embedFont(fonts.ttf.ubuntu_r, {
subset: true,
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@
"@types/color": "^3.0.1",
"@types/crypto-js": "^4.2.2",
"@types/jest": "^29.5.12",
"@types/node-fetch": "^2.5.7",
"@types/pako": "^1.0.1",
"@types/node": "^25.1.0",
"@types/pako": "^2.0.4",
"@typescript-eslint/eslint-plugin": "^7.0.0",
"@typescript-eslint/parser": "^7.0.0",
"downlevel-dts": "^0.11.0",
Expand All @@ -139,7 +139,7 @@
"rimraf": "^5.0.5",
"rollup": "^4.9.6",
"ts-jest": "^29.0",
"typescript": "^4.9.5"
"typescript": "^5.9.3"
},
"license": "MIT",
"private": false,
Expand Down
22 changes: 13 additions & 9 deletions src/api/PDFDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export default class PDFDocument {
* @returns Resolves with a document loaded from the input.
*/
static async load(
pdf: string | Uint8Array | ArrayBuffer,
pdf: string | Uint8Array<ArrayBuffer> | ArrayBuffer,
options: LoadOptions = {},
) {
const {
Expand Down Expand Up @@ -972,7 +972,7 @@ export default class PDFDocument {
* @returns Resolves when the attachment is complete.
*/
async attach(
attachment: string | Uint8Array | ArrayBuffer,
attachment: string | Uint8Array<ArrayBuffer> | ArrayBuffer,
name: string,
options: AttachmentOptions = {},
): Promise<void> {
Expand Down Expand Up @@ -1183,7 +1183,7 @@ export default class PDFDocument {
* const font2 = await pdfDoc.embedFont('AAEAAAAVAQAABABQRFNJRx/upe...')
* const font3 = await pdfDoc.embedFont('data:font/opentype;base64,AAEAAA...')
*
* // font=Uint8Array
* // font=Uint8Array<ArrayBuffer>
* import fs from 'fs'
* const font4 = await pdfDoc.embedFont(fs.readFileSync('Ubuntu-R.ttf'))
*
Expand All @@ -1198,7 +1198,7 @@ export default class PDFDocument {
* @returns Resolves with the embedded font.
*/
async embedFont(
font: StandardFonts | string | Uint8Array | ArrayBuffer,
font: StandardFonts | string | Uint8Array<ArrayBuffer> | ArrayBuffer,
options: EmbedFontOptions = {},
): Promise<PDFFont> {
const { subset = false, customName, features } = options;
Expand Down Expand Up @@ -1275,7 +1275,7 @@ export default class PDFDocument {
* const image1 = await pdfDoc.embedJpg('/9j/4AAQSkZJRgABAQAAAQABAAD/2wBD...')
* const image2 = await pdfDoc.embedJpg('data:image/jpeg;base64,/9j/4AAQ...')
*
* // jpg=Uint8Array
* // jpg=Uint8Array<ArrayBuffer>
* import fs from 'fs'
* const uint8Array = fs.readFileSync('cat_riding_unicorn.jpg')
* const image3 = await pdfDoc.embedJpg(uint8Array)
Expand All @@ -1289,7 +1289,9 @@ 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> {
async embedJpg(
jpg: string | Uint8Array<ArrayBuffer> | ArrayBuffer,
): Promise<PDFImage> {
assertIs(jpg, 'jpg', ['string', Uint8Array, ArrayBuffer]);
const bytes = toUint8Array(jpg);
const embedder = await JpegEmbedder.for(bytes);
Expand Down Expand Up @@ -1329,7 +1331,9 @@ 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> {
async embedPng(
png: string | Uint8Array<ArrayBuffer> | ArrayBuffer,
): Promise<PDFImage> {
assertIs(png, 'png', ['string', Uint8Array, ArrayBuffer]);
const bytes = toUint8Array(png);
const embedder = await PngEmbedder.for(bytes);
Expand Down Expand Up @@ -1390,7 +1394,7 @@ export default class PDFDocument {
* @returns Resolves with an array of the embedded pages.
*/
async embedPdf(
pdf: string | Uint8Array | ArrayBuffer | PDFDocument,
pdf: string | Uint8Array<ArrayBuffer> | ArrayBuffer | PDFDocument,
indices: number[] = [0],
): Promise<PDFEmbeddedPage[]> {
assertIs(pdf, 'pdf', [
Expand Down Expand Up @@ -1560,7 +1564,7 @@ export default class PDFDocument {
* @param options The options to be used when saving the document.
* @returns Resolves with the bytes of the serialized document.
*/
async save(options: SaveOptions = {}): Promise<Uint8Array> {
async save(options: SaveOptions = {}) {
const vparts = this.context.header.getVersionString().split('.');
const uOS =
options.rewrite || Number(vparts[0]) > 1 || Number(vparts[1]) >= 5;
Expand Down
4 changes: 2 additions & 2 deletions src/core/PDFContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,14 +318,14 @@ class PDFContext {
}

stream(
contents: string | Uint8Array,
contents: string | Uint8Array<ArrayBuffer>,
dict: LiteralObject = {},
): PDFRawStream {
return PDFRawStream.of(this.obj(dict), typedArrayFor(contents));
}

flateStream(
contents: string | Uint8Array,
contents: string | Uint8Array<ArrayBuffer>,
dict: LiteralObject = {},
): PDFRawStream {
return this.stream(pako.deflate(typedArrayFor(contents)), {
Expand Down
8 changes: 4 additions & 4 deletions src/core/embedders/CustomFontEmbedder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
class CustomFontEmbedder {
static async for(
fontkit: Fontkit,
fontData: Uint8Array,
fontData: Uint8Array<ArrayBuffer>,
customName?: string,
fontFeatures?: TypeFeatures,
) {
Expand All @@ -31,7 +31,7 @@ class CustomFontEmbedder {

readonly font: Font;
readonly scale: number;
readonly fontData: Uint8Array;
readonly fontData: Uint8Array<ArrayBuffer>;
readonly fontName: string;
readonly customName: string | undefined;
readonly fontFeatures: TypeFeatures | undefined;
Expand All @@ -41,7 +41,7 @@ class CustomFontEmbedder {

protected constructor(
font: Font,
fontData: Uint8Array,
fontData: Uint8Array<ArrayBuffer>,
customName?: string,
fontFeatures?: TypeFeatures,
) {
Expand Down Expand Up @@ -186,7 +186,7 @@ class CustomFontEmbedder {
return context.register(fontDescriptor);
}

protected async serializeFont(): Promise<Uint8Array> {
protected async serializeFont() {
return this.fontData;
}

Expand Down
8 changes: 4 additions & 4 deletions src/core/embedders/CustomFontSubsetEmbedder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Cache, mergeUint8Arrays, toHexStringOfMinLength } from '../../utils';
class CustomFontSubsetEmbedder extends CustomFontEmbedder {
static async for(
fontkit: Fontkit,
fontData: Uint8Array,
fontData: Uint8Array<ArrayBuffer>,
customFontName?: string,
fontFeatures?: TypeFeatures,
) {
Expand All @@ -37,7 +37,7 @@ class CustomFontSubsetEmbedder extends CustomFontEmbedder {

private constructor(
font: Font,
fontData: Uint8Array,
fontData: Uint8Array<ArrayBuffer>,
customFontName?: string,
fontFeatures?: TypeFeatures,
) {
Expand Down Expand Up @@ -75,9 +75,9 @@ class CustomFontSubsetEmbedder extends CustomFontEmbedder {
return glyph ? this.glyphIdMap.get(glyph.id)! : -1;
}

protected serializeFont(): Promise<Uint8Array> {
protected serializeFont(): Promise<Uint8Array<ArrayBuffer>> {
return new Promise((resolve, reject) => {
const parts: Uint8Array[] = [];
const parts: Uint8Array<ArrayBuffer>[] = [];
this.subset
.encodeStream()
.on('data', (bytes) => parts.push(bytes))
Expand Down
6 changes: 3 additions & 3 deletions src/core/embedders/FileEmbedder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ export interface EmbeddedFileOptions {

class FileEmbedder {
static for(
bytes: Uint8Array,
bytes: Uint8Array<ArrayBuffer>,
fileName: string,
options: EmbeddedFileOptions = {},
) {
return new FileEmbedder(bytes, fileName, options);
}

private readonly fileData: Uint8Array;
private readonly fileData: Uint8Array<ArrayBuffer>;
readonly fileName: string;
readonly options: EmbeddedFileOptions;

private constructor(
fileData: Uint8Array,
fileData: Uint8Array<ArrayBuffer>,
fileName: string,
options: EmbeddedFileOptions = {},
) {
Expand Down
6 changes: 3 additions & 3 deletions 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 async for(imageData: Uint8Array<ArrayBuffer>) {
const dataView = new DataView(
imageData.buffer,
imageData.byteOffset,
Expand Down Expand Up @@ -79,10 +79,10 @@ class JpegEmbedder {
readonly width: number;
readonly colorSpace: ColorSpace;

private readonly imageData: Uint8Array;
private readonly imageData: Uint8Array<ArrayBuffer>;

private constructor(
imageData: Uint8Array,
imageData: Uint8Array<ArrayBuffer>,
bitsPerComponent: number,
width: number,
height: number,
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 async for(imageData: Uint8Array<ArrayBuffer>) {
const png = PNG.load(imageData);
return new PngEmbedder(png);
}
Expand Down
1 change: 0 additions & 1 deletion src/core/streams/FlateStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ class FlateStream extends DecodeStream {
buffer = this.buffer;
let limit = buffer ? buffer.length : 0;
let pos = this.bufferLength;
// eslint-disable-next-line no-constant-condition
while (true) {
let code1 = this.getCode(litCodeTable);
if (code1 < 256) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/writers/PDFWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class PDFWriter {
return should;
}

async serializeToBuffer(): Promise<Uint8Array> {
async serializeToBuffer() {
const incremental = !(this.snapshot instanceof DefaultDocumentSnapshot);
const { size, header, indirectObjects, xref, trailerDict, trailer } =
await this.computeBufferSize(incremental);
Expand Down
Loading
Loading