Skip to content

Commit

Permalink
Fix JS -> PDF object conversions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccxvii committed Apr 23, 2024
1 parent f5b9ff4 commit 8468e9b
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/mupdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1991,8 +1991,13 @@ export class PDFDocument extends Document {
return obj
if (obj === null || obj === undefined)
return this.newNull()
if (typeof obj === "string")
return this.newString(obj)
if (typeof obj === "string") {
// if a JS string is surrounded by parens, convert it to a PDF string
if (obj.startsWith("(") && obj.endsWith(")"))
return this.newString(obj.slice(1, -1))
// otherwise treat it as a name
return this.newName(obj)
}
if (typeof obj === "number") {
if (obj === (obj | 0))
return this.newInteger(obj)
Expand All @@ -2003,13 +2008,13 @@ export class PDFDocument extends Document {
if (obj instanceof Array) {
let result = this.newArray(obj.length)
for (let item of obj)
result.push(this._PDFOBJ(item))
result.push(item)
return result
}
if (obj instanceof Object) {
let result = this.newDictionary()
for (let key in obj)
result.put(key, this._PDFOBJ(obj[key]))
result.put(key, obj[key])
return result
}
throw new TypeError("cannot convert value to PDFObject")
Expand Down

0 comments on commit 8468e9b

Please sign in to comment.