Skip to content

Commit

Permalink
fix all upgrade issues, remove svg support
Browse files Browse the repository at this point in the history
Whether I will add svg again will be seen in the future. Then you would have to implement your own implementation in any case, as the support from pdfjs has been cancelled. mozilla/pdf.js#16699
  • Loading branch information
lublak committed Mar 19, 2024
1 parent ebfa70d commit c5aeb7f
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 47 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"image"
],
"dependencies": {
"pdfjs-dist": "3.11.174"
"pdfjs-dist": "4.0.379"
},
"devDependencies": {
"@types/jest": "^29.5.12",
Expand Down
6 changes: 3 additions & 3 deletions src/canvasfactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface CanvasApi {
*
* @returns canvas 2d context
*/
createContext(): object;
createContext(): CanvasRenderingContext2D;
/**
* resets the canvas to the give size
*
Expand Down Expand Up @@ -44,7 +44,7 @@ export class CanvasFactory {
*/
public reset(canvasAndContext: {
canvas: CanvasApi | null,
context: object | null
context: CanvasRenderingContext2D | null
}, width: number, height: number): void {
if (!canvasAndContext.canvas) throw new Error('Canvas is not specified');
if (width <= 0 || height <= 0) throw new Error('Invalid canvas size');
Expand All @@ -55,7 +55,7 @@ export class CanvasFactory {
*/
public destroy(canvasAndContext: {
canvas: CanvasApi | null,
context: object | null
context: CanvasRenderingContext2D | null
}): void {
if (!canvasAndContext.canvas) throw new Error('Canvas is not specified');
canvasAndContext.canvas.destroy();
Expand Down
6 changes: 1 addition & 5 deletions src/contentinfoextractor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OPS } from 'pdfjs-dist/legacy/build/pdf';
import { OPS } from 'pdfjs-dist/legacy/build/pdf.mjs';
import { PDFOperatorList, PDFPageProxy } from 'pdfjs-dist/types/src/display/api';

interface Glyph {
Expand Down Expand Up @@ -826,10 +826,6 @@ export class ContentInfoExtractor {
}
private endAnnotation() {
}
private beginAnnotation() {
}
private endAnnotation() {
}
private paintJpegXObject() {
// not used
}
Expand Down
4 changes: 2 additions & 2 deletions src/nodecanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export class NodeCanvas implements CanvasApi {
/**
* @internal
*/
public createContext(): object {
return this.canvas.getContext('2d');
public createContext(): CanvasRenderingContext2D {
return this.canvas.getContext('2d') as any as CanvasRenderingContext2D;
}
/**
* @internal
Expand Down
2 changes: 1 addition & 1 deletion src/pdfdataextractor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getDocument, PermissionFlag } from 'pdfjs-dist/legacy/build/pdf';
import { getDocument, PermissionFlag } from 'pdfjs-dist/legacy/build/pdf.mjs';
import { PDFDocumentProxy, PDFPageProxy } from 'pdfjs-dist/types/src/display/api';
import { CanvasFactory } from './canvasfactory';
import { OcrFactory } from './ocrfactory';
Expand Down
28 changes: 0 additions & 28 deletions src/pdfpagedata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { OCRLang, Sort } from './types';
import { PageViewport } from 'pdfjs-dist/types/src/display/display_utils';
import { CanvasApi, CanvasFactory } from './canvasfactory';
import { ContentInfo, ContentInfoExtractor } from './contentinfoextractor';
import { SVGGraphics } from 'pdfjs-dist/types/src/pdf';
import { OcrApi, OcrFactory } from './ocrfactory';

interface SVGElementSerializer {
Expand Down Expand Up @@ -149,7 +148,6 @@ export class PdfPageData {
await this.page.render({
canvasContext: canvas.createContext(),
viewport: viewport,
canvasFactory: new CanvasFactory()
}).promise;
return canvas.toJPEG(quality);
}
Expand All @@ -166,33 +164,7 @@ export class PdfPageData {
await this.page.render({
canvasContext: canvas.createContext(),
viewport: viewport,
canvasFactory: new CanvasFactory()
}).promise;
return canvas.toPNG();
}

/**
* converts to a svg image
*
* @returns {Promise<string>} the svg image as a {string}
*/
public async toSVG(): Promise<string> {
let result = '';
const viewport: PageViewport = this.page.getViewport({ scale: 1.0 });
const opList = await this.page.getOperatorList();
const svgGfx = new SVGGraphics(
this.page.commonObjs,
this.page.objs,
true
);
svgGfx.embedFonts = true;
const svg: SVGElement = await svgGfx.getSVG(opList, viewport);
const serializer = svg.getSerializer();
let chunk = serializer.getNext();
while (chunk != null) {
result += chunk;
chunk = serializer.getNext();
}
return result;
}
}
4 changes: 2 additions & 2 deletions src/pureimagecanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class PureimageCanvas implements CanvasApi {
* @internal
*/
public constructor(width: number, height: number) {
this.bitmap = make(width, height, null);
this.bitmap = make(width, height);
}
/**
* @internal
Expand All @@ -40,7 +40,7 @@ export class PureimageCanvas implements CanvasApi {
/**
* @internal
*/
public createContext(): object {
public createContext(): CanvasRenderingContext2D {
return this.bitmap.getContext('2d');
}
/**
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { VerbosityLevel as RawVerbosityLevel } from 'pdfjs-dist/legacy/build/pdf';
import { VerbosityLevel as RawVerbosityLevel } from 'pdfjs-dist/legacy/build/pdf.mjs';

export enum VerbosityLevel {
/**
Expand Down

0 comments on commit c5aeb7f

Please sign in to comment.