Skip to content

Commit

Permalink
JS -- support doc.info.authors
Browse files Browse the repository at this point in the history
 - get the authors from dc:creator in xmp metadata
 - doc.info.metadata is the raw string with xml code
  • Loading branch information
calixteman committed Jan 8, 2021
1 parent 35845d1 commit e630956
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2705,6 +2705,7 @@ class WorkerTransport {
.then(results => {
return {
info: results[0],
rawMetadata: results[1],
metadata: results[1] ? new Metadata(results[1]) : null,
contentDispositionFilename: this._fullReader?.filename ?? null,
contentLength: this._fullReader?.contentLength ?? null,
Expand Down
30 changes: 26 additions & 4 deletions src/display/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,33 @@ class Metadata {
}

for (let j = 0, jj = desc.childNodes.length; j < jj; j++) {
if (desc.childNodes[j].nodeName.toLowerCase() !== "#text") {
const entry = desc.childNodes[j];
const entry = desc.childNodes[j];
if (entry.nodeName.toLowerCase() !== "#text") {
const name = entry.nodeName.toLowerCase();

this._metadataMap.set(name, entry.textContent.trim());
if (name === "dc:creator") {
if (!entry.childNodes.length) {
continue;
}
// Child must be a Bag (unordered array) or a Seq.
const authors = [];
const seqNode = entry.childNodes[0];
if (
seqNode.nodeName === "rdf:Bag" ||
seqNode.nodeName === "rdf:Seq"
) {
for (let k = 0, kk = seqNode.childNodes.length; k < kk; k++) {
const authorNode = seqNode.childNodes[k];
if (authorNode.nodeName === "rdf:li") {
authors.push(authorNode.textContent.trim());
}
}
}
if (authors.length > 0) {
this._metadataMap.set(name, authors);
}
} else {
this._metadataMap.set(name, entry.textContent.trim());
}
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/scripting_api/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ class Doc extends PDFObject {
// and they're are read-only.
this._info = new Proxy(
{
title: this.title,
author: this.author,
subject: this.subject,
keywords: this.keywords,
creator: this.creator,
producer: this.producer,
title: this._title,
author: this._author,
authors: data.authors || [this._author],
subject: this._subject,
keywords: this._keywords,
creator: this._creator,
producer: this._producer,
creationdate: this._creationDate,
moddate: this._modDate,
trapped: data.Trapped || "Unknown",
Expand Down
25 changes: 25 additions & 0 deletions test/integration/scripting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,4 +451,29 @@ describe("Interaction", () => {
);
});
});

describe("in js-authors.pdf", () => {
let pages;

beforeAll(async () => {
pages = await loadAndWait("js-authors.pdf", "#\\32 5R");
});

afterAll(async () => {
await closePages(pages);
});

it("must print authors in a text field", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
const text = await actAndWaitForInput(page, "#\\32 5R", async () => {
await page.click("[data-annotation-id='26R']");
});
expect(text)
.withContext(`In ${browserName}`)
.toEqual("author1::author2::author3::author4::author5");
})
);
});
});
});
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@
!tensor-allflags-withfunction.pdf
!issue10084_reduced.pdf
!issue4246.pdf
!js-authors.pdf
!issue4461.pdf
!issue4573.pdf
!issue4722.pdf
Expand Down
Binary file added test/pdfs/js-authors.pdf
Binary file not shown.
6 changes: 6 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3213,6 +3213,12 @@
"rounds": 1,
"type": "eq"
},
{ "id": "js-authors",
"file": "pdfs/js-authors.pdf",
"md5": "e892f290ae209286a29d3c17dfeab226",
"rounds": 1,
"type": "eq"
},
{ "id": "issue1810",
"file": "pdfs/issue1810.pdf",
"md5": "b173a9dfb7bf00e1a298c6e8cb95c03e",
Expand Down
5 changes: 4 additions & 1 deletion web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1655,7 +1655,8 @@ const PDFViewerApplication = {
baseURL: this.baseUrl,
filesize: this._contentLength,
filename: this._docFilename,
metadata: this.metadata,
metadata: this.rawMetadata,
authors: this.metadata.get("dc:creator") || null,
numPages: pdfDocument.numPages,
URL: this.url,
actions: docActions,
Expand Down Expand Up @@ -1756,6 +1757,7 @@ const PDFViewerApplication = {
async _initializeMetadata(pdfDocument) {
const {
info,
rawMetadata,
metadata,
contentDispositionFilename,
contentLength,
Expand All @@ -1765,6 +1767,7 @@ const PDFViewerApplication = {
return; // The document was closed while the metadata resolved.
}
this.documentInfo = info;
this.rawMetadata = rawMetadata;
this.metadata = metadata;
this._contentDispositionFilename = contentDispositionFilename;
this._contentLength ??= contentLength; // See `getDownloadInfo`-call above.
Expand Down

0 comments on commit e630956

Please sign in to comment.