Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POC] feat: mupdfjs with asm.js and not with wasm build to extract metadata and cover img #2671

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@
"match-sorter": "^8.0.0",
"mathjax": "^3.2.2",
"mime-types": "^2.1.35",
"mupdf": "github:edrlab/mupdf.js",
"moment": "^2.30.1",
"nanoid": "^5.0.9",
"node-fetch": "^3.3.2",
Expand Down
50 changes: 49 additions & 1 deletion src/main/pdf/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,59 @@ import { encodeURIComponent_RFC3986 } from "@r2-utils-js/_utils/http/UrlUtils";
import { IInfo } from "./extract.type";
import { THORIUM_READIUM2_ELECTRON_HTTP_PROTOCOL } from "readium-desktop/common/streamerProtocol";

import { readFile } from "node:fs/promises";

import * as mupdfjs from "mupdf";

const debug = debug_("readium-desktop:main/pdf/extract/index.ts");
debug("_");

type TExtractPdfData = [data: IInfo | undefined, coverPNG: Buffer | undefined];
export const extractPDFData =

export const extractPDFData = async (pdfPath: string): Promise<TExtractPdfData> => {

try {
const pdfBuffer = await readFile(pdfPath);

const doc = mupdfjs.PDFDocument.openDocument(pdfBuffer, "application/pdf");

const info: IInfo = {
Title: doc.getMetaData("info:Title"),
Subject: doc.getMetaData("info:Subject"),
Keywords: doc.getMetaData("info:Keywords"),
Author: doc.getMetaData("info:Author"),
Creator: doc.getMetaData("info:Creator"),
Producer: doc.getMetaData("info:Producer"),
CreationDate: doc.getMetaData("info:CreationDate"),
ModDate: doc.getMetaData("info:ModDate"),
numberOfPages: doc.countPages(),
};

const page = new mupdfjs.PDFPage(doc, 0);

const pixmap = page.toPixmap(mupdfjs.Matrix.identity, mupdfjs.ColorSpace.DeviceRGB, false, true);
const pngImage = pixmap.asPNG();
const img = Buffer.alloc(pngImage.byteLength);
for (let i = 0; i < img.length; ++i) {
img[i] = pngImage[i];
}

return [info, img];


} catch (e) {

debug("####");
debug("####");
debug(e);
debug("####");
debug("####");
}

return [undefined, undefined];
};

export const extractPDFDataPdfjs =
async (pdfPath: string)
: Promise<TExtractPdfData> => {

Expand Down
8 changes: 4 additions & 4 deletions src/main/pdf/extract.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

export interface IInfo {
PDFFormatVersion?: string;
IsAcroFormPresent?: boolean;
IsCollectionPresent?: boolean;
IsLinearized?: boolean;
IsXFAPresent?: boolean;
// IsAcroFormPresent?: boolean;
// IsCollectionPresent?: boolean;
// IsLinearized?: boolean;
// IsXFAPresent?: boolean;
Title?: string;
Subject?: string;
Keywords?: string;
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"removeComments": true,
"skipLibCheck": false,
"module": "ES2020",
"moduleResolution": "Node",
"moduleResolution": "node10",
"lib": [
"es2020",
"dom",
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const _externalsCache = new Set();
if (nodeEnv !== "production") {
const nodeExternals = require("webpack-node-externals");
const neFunc = nodeExternals({
allowlist: ["timeout-signal", "nanoid", "normalize-url", "node-fetch", "data-uri-to-buffer", /^fetch-blob/, /^formdata-polyfill/],
allowlist: ["timeout-signal", "nanoid", "normalize-url", "node-fetch", "mupdf", "data-uri-to-buffer", /^fetch-blob/, /^formdata-polyfill/],
importType: function (moduleName) {
if (!_externalsCache.has(moduleName)) {
console.log(`WEBPACK EXTERNAL (MAIN): [${moduleName}]`);
Expand Down
Loading