Skip to content

Commit 4604ffb

Browse files
AntikodeAntikode
authored andcommitted
fix: metadata.js Keywords field silently mangled multi-word keywords on round-trip
Loading a saved PDF re-split the Keywords string by whitespace and rejoined with commas for display, then saving re-split by comma and joined with a single space via pdf-lib's setKeywords(). Any keyword containing an internal space (e.g. "machine learning") was silently torn into separate keywords ("machine", "learning") after a single save+reload cycle, with no warning. Fixed by treating Keywords as the single free-text field it actually is: display/edit the raw string verbatim, and pass it to setKeywords() as a single-element array so pdf-lib's internal join(" ") is a no-op.
1 parent fd6f351 commit 4604ffb

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

metadata.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,11 @@ async function loadFile(file) {
107107
titleInput.value = doc.getTitle() || "";
108108
authorInput.value = doc.getAuthor() || "";
109109
subjectInput.value = doc.getSubject() || "";
110-
const keywords = doc.getKeywords();
111-
// pdf-lib returns getKeywords() as a single space-joined string; show it
112-
// comma-separated in the UI for easier editing either way.
113-
keywordsInput.value = keywords ? keywords.split(/\s+/).filter(Boolean).join(", ") : "";
110+
// Keywords is a single free-text PDF field, not a real array — show it
111+
// verbatim. Splitting/rejoining on whitespace would silently mangle any
112+
// keyword that itself contains a space (e.g. "machine learning" becomes
113+
// two separate keywords "machine" and "learning" on save+reload).
114+
keywordsInput.value = doc.getKeywords() || "";
114115
createdInput.value = dateToLocalInputValue(doc.getCreationDate());
115116
modifiedInput.value = dateToLocalInputValue(doc.getModificationDate());
116117

@@ -194,11 +195,11 @@ async function saveMetadata() {
194195
doc.setAuthor(authorInput.value.trim());
195196
doc.setSubject(subjectInput.value.trim());
196197

197-
const keywordList = keywordsInput.value
198-
.split(",")
199-
.map((k) => k.trim())
200-
.filter((k) => k.length > 0);
201-
doc.setKeywords(keywordList);
198+
// Pass the raw text as a single array element so pdf-lib's join(" ")
199+
// (used internally by setKeywords) is a no-op and the user's exact text
200+
// — commas, spaces, and all — survives the round trip untouched.
201+
const keywordsValue = keywordsInput.value.trim();
202+
doc.setKeywords(keywordsValue ? [keywordsValue] : []);
202203

203204
const createdDate = localInputValueToDate(createdInput.value);
204205
if (createdDate) {

0 commit comments

Comments
 (0)