Repository navigation
#210 Limit imported PDF size in Document Editor #210
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,83 @@ | ||
| import type { | ||
| DocumentEditorToolbarItem, | ||
| DocumentEditorUIHandler, | ||
| DocumentOperations, | ||
| } from "@nutrient-sdk/viewer"; | ||
| import { baseOptions } from "../../shared/base-options"; | ||
|
|
||
| // Replace the Document Editor's Import Document button with one that rejects | ||
| // PDFs over a size limit before they're added to the document. | ||
| // | ||
| // Needs Nutrient Web SDK 1.18.0 or later. On 1.17.0 and 1.9.1, a custom | ||
| // button's `setOperations` call also applies edits the user undid, so Rotate, | ||
| // Undo, then Import keeps the rotation and drops the import. | ||
|
|
||
| // 1 MB here is 1024 × 1024 bytes. | ||
| const MAX_FILE_SIZE_MB = 25; | ||
| const MAX_FILE_SIZE_BYTES = MAX_FILE_SIZE_MB * 1024 * 1024; | ||
|
|
||
| const importButton: DocumentEditorToolbarItem = { | ||
| type: "custom", | ||
| id: "import-pdf-with-size-limit", | ||
| title: "Import PDF", | ||
| onPress: (_event: Event, handler: DocumentEditorUIHandler) => { | ||
| // A file input that isn't added to the page opens the file chooser. | ||
| const input = document.createElement("input"); | ||
| input.type = "file"; | ||
| input.accept = "application/pdf"; | ||
|
|
||
| input.onchange = () => { | ||
| const file = input.files?.[0]; | ||
| if (!file) return; | ||
|
|
||
| if (file.type !== "application/pdf") { | ||
| alert("The file must be a PDF."); | ||
| return; | ||
| } | ||
|
|
||
| if (file.size > MAX_FILE_SIZE_BYTES) { | ||
| // Round up so a file just over the limit doesn't read as 25.0 MB. | ||
| const fileSizeMB = ( | ||
| Math.ceil((file.size / 1024 / 1024) * 10) / 10 | ||
| ).toFixed(1); | ||
| alert( | ||
| `This PDF is ${fileSizeMB} MB. You can import PDFs up to ${MAX_FILE_SIZE_MB} MB.`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| // Insert after the last selected page. With no page selected, insert at | ||
| // the start, like the built-in button. | ||
| const selectedPages = handler.getSelectedPageIndexes(); | ||
| const position = | ||
| selectedPages.length > 0 | ||
| ? { afterPageIndex: Math.max(...selectedPages) } | ||
| : { beforePageIndex: 0 }; | ||
|
|
||
| const operation: DocumentOperations.DocumentOperationsUnion = { | ||
| type: "importDocument", | ||
| ...position, | ||
| // The Document Editor shows the imported PDF as one page until the | ||
| // user saves, so later edits to other pages need it counted as one. | ||
| treatImportedDocumentAsOnePage: true, | ||
| document: file, | ||
| }; | ||
|
|
||
| // Stage the import. It's applied when the user saves. | ||
| handler.setOperations((operations) => operations.push(operation), true); | ||
| }; | ||
|
|
||
| input.click(); | ||
| }, | ||
| }; | ||
|
|
||
| NutrientViewer.load({ | ||
| ...baseOptions, | ||
| theme: NutrientViewer.Theme.DARK, | ||
| documentEditorToolbarItems: [ | ||
| importButton, | ||
| ...NutrientViewer.defaultDocumentEditorToolbarItems.filter( | ||
| (item) => item.type !== "import-document", | ||
| ), | ||
| ], | ||
| }); | ||
This file contains hidden or 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,6 @@ | ||
| --- | ||
| category: document-editor | ||
| title: Limit Imported PDF Size | ||
| description: Replace the Document Editor's Import Document button with a custom one that rejects PDFs over 25 MB before they're added to the document. | ||
| keywords: [document editor, import document, file size, custom toolbar, toolbar items, importDocument, setOperations] | ||
| --- |
This file contains hidden or 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,2 @@ | ||
| [InternetShortcut] | ||
| URL=https://www.nutrient.io/demo/sandbox?p=eyJqcyI6Ii8vIFJlcGxhY2UgdGhlIERvY3VtZW50IEVkaXRvcidzIEltcG9ydCBEb2N1bWVudCBidXR0b24gd2l0aCBvbmUgdGhhdCByZWplY3RzXG4vLyBQREZzIG92ZXIgYSBzaXplIGxpbWl0IGJlZm9yZSB0aGV5J3JlIGFkZGVkIHRvIHRoZSBkb2N1bWVudC5cbi8vXG4vLyBOZWVkcyBOdXRyaWVudCBXZWIgU0RLIDEuMTguMCBvciBsYXRlci4gT24gMS4xNy4wIGFuZCAxLjkuMSwgYSBjdXN0b21cbi8vIGJ1dHRvbidzIGBzZXRPcGVyYXRpb25zYCBjYWxsIGFsc28gYXBwbGllcyBlZGl0cyB0aGUgdXNlciB1bmRpZCwgc28gUm90YXRlLFxuLy8gVW5kbywgdGhlbiBJbXBvcnQga2VlcHMgdGhlIHJvdGF0aW9uIGFuZCBkcm9wcyB0aGUgaW1wb3J0LlxuXG4vLyAxIE1CIGhlcmUgaXMgMTAyNCDDlyAxMDI0IGJ5dGVzLlxuY29uc3QgTUFYX0ZJTEVfU0laRV9NQiA9IDI1O1xuY29uc3QgTUFYX0ZJTEVfU0laRV9CWVRFUyA9IE1BWF9GSUxFX1NJWkVfTUIgKiAxMDI0ICogMTAyNDtcblxuY29uc3QgaW1wb3J0QnV0dG9uID0ge1xuICB0eXBlOiBcImN1c3RvbVwiLFxuICBpZDogXCJpbXBvcnQtcGRmLXdpdGgtc2l6ZS1saW1pdFwiLFxuICB0aXRsZTogXCJJbXBvcnQgUERGXCIsXG4gIG9uUHJlc3M6IChfZXZlbnQsIGhhbmRsZXIpID0%2BIHtcbi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xpY2soKTtcbiAgfSxcbn07XG5cbk51dHJpZW50Vmlld2VyLmxvYWQoe1xuICAuLi5iYXNlT3B0aW9ucyxcbiAgdGhlbWU6IE51dHJpZW50Vmlld2VyLlRoZW1lLkRBUkssXG4gIGRvY3VtZW50RWRpdG9yVG9vbGJhckl0ZW1zOiBbXG4gICAgaW1wb3J0QnV0dG9uLFxuICAgIC4uLk51dHJpZW50Vmlld2VyLmRlZmF1bHREb2N1bWVudEVkaXRvclRvb2xiYXJJdGVtcy5maWx0ZXIoXG4gICAgICAoaXRlbSkgPT4gaXRlbS50eXBlICE9PSBcImltcG9ydC1kb2N1bWVudFwiLFxuICAgICksXG4gIF0sXG59KTtcblxuIiwiY3NzIjoiLyogQWRkIHlvdXIgQ1NTIGhlcmUgKi9cbiIsInNldHRpbmdzIjp7ImZpbGVOYW1lIjoiYmFzaWMucGRmIiwidXNlRHluYW1pY0ZvbnRzIjp0cnVlLCJjdXN0b21Gb250RmlsZU5hbWVzIjpbXX0sInYiOjF9 |
This file contains hidden or 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
Oops, something went wrong.
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.
With the SDK pinned here (1.9.1), Rotate → Undo → Import leaves the rotation active and the new PDF out of Save.
setOperationskeeps the undone edits, unlike the built-in importer. Please preserve the built-in undo behavior, or document a verified minimum SDK version, and check this sequence on 1.25.0 too.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.
Rotate, Undo, Import on 1.9.1 and 1.17.0 keeps the rotation and drops the import with this button, while the built-in one doesn’t. On 1.18.0 and 1.25.0 both give the same result, so I added a 1.18.0 minimum to the example’s comment and the PR description