unidoc / unipdf

Golang PDF library for creating and processing PDF files (pure go)
https://unidoc.io
Other
2.5k stars 249 forks source link

[FEATURE] Partial form flattening through explicit configuration #392

Closed ghost closed 3 years ago

ghost commented 4 years ago

Is your feature request related to a problem? Please describe.

Partially related to this PR: https://github.com/unidoc/unipdf/pull/391

Describe the solution you'd like

When using the FlattenFields method on a PDF Reader object, i want to be able to pass a list/map of field names to be flattened, so only those will be, independently of any internal working of the library.

Describe alternatives you've considered

Patching the library and implement it myself, or check for other products.

I also see this comment: https://github.com/unidoc/unipdf/blob/master/model/flatten.go#L51

// TODO(gunnsth): Check if wa.Flags() has Print flag then include, otherwise exclude.

Additional context

When using checkboxes, we cannot decide which ones we want to see flattened and which one we don't want to see flattened.

This currently works on text fields only because the library checks for field.V != nil to excluse those forms from flattening, but not on other types. Here: https://github.com/unidoc/unipdf/blob/master/model/flatten.go#L54

PS: we bought a "Basic" licence (not affiliated with this github account though), you can contact me in private if there is a possibility to prioritise this request.

github-actions[bot] commented 4 years ago

Welcome! Thanks for posting your first issue. The way things work here is that while customer issues are prioritized, other issues go into our backlog where they are assessed and fitted into the roadmap when suitable. If you need to get this done, consider buying a license which also enables you to use it in your commercial products. More information can be found on https://unidoc.io/

gunnsth commented 4 years ago

@florentpeterschmitt-lp Thanks for posting the issue. Can you describe the use case a little bit? Why do you only want to flatten a subset of fields (partial)?

When using checkboxes, we cannot decide which ones we want to see flattened and which one we don't want to see flattened.

Why not just flatten everything? Are you concerned about breaking something else? Or are you only managing a part of a form?

ghost commented 4 years ago

Thanks for your answer!

@florentpeterschmitt-lp Thanks for posting the issue. Can you describe the use case a little bit? Why do you only want to flatten a subset of fields (partial)?

Why not just flatten everything? Are you concerned about breaking something else? Or are you only managing a part of a form?

We want to let the possibility for a user to manually fill some forms, because we do not/cannot own all the information during the generation process :)

gunnsth commented 3 years ago

Thanks. We do have plans to add this functionality with a filter function that can determine which fields to include in flattening.

gunnsth commented 3 years ago

Support for this has been added via

func (r *PdfReader) FlattenFieldsWithOpts(appgen FieldAppearanceGenerator, opts *FieldFlattenOpts) error {

and

// FieldFlattenOpts defines a set of options which can be used to configure
// the field flattening process.
type FieldFlattenOpts struct {
    // FilterFunc allows filtering the form fields used in the flattening
    // process. If the filter function returns true, the field is flattened,
    // otherwise it is skipped.
    // If a non-terminal field is discarded, all of its children (the fields
    // present in the Kids array) are discarded as well.
    // Non-terminal fields are kept in the AcroForm if one or more of their
    // child fields have not been selected for flattening.
    // If a filter function is not provided, all form fields are flattened.
    FilterFunc FieldFilterFunc
}

where

// FieldFilterFunc represents a PDF field filtering function. If the function
// returns true, the PDF field is kept, otherwise it is discarded.
type FieldFilterFunc func(*PdfField) bool

determines what fields are flattened

ghost commented 3 years ago

Sorry for [very] late reply, and thank for this addition ! I did not see the notification :)