-
Notifications
You must be signed in to change notification settings - Fork 31
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
[WIP] Add scrub #141
Open
mipo1357
wants to merge
16
commits into
master
Choose a base branch
from
add-scrub
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+545
−106
Open
[WIP] Add scrub #141
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8cc8c9d
add metadata reset logic
mipo1357 a4a14ff
add links reset logic
mipo1357 ca634a5
add attachedFiles reset logic
mipo1357 dde76f3
add javascript reset logic
mipo1357 6cf09cf
add xml reset logic
mipo1357 eac9096
add embeddedFiles reset logic
mipo1357 6ce715c
add clean page logic
mipo1357 8f3d3e7
Add clean thumbnail logic
mipo1357 9ff2aac
Add reset_fields logic
mipo1357 5bd5628
Add resetResponses logic
mipo1357 9c88425
refactor: use delete() instead of put(null) for XML metadata removal
mipo1357 3e9daa9
refactor: replace empty file with null in file attachment scrubbing
mipo1357 c81a46f
update
mipo1357 7a02d46
refactor: improve thumbnail removal by using delete instead of null a…
mipo1357 0729d22
fix: add super call in PDFPage constructor to resolve linter error
mipo1357 4cae059
Merge branch 'master' of https://github.com/ArtifexSoftware/mupdf.js …
mipo1357 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { afterEach, describe, expect, it } from "vitest"; | ||
import { PDFDocument } from "../../../dist/mupdfjs"; | ||
|
||
describe("PDFDocument scrub test", () => { | ||
it("should clean metadata from a new document", () => { | ||
// Create a new document | ||
const doc = PDFDocument.createBlankDocument(); | ||
|
||
try { | ||
// Set and verify Title | ||
doc.setMetaData("info:Title", "Test Document"); | ||
doc.setMetaData("info:Author", "Test Author"); | ||
doc.setMetaData("info:Subject", "Test Subject"); | ||
doc.setMetaData("info:Keywords", "test, pdf"); | ||
doc.setMetaData("info:Creator", "Test Creator"); | ||
doc.setMetaData("info:Producer", "Test Producer"); | ||
doc.setMetaData("info:CreationDate", "D:20240101000000Z"); | ||
doc.setMetaData("info:ModDate", "D:20240101120000Z"); | ||
|
||
expect(doc.getMetaData("info:Title")).toBe("Test Document"); | ||
expect(doc.getMetaData("info:Author")).toBe("Test Author"); | ||
expect(doc.getMetaData("info:Subject")).toBe("Test Subject"); | ||
expect(doc.getMetaData("info:Keywords")).toBe("test, pdf"); | ||
expect(doc.getMetaData("info:Creator")).toBe("Test Creator"); | ||
expect(doc.getMetaData("info:Producer")).toBe("Test Producer"); | ||
expect(doc.getMetaData("info:CreationDate")).toBe("D:20240101000000Z"); | ||
expect(doc.getMetaData("info:ModDate")).toBe("D:20240101120000Z"); | ||
|
||
// Scrub metadata | ||
doc.scrub({ metadata: true }); | ||
|
||
// Verify all metadata is cleaned after scrub | ||
expect(doc.getMetaData("info:Title")).toBeUndefined(); | ||
expect(doc.getMetaData("info:Author")).toBeUndefined(); | ||
expect(doc.getMetaData("info:Subject")).toBeUndefined(); | ||
expect(doc.getMetaData("info:Keywords")).toBeUndefined(); | ||
expect(doc.getMetaData("info:Creator")).toBeUndefined(); | ||
expect(doc.getMetaData("info:Producer")).toBeUndefined(); | ||
expect(doc.getMetaData("info:CreationDate")).toBeUndefined(); | ||
expect(doc.getMetaData("info:ModDate")).toBeUndefined(); | ||
} finally { | ||
doc.destroy(); | ||
} | ||
}); | ||
|
||
it("should remove all links from pages", () => { | ||
// Create a new document | ||
const doc = PDFDocument.createBlankDocument(); | ||
|
||
try { | ||
// Add a link to the first page | ||
const page = doc.loadPage(0); | ||
page.createLink([50, 50, 150, 100], "https://example.com"); | ||
|
||
// Verify link exists | ||
const linksBeforeScrub = page.getLinks(); | ||
expect(linksBeforeScrub.length).toBe(1); | ||
expect(linksBeforeScrub[0].getURI()).toBe("https://example.com"); | ||
|
||
// Scrub links | ||
doc.scrub({ removeLinks: true }); | ||
|
||
// Verify links are removed | ||
const linksAfterScrub = page.getLinks(); | ||
expect(linksAfterScrub.length).toBe(0); | ||
} finally { | ||
doc.destroy(); | ||
} | ||
}); | ||
|
||
it("should remove file attachment annotations", () => { | ||
// Create a new document | ||
const doc = PDFDocument.createBlankDocument(); | ||
|
||
try { | ||
// Add a file attachment annotation to the first page | ||
const page = doc.loadPage(0); | ||
const annot = page.createAnnotation("FileAttachment"); | ||
annot.setRect([50, 50, 100, 100]); | ||
annot.setContents("Test attachment"); | ||
annot.update(); | ||
|
||
// Verify file attachment exists | ||
const annotationsBeforeScrub = page.getAnnotations(); | ||
const fileAttachment = annotationsBeforeScrub.find(a => a.getType() === "FileAttachment"); | ||
expect(fileAttachment).toBeDefined(); | ||
|
||
// Scrub file attachments | ||
doc.scrub({ attachedFiles: true }); | ||
|
||
// Verify file attachment exists but content is empty | ||
const annotationsAfterScrub = page.getAnnotations(); | ||
const scrubbedAttachment = annotationsAfterScrub.find(a => a.getType() === "FileAttachment"); | ||
expect(scrubbedAttachment).toBeDefined(); | ||
expect(annotationsAfterScrub.length).toBe(annotationsBeforeScrub.length); | ||
|
||
// Verify file content is empty | ||
const fileSpec = scrubbedAttachment!.getFileSpec(); | ||
expect(fileSpec).toBeDefined(); | ||
|
||
// Get embedded file stream | ||
const ef = fileSpec.get("EF"); | ||
expect(ef.isDictionary()).toBe(true); | ||
|
||
// Verify stream content is empty | ||
const stream = ef.get("F"); | ||
expect(stream.isStream()).toBe(true); | ||
const content = stream.readStream(); | ||
expect(content.getLength()).toBe(1); | ||
expect(content.readByte(0)).toBe(32); // space character | ||
} finally { | ||
doc.destroy(); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really want to replace the attached file with an empty file?
We can also use
annot.setFileSpec(null)
to remove the attachment instead.