Skip to content

Commit

Permalink
Fix #779, #780
Browse files Browse the repository at this point in the history
  • Loading branch information
hhrutter committed Feb 6, 2024
1 parent 865e6b7 commit 9295163
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
20 changes: 18 additions & 2 deletions pkg/pdfcpu/validate/annotation.go
Expand Up @@ -376,10 +376,18 @@ func validateAnnotationDictLink(xRefTable *model.XRefTable, d types.Dict, dictNa
func validateAnnotationDictFreeTextPart1(xRefTable *model.XRefTable, d types.Dict, dictName string) error {

// DA, required, string
_, err := validateStringEntry(xRefTable, d, dictName, "DA", REQUIRED, model.V10, nil)
validate := validateDA
if xRefTable.ValidationMode == model.ValidationRelaxed {
validate = validateDARelaxed
}
da, err := validateStringEntry(xRefTable, d, dictName, "DA", REQUIRED, model.V10, validate)
if err != nil {
return err
}
if xRefTable.ValidationMode == model.ValidationRelaxed && da != nil {
// Repair
d["DA"] = types.StringLiteral(*da)
}

// Q, optional, integer, since V1.4, 0,1,2
sinceVersion := model.V14
Expand Down Expand Up @@ -1177,10 +1185,18 @@ func validateAnnotationDictRedact(xRefTable *model.XRefTable, d types.Dict, dict
}

// DA, required, byte string
_, err = validateStringEntry(xRefTable, d, dictName, "DA", REQUIRED, model.V10, nil)
validate := validateDA
if xRefTable.ValidationMode == model.ValidationRelaxed {
validate = validateDARelaxed
}
da, err := validateStringEntry(xRefTable, d, dictName, "DA", REQUIRED, model.V10, validate)
if err != nil {
return err
}
if xRefTable.ValidationMode == model.ValidationRelaxed && da != nil {
// Repair
d["DA"] = types.StringLiteral(*da)
}

// Q, optional, integer
_, err = validateIntegerEntry(xRefTable, d, dictName, "Q", OPTIONAL, model.V10, nil)
Expand Down
14 changes: 13 additions & 1 deletion pkg/pdfcpu/validate/form.go
Expand Up @@ -234,6 +234,10 @@ func validateFormFieldDA(xRefTable *model.XRefTable, d types.Dict, dictName stri
if err != nil {
return false, err
}
if xRefTable.ValidationMode == model.ValidationRelaxed && da != nil {
// Repair
d["DA"] = types.StringLiteral(*da)
}

return da != nil && *da != "", nil
}
Expand Down Expand Up @@ -551,10 +555,18 @@ func validateForm(xRefTable *model.XRefTable, rootDict types.Dict, required bool
dictName := "acroFormDict"

// DA: optional, string
da, err := validateStringEntry(xRefTable, d, dictName, "DA", OPTIONAL, model.V10, validateDA)
validate := validateDA
if xRefTable.ValidationMode == model.ValidationRelaxed {
validate = validateDARelaxed
}
da, err := validateStringEntry(xRefTable, d, dictName, "DA", OPTIONAL, model.V10, validate)
if err != nil {
return err
}
if xRefTable.ValidationMode == model.ValidationRelaxed && da != nil {
// Repair
d["DA"] = types.StringLiteral(*da)
}

requiresDA := da == nil || len(*da) == 0

Expand Down
2 changes: 1 addition & 1 deletion pkg/pdfcpu/validate/object.go
Expand Up @@ -1032,7 +1032,7 @@ func decodeString(o types.Object, dictName, entryName string) (s string, err err
case types.HexLiteral:
s, err = types.HexLiteralToString(o)
default:
err = errors.Errorf("pdfcpu: decodeString: dict=%s entry=%s invalid type", dictName, entryName)
err = errors.Errorf("pdfcpu: decodeString: dict=%s entry=%s invalid type %T", dictName, entryName, o)
}
return s, err
}
Expand Down
14 changes: 13 additions & 1 deletion pkg/pdfcpu/validate/structTree.go
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package validate

import (
"strconv"

"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types"
"github.com/pkg/errors"
Expand Down Expand Up @@ -353,7 +355,17 @@ func validateStructElementDictPart1(xRefTable *model.XRefTable, d types.Dict, di
// S: structure type, required, name, see 14.7.3 and Annex E.
_, err := validateNameEntry(xRefTable, d, dictName, "S", OPTIONAL, model.V10, nil)
if err != nil {
return err
if xRefTable.ValidationMode == model.ValidationStrict {
return err
}
i, err := validateIntegerEntry(xRefTable, d, dictName, "S", OPTIONAL, model.V10, nil)
if err != nil {
return err
}
if i != nil {
// Repair
d["S"] = types.Name(strconv.Itoa((*i).Value()))
}
}

// P: immediate parent, required, indirect reference
Expand Down

0 comments on commit 9295163

Please sign in to comment.