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

[api-minor] Expose the /Desc-attribute of file attachments in the viewer (issue 18030) #18031

Merged
merged 1 commit into from
May 1, 2024
Merged
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
16 changes: 12 additions & 4 deletions src/core/file_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* limitations under the License.
*/

import { stringToPDFString, warn } from "../shared/util.js";
import { shadow, stringToPDFString, warn } from "../shared/util.js";
import { BaseStream } from "./base_stream.js";
import { Dict } from "./primitives.js";

Expand Down Expand Up @@ -53,9 +53,6 @@ class FileSpec {
if (root.has("FS")) {
this.fs = root.get("FS");
}
this.description = root.has("Desc")
? stringToPDFString(root.get("Desc"))
: "";
if (root.has("RF")) {
warn("Related file specifications are not supported");
}
Expand Down Expand Up @@ -102,10 +99,21 @@ class FileSpec {
return content;
}

get description() {
let description = "";

const desc = this.root?.get("Desc");
if (desc && typeof desc === "string") {
description = stringToPDFString(desc);
}
return shadow(this, "description", description);
}

get serializable() {
return {
filename: this.filename,
content: this.content,
description: this.description,
};
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,9 @@ class LinkAnnotationElement extends AnnotationElement {
*/
#bindAttachment(link, attachment, dest = null) {
link.href = this.linkService.getAnchorUrl("");
if (attachment.description) {
link.title = attachment.description;
}
link.onclick = () => {
this.downloadManager?.openOrDownloadData(
attachment.content,
Expand Down Expand Up @@ -2856,14 +2859,15 @@ class FileAttachmentAnnotationElement extends AnnotationElement {
constructor(parameters) {
super(parameters, { isRenderable: true });

const { filename, content } = this.data.file;
const { filename, content, description } = this.data.file;
this.filename = getFilenameFromUrl(filename, /* onlyStripPath = */ true);
this.content = content;

this.linkService.eventBus?.dispatch("fileattachmentannotation", {
source: this,
filename,
content,
description,
});
}

Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
!issue17056.pdf
!issue17679.pdf
!issue17679_2.pdf
!issue18030.pdf
!issue14953.pdf
!issue15367.pdf
!issue15372.pdf
Expand Down
Binary file added test/pdfs/issue18030.pdf
Binary file not shown.
3 changes: 2 additions & 1 deletion test/unit/annotation_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4005,7 +4005,7 @@ describe("annotation", function () {
const fileSpecRef = Ref.get(19, 0);
const fileSpecDict = new Dict();
fileSpecDict.set("Type", Name.get("Filespec"));
fileSpecDict.set("Desc", "");
fileSpecDict.set("Desc", "abc");
fileSpecDict.set("EF", embeddedFileDict);
fileSpecDict.set("UF", "Test.txt");

Expand Down Expand Up @@ -4035,6 +4035,7 @@ describe("annotation", function () {
expect(data.annotationType).toEqual(AnnotationType.FILEATTACHMENT);
expect(data.file.filename).toEqual("Test.txt");
expect(data.file.content).toEqual(stringToBytes("Test attachment"));
expect(data.file.description).toEqual("abc");
});
});

Expand Down
23 changes: 20 additions & 3 deletions test/unit/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1475,11 +1475,28 @@ describe("api", function () {
const pdfDoc = await loadingTask.promise;
const attachments = await pdfDoc.getAttachments();

const attachment = attachments["foo.txt"];
expect(attachment.filename).toEqual("foo.txt");
expect(attachment.content).toEqual(
const { filename, content, description } = attachments["foo.txt"];
expect(filename).toEqual("foo.txt");
expect(content).toEqual(
new Uint8Array([98, 97, 114, 32, 98, 97, 122, 32, 10])
);
expect(description).toEqual("");

await loadingTask.destroy();
});

it("gets attachments, with /Desc", async function () {
const loadingTask = getDocument(buildGetDocumentParams("issue18030.pdf"));
const pdfDoc = await loadingTask.promise;
const attachments = await pdfDoc.getAttachments();

const { filename, content, description } = attachments["empty.pdf"];
expect(filename).toEqual("Empty page.pdf");
expect(content instanceof Uint8Array).toEqual(true);
expect(content.length).toEqual(2357);
expect(description).toEqual(
"SHA512: 06bec56808f93846f1d41ff0be4e54079c1291b860378c801c0f35f1d127a8680923ff6de59bd5a9692f01f0d97ca4f26da178ed03635fa4813d86c58a6c981a"
);

await loadingTask.destroy();
});
Expand Down
11 changes: 8 additions & 3 deletions web/pdf_attachment_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ class PDFAttachmentViewer extends BaseTreeViewer {
/**
* @protected
*/
_bindLink(element, { content, filename }) {
_bindLink(element, { content, description, filename }) {
if (description) {
element.title = description;
}
element.onclick = () => {
this.downloadManager.openOrDownloadData(content, filename);
return false;
Expand All @@ -120,6 +123,7 @@ class PDFAttachmentViewer extends BaseTreeViewer {
for (const name in attachments) {
const item = attachments[name];
const content = item.content,
description = item.description,
filename = getFilenameFromUrl(
item.filename,
/* onlyStripPath = */ true
Expand All @@ -129,7 +133,7 @@ class PDFAttachmentViewer extends BaseTreeViewer {
div.className = "treeItem";

const element = document.createElement("a");
this._bindLink(element, { content, filename });
this._bindLink(element, { content, description, filename });
element.textContent = this._normalizeTextContent(filename);

div.append(element);
Expand All @@ -144,7 +148,7 @@ class PDFAttachmentViewer extends BaseTreeViewer {
/**
* Used to append FileAttachment annotations to the sidebar.
*/
#appendAttachment({ filename, content }) {
#appendAttachment({ filename, content, description }) {
const renderedPromise = this._renderedCapability.promise;

renderedPromise.then(() => {
Expand All @@ -161,6 +165,7 @@ class PDFAttachmentViewer extends BaseTreeViewer {
attachments[filename] = {
filename,
content,
description,
};
this.render({
attachments,
Expand Down
Loading